Skip to content

Commit 52b34fb

Browse files
committed
Add aligned new overloads and make sure zero-allocated malloc is not called (operator new requires non-null for zero-sized allocations).
1 parent 73ddb75 commit 52b34fb

File tree

1 file changed

+40
-1
lines changed

1 file changed

+40
-1
lines changed

test/global_resource_test.cpp

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,13 @@ std::size_t allocation_count = 0;
4444
void* operator new[](std::size_t count) BOOST_CONTAINER_NEW_EXCEPTION_SPECIFIER
4545
{
4646
++allocation_count;
47-
return std::malloc(count);
47+
return std::malloc(count ? count : 1u);
48+
}
49+
50+
void* operator new(std::size_t count) BOOST_CONTAINER_NEW_EXCEPTION_SPECIFIER
51+
{
52+
++allocation_count;
53+
return std::malloc(count ? count : 1u);
4854
}
4955

5056
void operator delete[](void *p) BOOST_CONTAINER_DELETE_EXCEPTION_SPECIFIER
@@ -53,6 +59,39 @@ void operator delete[](void *p) BOOST_CONTAINER_DELETE_EXCEPTION_SPECIFIER
5359
return std::free(p);
5460
}
5561

62+
void operator delete(void *p) BOOST_CONTAINER_DELETE_EXCEPTION_SPECIFIER
63+
{
64+
--allocation_count;
65+
return std::free(p);
66+
}
67+
68+
#if defined __cpp_aligned_new
69+
70+
void* operator new[](std::size_t count, std::align_val_t) BOOST_CONTAINER_NEW_EXCEPTION_SPECIFIER
71+
{
72+
++allocation_count;
73+
return std::malloc(count ? count : 1u);
74+
}
75+
76+
void* operator new(std::size_t count, std::align_val_t) BOOST_CONTAINER_NEW_EXCEPTION_SPECIFIER
77+
{
78+
++allocation_count;
79+
return std::malloc(count ? count : 1u);
80+
}
81+
82+
void operator delete[](void *p, std::align_val_t) BOOST_CONTAINER_DELETE_EXCEPTION_SPECIFIER
83+
{
84+
--allocation_count;
85+
return std::free(p);
86+
}
87+
88+
void operator delete(void *p, std::align_val_t) BOOST_CONTAINER_DELETE_EXCEPTION_SPECIFIER
89+
{
90+
--allocation_count;
91+
return std::free(p);
92+
}
93+
94+
#endif
5695
#endif //BOOST_CONTAINER_ASAN
5796

5897
#ifdef BOOST_MSVC

0 commit comments

Comments
 (0)