Guarded allocator: Ensure alignment and size of MemHead #120582
@ -258,6 +258,13 @@ void MEM_use_guarded_allocator(void);
|
||||
# include <type_traits>
|
||||
# include <utility>
|
||||
|
||||
/* Conservative value of memory alignment returned by non-aligned OS-level memory allocation
|
||||
|
||||
* functions. For alignments smaller than this value, using non-aligned versions of allocator API
|
||||
Hans Goudey
commented
How about adding How about adding `For alignments smaller than this value, using non-aligned versions of allocator API functions is okay, allowing use of calloc, for example.`
Sergey Sharybin
commented
Sounds good to me! Sounds good to me!
|
||||
* functions is okay, allowing use of calloc, for example. */
|
||||
# define MEM_MIN_CPP_ALIGNMENT \
|
||||
(__STDCPP_DEFAULT_NEW_ALIGNMENT__ < alignof(void *) ? __STDCPP_DEFAULT_NEW_ALIGNMENT__ : \
|
||||
alignof(void *))
|
||||
|
||||
/**
|
||||
* Allocate new memory for and constructs an object of type #T.
|
||||
* #MEM_delete should be used to delete the object. Just calling #MEM_freeN is not enough when #T
|
||||
|
@ -129,6 +129,8 @@ typedef struct MemHead {
|
||||
#endif
|
||||
|
||||
} MemHead;
|
||||
static_assert(MEM_MIN_CPP_ALIGNMENT <= alignof(MemHead), "Bad alignment of MemHead");
|
||||
static_assert(MEM_MIN_CPP_ALIGNMENT <= sizeof(MemHead), "Bad size of MemHead");
|
||||
|
||||
typedef MemHead MemHeadAligned;
|
||||
|
||||
|
@ -32,11 +32,15 @@ typedef struct MemHead {
|
||||
/* Length of allocated memory block. */
|
||||
size_t len;
|
||||
} MemHead;
|
||||
static_assert(MEM_MIN_CPP_ALIGNMENT <= alignof(MemHead), "Bad alignment of MemHead");
|
||||
static_assert(MEM_MIN_CPP_ALIGNMENT <= sizeof(MemHead), "Bad size of MemHead");
|
||||
|
||||
typedef struct MemHeadAligned {
|
||||
short alignment;
|
||||
size_t len;
|
||||
} MemHeadAligned;
|
||||
static_assert(MEM_MIN_CPP_ALIGNMENT <= alignof(MemHeadAligned), "Bad alignment of MemHeadAligned");
|
||||
static_assert(MEM_MIN_CPP_ALIGNMENT <= sizeof(MemHeadAligned), "Bad size of MemHeadAligned");
|
||||
|
||||
static bool malloc_debug_memset = false;
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user
There should be a comment describing what this means IMO. That would make the static asserts more meaningful too
That is a good point. Tried my best to explain what it is.