|
| 1 | +////////////////////////////////////////////////////////////////////////////// |
| 2 | +// |
| 3 | +// (C) Copyright Ion Gaztanaga 2025-2025. Distributed under the Boost |
| 4 | +// Software License, Version 1.0. (See accompanying file |
| 5 | +// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
| 6 | +// |
| 7 | +// See http://www.boost.org/libs/container for documentation. |
| 8 | +// |
| 9 | +////////////////////////////////////////////////////////////////////////////// |
| 10 | +#ifndef BOOST_CONTAINER_DETAIL_ALIGNED_ALLOC_HPP |
| 11 | +#define BOOST_CONTAINER_DETAIL_ALIGNED_ALLOC_HPP |
| 12 | + |
| 13 | +#ifndef BOOST_CONFIG_HPP |
| 14 | +# include <boost/config.hpp> |
| 15 | +#endif |
| 16 | + |
| 17 | +#if defined(BOOST_HAS_PRAGMA_ONCE) |
| 18 | +# pragma once |
| 19 | +#endif |
| 20 | + |
| 21 | +// Platform detection |
| 22 | +#if defined(_WIN32) && !defined(__CYGWIN__) |
| 23 | + #define BOOST_CONTAINER_HAS_ALIGNED_MALLOC |
| 24 | +#else |
| 25 | + #include <unistd.h> //Include it to detect POSIX features |
| 26 | + #if defined(_POSIX_C_SOURCE) && (_POSIX_C_SOURCE >= 200112L) |
| 27 | + #define BOOST_CONTAINER_HAS_POSIX_MEMALIGN |
| 28 | + #elif defined(_XOPEN_SOURCE) && (_XOPEN_SOURCE >= 600) |
| 29 | + #define BOOST_CONTAINER_HAS_POSIX_MEMALIGN |
| 30 | + #elif defined(__APPLE__) |
| 31 | + #include <Availability.h> |
| 32 | + #if __MAC_OS_X_VERSION_MIN_REQUIRED >= 101500 |
| 33 | + #define BOOST_CONTAINER_HAS_ALIGNED_ALLOC |
| 34 | + #else |
| 35 | + #define BOOST_CONTAINER_HAS_POSIX_MEMALIGN |
| 36 | + #endif |
| 37 | + #elif defined(__ANDROID__) |
| 38 | + #if (__ANDROID_API__ >= 28) |
| 39 | + #define BOOST_CONTAINER_HAS_ALIGNED_ALLOC |
| 40 | + #else |
| 41 | + #define BOOST_CONTAINER_HAS_POSIX_MEMALIGN |
| 42 | + #endif |
| 43 | + #elif defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L) |
| 44 | + #define BOOST_CONTAINER_HAS_ALIGNED_ALLOC |
| 45 | + #endif |
| 46 | +#endif |
| 47 | + |
| 48 | +// Include |
| 49 | +#if defined(BOOST_CONTAINER_HAS_ALIGNED_MALLOC) |
| 50 | + #include <malloc.h> |
| 51 | +#elif defined(BOOST_CONTAINER_HAS_POSIX_MEMALIGN) |
| 52 | + #include <stdlib.h> |
| 53 | +#elif defined(BOOST_CONTAINER_HAS_ALIGNED_ALLOC) |
| 54 | + #include <stdlib.h> |
| 55 | +#else |
| 56 | + #include <stdlib.h> //for malloc |
| 57 | +#endif |
| 58 | + |
| 59 | +namespace boost { |
| 60 | +namespace container { |
| 61 | +namespace dtl { |
| 62 | + |
| 63 | +#if defined(BOOST_CONTAINER_HAS_POSIX_MEMALIGN) |
| 64 | + |
| 65 | +inline void* aligned_allocate(std::size_t al, std::size_t sz) |
| 66 | +{ |
| 67 | + void *ptr; |
| 68 | + // posix_memalign requires aligned multiple of void* |
| 69 | + if (al < sizeof(void*)) |
| 70 | + al = sizeof(void*); |
| 71 | + int ret = posix_memalign(&ptr, al, sz); |
| 72 | + if (ret != 0) |
| 73 | + return 0; |
| 74 | + return ptr; |
| 75 | +} |
| 76 | + |
| 77 | +#elif defined(BOOST_CONTAINER_HAS_ALIGNED_ALLOC) |
| 78 | + |
| 79 | +inline void* aligned_allocate(std::size_t al, std::size_t sz) |
| 80 | +{ |
| 81 | + // Some aligned_allocate are based on posix_memalign so require also minimal alignment |
| 82 | + if (al < sizeof(void*)) |
| 83 | + al = sizeof(void*); |
| 84 | + |
| 85 | + // aligned_allocate requires size to be a multiple of alignment |
| 86 | + std::size_t rounded_size = std::size_t(sz + al - 1u) & ~std::size_t(al - 1); |
| 87 | + |
| 88 | + //Check for rounded size overflow |
| 89 | + return rounded_size ? aligned_allocate(al, rounded_size) : 0; |
| 90 | +} |
| 91 | + |
| 92 | +#elif defined(BOOST_CONTAINER_HAS_ALIGNED_MALLOC) |
| 93 | + |
| 94 | +inline void* aligned_allocate(std::size_t al, std::size_t sz) |
| 95 | +{ |
| 96 | + return _aligned_malloc(sz, al); |
| 97 | +} |
| 98 | + |
| 99 | +#else |
| 100 | + |
| 101 | +inline void* aligned_allocate(std::size_t al, std::size_t sz) |
| 102 | +{ |
| 103 | + //Make room for a back pointer metadata |
| 104 | + void* const mptr = malloc(sz + sizeof(void*) + al); |
| 105 | + if (!mptr) |
| 106 | + return 0; |
| 107 | + |
| 108 | + //Now align the returned pointer (which will be aligned at least to sizeof(void*) |
| 109 | + std::size_t raw_addr = reinterpret_cast<std::size_t>(mptr); |
| 110 | + std::size_t offset = sizeof(void*); |
| 111 | + void *const ptr = reinterpret_cast<void*>((raw_addr + offset + al - 1u) & ~(al - 1u)); |
| 112 | + |
| 113 | + // Store the original pointer just before the aligned address |
| 114 | + void** backpointer = reinterpret_cast<void**>(ptr) - 1; |
| 115 | + *backpointer = mptr; |
| 116 | + return ptr; |
| 117 | +} |
| 118 | + |
| 119 | +#endif |
| 120 | + |
| 121 | +#if defined(BOOST_CONTAINER_HAS_ALIGNED_ALLOC) || defined(BOOST_CONTAINER_HAS_POSIX_MEMALIGN) |
| 122 | + |
| 123 | +inline void aligned_deallocate(void* ptr) |
| 124 | +{ |
| 125 | + if (!ptr) |
| 126 | + return; |
| 127 | + free(ptr); |
| 128 | +} |
| 129 | + |
| 130 | +#elif defined(BOOST_CONTAINER_HAS_ALIGNED_MALLOC) |
| 131 | + |
| 132 | +inline void aligned_deallocate(void* ptr) |
| 133 | +{ |
| 134 | + _aligned_free(ptr); //_aligned_free supports NULL ptr |
| 135 | +} |
| 136 | + |
| 137 | +#else |
| 138 | + |
| 139 | +inline void aligned_deallocate(void* ptr) |
| 140 | +{ |
| 141 | + // Obtain backpointer data and free it |
| 142 | + void** storage = reinterpret_cast<void**>(ptr) - 1; |
| 143 | + free(*storage); |
| 144 | +} |
| 145 | + |
| 146 | +#endif// |
| 147 | + |
| 148 | +} //namespace dtl { |
| 149 | +} //namespace container { |
| 150 | +} //namespace boost { |
| 151 | + |
| 152 | +#endif //#ifndef BOOST_CONTAINER_DETAIL_ALIGNED_ALLOC_HPP |
0 commit comments