aboutsummaryrefslogtreecommitdiffstats
path: root/common
diff options
context:
space:
mode:
authorChris Robinson <[email protected]>2022-05-16 23:08:07 -0700
committerChris Robinson <[email protected]>2022-05-16 23:56:07 -0700
commita5729d1ff6cc5e04d3ee108e553b45eeaa0fec9e (patch)
treebc915e54d10ec2fe751b78a41eab2c497c447df7 /common
parent3e9597a76829717ff5c6dac718bce7a0f072ba4a (diff)
Check the container type given to DEF_NEWDEL and DEF_FAM_NEWDEL
There's apparently no way to get the containing class/struct type from a static member function, and operator new/delete are implicitly static member functions so the macros to define them need to be told the type to allocate for. This ensures the type specified matches the containing type.
Diffstat (limited to 'common')
-rw-r--r--common/almalloc.h10
1 files changed, 7 insertions, 3 deletions
diff --git a/common/almalloc.h b/common/almalloc.h
index 711d02fd..295107dc 100644
--- a/common/almalloc.h
+++ b/common/almalloc.h
@@ -29,9 +29,11 @@ void *al_calloc(size_t alignment, size_t size);
#define DEF_NEWDEL(T) \
void *operator new(size_t size) \
{ \
- void *ret = al_malloc(alignof(T), size); \
- if(!ret) throw std::bad_alloc(); \
- return ret; \
+ static_assert(&operator new == &T::operator new, \
+ "Incorrect container type specified"); \
+ if(void *ret{al_malloc(alignof(T), size)}) \
+ return ret; \
+ throw std::bad_alloc(); \
} \
void *operator new[](size_t size) { return operator new(size); } \
void operator delete(void *block) noexcept { al_free(block); } \
@@ -50,6 +52,8 @@ enum FamCount : size_t { };
#define DEF_FAM_NEWDEL(T, FamMem) \
static constexpr size_t Sizeof(size_t count) noexcept \
{ \
+ static_assert(&Sizeof == &T::Sizeof, \
+ "Incorrect container type specified"); \
return std::max(decltype(FamMem)::Sizeof(count, offsetof(T, FamMem)), \
sizeof(T)); \
} \