Skip to content

Commit e4fcf4b

Browse files
committed
Add overaligned type support for global pmr: implement operator_new_raw_allocate/operator_delete_raw_deallocate and use them in new_delete_resource_imp
1 parent ce530f5 commit e4fcf4b

File tree

2 files changed

+34
-18
lines changed

2 files changed

+34
-18
lines changed

include/boost/container/detail/operator_new_helpers.hpp

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,47 +20,62 @@
2020

2121
#include <boost/container/detail/std_fwd.hpp>
2222
#include <boost/container/throw_exception.hpp>
23+
#include <boost/container/detail/type_traits.hpp>
2324

2425
namespace boost {
2526
namespace container {
2627
namespace dtl {
2728

28-
template <class T>
29-
T* operator_new_allocate(std::size_t count)
29+
BOOST_CONTAINER_FORCEINLINE void* operator_new_raw_allocate(const std::size_t size, const std::size_t alignment)
3030
{
31-
const std::size_t max_count = std::size_t(-1)/(2*sizeof(T));
32-
if(BOOST_UNLIKELY(count > max_count))
33-
throw_bad_alloc();
31+
(void)alignment;
3432
#if defined(__cpp_aligned_new)
35-
BOOST_IF_CONSTEXPR(__STDCPP_DEFAULT_NEW_ALIGNMENT__ < alignof(T)) {
36-
return static_cast<T*>(::operator new(count*sizeof(T), std::align_val_t(alignof(T))));
33+
if(__STDCPP_DEFAULT_NEW_ALIGNMENT__ < alignment) {
34+
return ::operator new(size, std::align_val_t(alignment));
3735
}
3836
#endif
39-
return static_cast<T*>(::operator new(count*sizeof(T)));
37+
return ::operator new(size);
4038
}
4139

42-
template <class T>
43-
void operator_delete_deallocate(T* ptr, std::size_t n) BOOST_NOEXCEPT_OR_NOTHROW
40+
BOOST_CONTAINER_FORCEINLINE void operator_delete_raw_deallocate
41+
(void* const ptr, const std::size_t size, const std::size_t alignment) BOOST_NOEXCEPT_OR_NOTHROW
4442
{
45-
(void)n;
43+
(void)size;
44+
(void)alignment;
4645
#ifdef __cpp_aligned_new
47-
BOOST_IF_CONSTEXPR(__STDCPP_DEFAULT_NEW_ALIGNMENT__ < alignof(T)) {
46+
if(__STDCPP_DEFAULT_NEW_ALIGNMENT__ < alignment) {
4847
# if defined(__cpp_sized_deallocation)
49-
::operator delete((void*)ptr, n * sizeof(T), std::align_val_t(alignof(T)));
48+
::operator delete(ptr, size, std::align_val_t(alignment));
5049
#else
51-
::operator delete((void*)ptr, std::align_val_t(alignof(T)));
50+
::operator delete(ptr, std::align_val_t(alignment));
5251
# endif
5352
return;
5453
}
5554
#endif
5655

5756
# if defined(__cpp_sized_deallocation)
58-
::operator delete((void*)ptr, n * sizeof(T));
57+
::operator delete(ptr, size);
5958
#else
60-
::operator delete((void*)ptr);
59+
::operator delete(ptr);
6160
# endif
6261
}
6362

63+
template <class T>
64+
BOOST_CONTAINER_FORCEINLINE T* operator_new_allocate(std::size_t count)
65+
{
66+
const std::size_t max_count = std::size_t(-1)/(2*sizeof(T));
67+
if(BOOST_UNLIKELY(count > max_count))
68+
throw_bad_alloc();
69+
return static_cast<T*>(operator_new_raw_allocate(count*sizeof(T), alignment_of<T>::value));
70+
}
71+
72+
template <class T>
73+
BOOST_CONTAINER_FORCEINLINE void operator_delete_deallocate(T* ptr, std::size_t n) BOOST_NOEXCEPT_OR_NOTHROW
74+
{
75+
operator_delete_raw_deallocate((void*)ptr, n * sizeof(T), alignment_of<T>::value);
76+
}
77+
78+
6479
} //namespace dtl {
6580
} //namespace container {
6681
} //namespace boost {

src/global_resource.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <boost/container/throw_exception.hpp>
1515
#include <boost/container/detail/dlmalloc.hpp> //For global lock
1616
#include <boost/container/detail/singleton.hpp>
17+
#include <boost/container/detail/operator_new_helpers.hpp>
1718

1819
#include <cstddef>
1920
#include <new>
@@ -31,10 +32,10 @@ class new_delete_resource_imp
3132
{}
3233

3334
void* do_allocate(std::size_t bytes, std::size_t alignment) BOOST_OVERRIDE
34-
{ (void)bytes; (void)alignment; return new char[bytes]; }
35+
{ return boost::container::dtl::operator_new_raw_allocate(bytes, alignment); }
3536

3637
void do_deallocate(void* p, std::size_t bytes, std::size_t alignment) BOOST_OVERRIDE
37-
{ (void)bytes; (void)alignment; delete[]((char*)p); }
38+
{ return boost::container::dtl::operator_delete_raw_deallocate(p, bytes, alignment); }
3839

3940
bool do_is_equal(const memory_resource& other) const BOOST_NOEXCEPT BOOST_OVERRIDE
4041
{ return &other == this; }

0 commit comments

Comments
 (0)