Guarded allocator: Ensure alignment and size of MemHead #120582

Merged
Sergey Sharybin merged 3 commits from Sergey/blender:guardedalloc_ensure_memhead_alignment into main 2024-04-12 17:22:57 +02:00
3 changed files with 13 additions and 0 deletions

View File

@ -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

There should be a comment describing what this means IMO. That would make the static asserts more meaningful too

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.

That is a good point. Tried my best to explain what it is.
* functions. For alignments smaller than this value, using non-aligned versions of allocator API

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.

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.`

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

View File

@ -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;

View File

@ -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;