Chris@16
|
1 /*
|
Chris@16
|
2 * Copyright Andrey Semashev 2007 - 2013.
|
Chris@16
|
3 * Distributed under the Boost Software License, Version 1.0.
|
Chris@16
|
4 * (See accompanying file LICENSE_1_0.txt or copy at
|
Chris@16
|
5 * http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
6 */
|
Chris@16
|
7 /*!
|
Chris@16
|
8 * \file malloc_aligned.hpp
|
Chris@16
|
9 * \author Andrey Semashev
|
Chris@16
|
10 * \date 12.07.2013
|
Chris@16
|
11 *
|
Chris@16
|
12 * \brief This header is the Boost.Log library implementation, see the library documentation
|
Chris@16
|
13 * at http://www.boost.org/doc/libs/release/libs/log/doc/html/index.html.
|
Chris@16
|
14 */
|
Chris@16
|
15
|
Chris@16
|
16 #ifndef BOOST_LOG_DETAIL_MALLOC_ALIGNED_HPP_INCLUDED_
|
Chris@16
|
17 #define BOOST_LOG_DETAIL_MALLOC_ALIGNED_HPP_INCLUDED_
|
Chris@16
|
18
|
Chris@16
|
19 #include <cstddef>
|
Chris@16
|
20 #include <cstdlib>
|
Chris@16
|
21 #include <boost/assert.hpp>
|
Chris@16
|
22 #include <boost/cstdint.hpp>
|
Chris@16
|
23 #include <boost/log/detail/config.hpp>
|
Chris@16
|
24
|
Chris@16
|
25 // MSVC has its own _aligned_malloc and _aligned_free functions.
|
Chris@16
|
26 // But MinGW doesn't declare these aligned memory allocation routines for MSVC 6 runtime.
|
Chris@16
|
27 #if defined(BOOST_WINDOWS) && !(defined(__MINGW32__) && __MSVCRT_VERSION__ < 0x0700)
|
Chris@16
|
28 #include <malloc.h>
|
Chris@16
|
29 #define BOOST_LOG_HAS_MSVC_ALIGNED_MALLOC 1
|
Chris@16
|
30 #endif
|
Chris@16
|
31
|
Chris@16
|
32 #if defined(BOOST_HAS_UNISTD_H)
|
Chris@16
|
33 #include <unistd.h> // _POSIX_VERSION
|
Chris@16
|
34 #endif
|
Chris@16
|
35
|
Chris@16
|
36 #if defined(__APPLE__) || defined(__APPLE_CC__) || defined(macintosh)
|
Chris@16
|
37 #include <AvailabilityMacros.h>
|
Chris@16
|
38 #if defined(MAC_OS_X_VERSION_MIN_REQUIRED) && (MAC_OS_X_VERSION_MIN_REQUIRED >= 1060)
|
Chris@16
|
39 // Mac OS X 10.6 and later have posix_memalign
|
Chris@16
|
40 #define BOOST_LOG_HAS_POSIX_MEMALIGN 1
|
Chris@16
|
41 #endif
|
Chris@16
|
42 #elif defined(__ANDROID__)
|
Chris@16
|
43 // Android NDK (up to release 8e, at least) doesn't have posix_memalign despite it defines POSIX macros as if it does.
|
Chris@16
|
44 // But we can use memalign() with free() on this platform.
|
Chris@16
|
45 #include <malloc.h>
|
Chris@16
|
46 #define BOOST_LOG_HAS_FREEABLE_MEMALIGN 1
|
Chris@16
|
47 #elif (defined(_POSIX_VERSION) && (_POSIX_VERSION >= 200112L)) || (defined(_XOPEN_SOURCE) && (_XOPEN_SOURCE >= 600))
|
Chris@16
|
48 // Solaris 10 does not have posix_memalign. Solaris 11 and later seem to have it.
|
Chris@16
|
49 #if !(defined(sun) || defined(__sun)) || defined(__SunOS_5_11) || defined(__SunOS_5_12)
|
Chris@16
|
50 #define BOOST_LOG_HAS_POSIX_MEMALIGN 1
|
Chris@16
|
51 #endif
|
Chris@16
|
52 #endif
|
Chris@16
|
53
|
Chris@16
|
54 #include <boost/log/detail/header.hpp>
|
Chris@16
|
55
|
Chris@16
|
56 #ifdef BOOST_HAS_PRAGMA_ONCE
|
Chris@16
|
57 #pragma once
|
Chris@16
|
58 #endif
|
Chris@16
|
59
|
Chris@16
|
60 #ifndef BOOST_LOG_CPU_CACHE_LINE_SIZE
|
Chris@16
|
61 //! The macro defines the CPU cache line size for the target architecture. This is mostly used for optimization.
|
Chris@16
|
62 #define BOOST_LOG_CPU_CACHE_LINE_SIZE 64
|
Chris@16
|
63 #endif
|
Chris@16
|
64
|
Chris@16
|
65 namespace boost {
|
Chris@16
|
66
|
Chris@16
|
67 BOOST_LOG_OPEN_NAMESPACE
|
Chris@16
|
68
|
Chris@16
|
69 namespace aux {
|
Chris@16
|
70
|
Chris@16
|
71 /*!
|
Chris@16
|
72 * Allocates uninitialized aligned memory. Memory alignment must be a power of 2 and a multiple of sizeof(void*).
|
Chris@16
|
73 * The implementation may impose an upper bound of the alignment as well.
|
Chris@16
|
74 */
|
Chris@16
|
75 inline void* malloc_aligned(std::size_t size, uint32_t alignment)
|
Chris@16
|
76 {
|
Chris@16
|
77 #if defined(BOOST_LOG_HAS_POSIX_MEMALIGN)
|
Chris@16
|
78 void* p = NULL;
|
Chris@16
|
79 if (posix_memalign(&p, alignment, size) != 0)
|
Chris@16
|
80 return NULL;
|
Chris@16
|
81 return p;
|
Chris@16
|
82 #elif defined(BOOST_LOG_HAS_FREEABLE_MEMALIGN)
|
Chris@16
|
83 return memalign(alignment, size);
|
Chris@16
|
84 #elif defined(BOOST_LOG_HAS_MSVC_ALIGNED_MALLOC)
|
Chris@16
|
85 return _aligned_malloc(size, alignment);
|
Chris@16
|
86 #else
|
Chris@16
|
87 BOOST_ASSERT(alignment >= sizeof(void*));
|
Chris@16
|
88 void* p = std::malloc(size + alignment);
|
Chris@16
|
89 if (p)
|
Chris@16
|
90 {
|
Chris@16
|
91 unsigned char* q = static_cast< unsigned char* >(p) + alignment;
|
Chris@16
|
92 #if defined(BOOST_HAS_INTPTR_T)
|
Chris@16
|
93 q = (unsigned char*)((uintptr_t)q & (~(uintptr_t)(alignment - 1u)));
|
Chris@16
|
94 #else
|
Chris@16
|
95 q -= ((std::size_t)q & (std::size_t)(alignment - 1u));
|
Chris@16
|
96 #endif
|
Chris@16
|
97 // Here we assume that the system allocator aligns to 4 bytes at the very least.
|
Chris@16
|
98 // Therefore we will always have at least 4 bytes before the aligned pointer.
|
Chris@16
|
99 const uint32_t diff = q - static_cast< unsigned char* >(p);
|
Chris@16
|
100 p = q;
|
Chris@16
|
101 *reinterpret_cast< uint32_t* >(q - 4u) = diff;
|
Chris@16
|
102 }
|
Chris@16
|
103 return p;
|
Chris@16
|
104 #endif
|
Chris@16
|
105 }
|
Chris@16
|
106
|
Chris@16
|
107 /*!
|
Chris@16
|
108 * Frees memory allocated with \c malloc_aligned.
|
Chris@16
|
109 */
|
Chris@16
|
110 inline void free_aligned(void* p)
|
Chris@16
|
111 {
|
Chris@16
|
112 #if defined(BOOST_LOG_HAS_POSIX_MEMALIGN) || defined(BOOST_LOG_HAS_FREEABLE_MEMALIGN)
|
Chris@16
|
113 free(p);
|
Chris@16
|
114 #elif defined(BOOST_LOG_HAS_MSVC_ALIGNED_MALLOC)
|
Chris@16
|
115 _aligned_free(p);
|
Chris@16
|
116 #else
|
Chris@16
|
117 if (p)
|
Chris@16
|
118 {
|
Chris@16
|
119 unsigned char* const q = static_cast< unsigned char* >(p);
|
Chris@16
|
120 const uint32_t diff = *reinterpret_cast< uint32_t* >(q - 4u);
|
Chris@16
|
121 std::free(q - diff);
|
Chris@16
|
122 }
|
Chris@16
|
123 #endif
|
Chris@16
|
124 }
|
Chris@16
|
125
|
Chris@16
|
126 } // namespace aux
|
Chris@16
|
127
|
Chris@16
|
128 BOOST_LOG_CLOSE_NAMESPACE // namespace log
|
Chris@16
|
129
|
Chris@16
|
130 } // namespace boost
|
Chris@16
|
131
|
Chris@16
|
132 #include <boost/log/detail/footer.hpp>
|
Chris@16
|
133
|
Chris@16
|
134 #endif // BOOST_LOG_DETAIL_MALLOC_ALIGNED_HPP_INCLUDED_
|