From c99257c1dc5d9f2a236be9e545b6262ae785aff8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ion=20Gazta=C3=B1aga?= Date: Fri, 26 Dec 2025 16:14:33 +0100 Subject: [PATCH] Fix recursion in aligned_allocation. Rename aligned_alloc.hpp to aligned_allocation.hpp to be consistent: - "aligned_allocation" will be the internal name for the utility in the Container library. - "aligned_alloc" is the C11 function name --- ...{aligned_alloc.hpp => aligned_allocation.hpp} | 16 +++++++--------- .../container/detail/operator_new_helpers.hpp | 13 ++++++++++++- 2 files changed, 19 insertions(+), 10 deletions(-) rename include/boost/container/detail/{aligned_alloc.hpp => aligned_allocation.hpp} (88%) diff --git a/include/boost/container/detail/aligned_alloc.hpp b/include/boost/container/detail/aligned_allocation.hpp similarity index 88% rename from include/boost/container/detail/aligned_alloc.hpp rename to include/boost/container/detail/aligned_allocation.hpp index 42b4c08..5599dd6 100644 --- a/include/boost/container/detail/aligned_alloc.hpp +++ b/include/boost/container/detail/aligned_allocation.hpp @@ -7,8 +7,8 @@ // See http://www.boost.org/libs/container for documentation. // ////////////////////////////////////////////////////////////////////////////// -#ifndef BOOST_CONTAINER_DETAIL_ALIGNED_ALLOC_HPP -#define BOOST_CONTAINER_DETAIL_ALIGNED_ALLOC_HPP +#ifndef BOOST_CONTAINER_DETAIL_ALIGNED_ALLOCATION_HPP +#define BOOST_CONTAINER_DETAIL_ALIGNED_ALLOCATION_HPP #ifndef BOOST_CONFIG_HPP # include @@ -25,8 +25,6 @@ #include //Include it to detect POSIX features #if defined(_POSIX_C_SOURCE) && (_POSIX_C_SOURCE >= 200112L) #define BOOST_CONTAINER_HAS_POSIX_MEMALIGN - #elif defined(_XOPEN_SOURCE) && (_XOPEN_SOURCE >= 600) - #define BOOST_CONTAINER_HAS_POSIX_MEMALIGN #elif defined(__APPLE__) #include #if __MAC_OS_X_VERSION_MIN_REQUIRED >= 101500 @@ -86,7 +84,7 @@ inline void* aligned_allocate(std::size_t al, std::size_t sz) std::size_t rounded_size = std::size_t(sz + al - 1u) & ~std::size_t(al - 1); //Check for rounded size overflow - return rounded_size ? aligned_allocate(al, rounded_size) : 0; + return rounded_size ? ::aligned_alloc(al, rounded_size) : 0; } #elif defined(BOOST_CONTAINER_HAS_ALIGNED_MALLOC) @@ -106,12 +104,12 @@ inline void* aligned_allocate(std::size_t al, std::size_t sz) return 0; //Now align the returned pointer (which will be aligned at least to sizeof(void*) - std::size_t raw_addr = reinterpret_cast(mptr); - std::size_t offset = sizeof(void*); + const std::size_t raw_addr = reinterpret_cast(mptr); + const std::size_t offset = sizeof(void*); void *const ptr = reinterpret_cast((raw_addr + offset + al - 1u) & ~(al - 1u)); // Store the original pointer just before the aligned address - void** backpointer = reinterpret_cast(ptr) - 1; + void** const backpointer = reinterpret_cast(ptr) - 1; *backpointer = mptr; return ptr; } @@ -149,4 +147,4 @@ inline void aligned_deallocate(void* ptr) } //namespace container { } //namespace boost { -#endif //#ifndef BOOST_CONTAINER_DETAIL_ALIGNED_ALLOC_HPP +#endif //#ifndef BOOST_CONTAINER_DETAIL_ALIGNED_ALLOCATION_HPP diff --git a/include/boost/container/detail/operator_new_helpers.hpp b/include/boost/container/detail/operator_new_helpers.hpp index 1bfd69d..07d60fe 100644 --- a/include/boost/container/detail/operator_new_helpers.hpp +++ b/include/boost/container/detail/operator_new_helpers.hpp @@ -23,13 +23,24 @@ #include #if !defined(__cpp_aligned_new) -#include +#include #endif namespace boost { namespace container { namespace dtl { +//For GCC and clang there are several cases where __STDCPP_DEFAULT_NEW_ALIGNMENT__ +//is not properly synchronized with the default alignment of malloc. Examples: +// +// - On Unix platforms, a programmer uses jemalloc, mimalloc that have historically a lower +// default alignment (to waste less memory) +// +// - On Windows platforms, the allocator is provided by MSVCRT o UCRT that uses HeapAlloc +// (e.g. 8 byte alignment for x86 and 16 bytes for x64) +// +// - On Apple platforms the default malloc implementation has a reduced defaykt alignment +// even on ARM64 platforms. BOOST_CONTAINER_FORCEINLINE bool operator_new_raw_overaligned(std::size_t alignment) { //In MacOs, the default allocator can return data aligned to 8 bytes