Skip to content

Commit c99257c

Browse files
committed
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
1 parent e19e121 commit c99257c

File tree

2 files changed

+19
-10
lines changed

2 files changed

+19
-10
lines changed

include/boost/container/detail/aligned_alloc.hpp renamed to include/boost/container/detail/aligned_allocation.hpp

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
// See http://www.boost.org/libs/container for documentation.
88
//
99
//////////////////////////////////////////////////////////////////////////////
10-
#ifndef BOOST_CONTAINER_DETAIL_ALIGNED_ALLOC_HPP
11-
#define BOOST_CONTAINER_DETAIL_ALIGNED_ALLOC_HPP
10+
#ifndef BOOST_CONTAINER_DETAIL_ALIGNED_ALLOCATION_HPP
11+
#define BOOST_CONTAINER_DETAIL_ALIGNED_ALLOCATION_HPP
1212

1313
#ifndef BOOST_CONFIG_HPP
1414
# include <boost/config.hpp>
@@ -25,8 +25,6 @@
2525
#include <unistd.h> //Include it to detect POSIX features
2626
#if defined(_POSIX_C_SOURCE) && (_POSIX_C_SOURCE >= 200112L)
2727
#define BOOST_CONTAINER_HAS_POSIX_MEMALIGN
28-
#elif defined(_XOPEN_SOURCE) && (_XOPEN_SOURCE >= 600)
29-
#define BOOST_CONTAINER_HAS_POSIX_MEMALIGN
3028
#elif defined(__APPLE__)
3129
#include <Availability.h>
3230
#if __MAC_OS_X_VERSION_MIN_REQUIRED >= 101500
@@ -86,7 +84,7 @@ inline void* aligned_allocate(std::size_t al, std::size_t sz)
8684
std::size_t rounded_size = std::size_t(sz + al - 1u) & ~std::size_t(al - 1);
8785

8886
//Check for rounded size overflow
89-
return rounded_size ? aligned_allocate(al, rounded_size) : 0;
87+
return rounded_size ? ::aligned_alloc(al, rounded_size) : 0;
9088
}
9189

9290
#elif defined(BOOST_CONTAINER_HAS_ALIGNED_MALLOC)
@@ -106,12 +104,12 @@ inline void* aligned_allocate(std::size_t al, std::size_t sz)
106104
return 0;
107105

108106
//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*);
107+
const std::size_t raw_addr = reinterpret_cast<std::size_t>(mptr);
108+
const std::size_t offset = sizeof(void*);
111109
void *const ptr = reinterpret_cast<void*>((raw_addr + offset + al - 1u) & ~(al - 1u));
112110

113111
// Store the original pointer just before the aligned address
114-
void** backpointer = reinterpret_cast<void**>(ptr) - 1;
112+
void** const backpointer = reinterpret_cast<void**>(ptr) - 1;
115113
*backpointer = mptr;
116114
return ptr;
117115
}
@@ -149,4 +147,4 @@ inline void aligned_deallocate(void* ptr)
149147
} //namespace container {
150148
} //namespace boost {
151149

152-
#endif //#ifndef BOOST_CONTAINER_DETAIL_ALIGNED_ALLOC_HPP
150+
#endif //#ifndef BOOST_CONTAINER_DETAIL_ALIGNED_ALLOCATION_HPP

include/boost/container/detail/operator_new_helpers.hpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,24 @@
2323
#include <boost/container/detail/type_traits.hpp>
2424

2525
#if !defined(__cpp_aligned_new)
26-
#include <boost/container/detail/aligned_alloc.hpp>
26+
#include <boost/container/detail/aligned_allocation.hpp>
2727
#endif
2828

2929
namespace boost {
3030
namespace container {
3131
namespace dtl {
3232

33+
//For GCC and clang there are several cases where __STDCPP_DEFAULT_NEW_ALIGNMENT__
34+
//is not properly synchronized with the default alignment of malloc. Examples:
35+
//
36+
// - On Unix platforms, a programmer uses jemalloc, mimalloc that have historically a lower
37+
// default alignment (to waste less memory)
38+
//
39+
// - On Windows platforms, the allocator is provided by MSVCRT o UCRT that uses HeapAlloc
40+
// (e.g. 8 byte alignment for x86 and 16 bytes for x64)
41+
//
42+
// - On Apple platforms the default malloc implementation has a reduced defaykt alignment
43+
// even on ARM64 platforms.
3344
BOOST_CONTAINER_FORCEINLINE bool operator_new_raw_overaligned(std::size_t alignment)
3445
{
3546
//In MacOs, the default allocator can return data aligned to 8 bytes

0 commit comments

Comments
 (0)