Skip to content

Commit e18078f

Browse files
committed
Define operator_new_allocate/operator_delete_deallocate helpers, taking care of __cpp_aligned_new and __cpp_sized_deallocation, and use them in new_allocator
1 parent ef4626b commit e18078f

File tree

2 files changed

+70
-10
lines changed

2 files changed

+70
-10
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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_OPERATOR_NEW_HELPERS_HPP
11+
#define BOOST_CONTAINER_DETAIL_OPERATOR_NEW_HELPERS_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+
#include <boost/container/detail/std_fwd.hpp>
22+
23+
namespace boost {
24+
namespace container {
25+
namespace dtl {
26+
27+
template <class T>
28+
T* operator_new_allocate(std::size_t count)
29+
{
30+
const std::size_t max_count = std::size_t(-1)/(2*sizeof(T));
31+
if(BOOST_UNLIKELY(count > max_count))
32+
throw_bad_alloc();
33+
#if defined(__cpp_aligned_new)
34+
BOOST_IF_CONSTEXPR(__STDCPP_DEFAULT_NEW_ALIGNMENT__ < alignof(T)) {
35+
return static_cast<T*>(::operator new(count*sizeof(T), std::align_val_t(alignof(T))));
36+
}
37+
#endif
38+
return static_cast<T*>(::operator new(count*sizeof(T)));
39+
}
40+
41+
template <class T>
42+
void operator_delete_deallocate(T* ptr, std::size_t n) BOOST_NOEXCEPT_OR_NOTHROW
43+
{
44+
(void)n;
45+
#ifdef __cpp_aligned_new
46+
BOOST_IF_CONSTEXPR(__STDCPP_DEFAULT_NEW_ALIGNMENT__ < alignof(T)) {
47+
# if defined(__cpp_sized_deallocation)
48+
::operator delete((void*)ptr, n * sizeof(T), std::align_val_t(alignof(T)));
49+
#else
50+
::operator delete((void*)ptr, std::align_val_t(alignof(T)));
51+
# endif
52+
return;
53+
}
54+
#endif
55+
56+
# if defined(__cpp_sized_deallocation)
57+
::operator delete((void*)ptr, n * sizeof(T));
58+
#else
59+
::operator delete((void*)ptr);
60+
# endif
61+
}
62+
63+
} //namespace dtl {
64+
} //namespace container {
65+
} //namespace boost {
66+
67+
#endif //#ifndef BOOST_CONTAINER_DETAIL_OPERATOR_NEW_HELPERS_HPP

include/boost/container/new_allocator.hpp

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include <boost/container/detail/config_begin.hpp>
2323
#include <boost/container/detail/workaround.hpp>
2424
#include <boost/container/throw_exception.hpp>
25+
#include <boost/container/detail/operator_new_helpers.hpp>
2526
#include <cstddef>
2627

2728
//!\file
@@ -152,22 +153,14 @@ class new_allocator
152153
//!Throws bad_alloc if there is no enough memory
153154
pointer allocate(size_type count)
154155
{
155-
const std::size_t max_count = std::size_t(-1)/(2*sizeof(T));
156-
if(BOOST_UNLIKELY(count > max_count))
157-
throw_bad_alloc();
158-
return static_cast<T*>(::operator new(count*sizeof(T)));
156+
return dtl::operator_new_allocate<T>(count);
159157
}
160158

161159
//!Deallocates previously allocated memory.
162160
//!Never throws
163161
void deallocate(pointer ptr, size_type n) BOOST_NOEXCEPT_OR_NOTHROW
164162
{
165-
(void)n;
166-
# if defined(__cpp_sized_deallocation)
167-
::operator delete((void*)ptr, n * sizeof(T));
168-
#else
169-
::operator delete((void*)ptr);
170-
# endif
163+
return dtl::operator_delete_deallocate<T>(ptr, n);
171164
}
172165

173166
//!Returns the maximum number of elements that could be allocated.

0 commit comments

Comments
 (0)