Guarded allocator: Ensure alignment and size of MemHead
Ensure that the MemHead and MemHeadAligned are such that memory allocation followed with the head offset keeps the allocation aligned to at least MEM_MIN_CPP_ALIGNMENT. Pull Request: https://projects.blender.org/blender/blender/pulls/120582
This commit is contained in:
committed by
Sergey Sharybin
parent
fac451e513
commit
a4b36cd0d5
@@ -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
|
||||
* 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;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user