1
0
mirror of https://github.com/ThrowTheSwitch/Unity.git synced 2026-01-23 00:15:58 +01:00

Fix malloc alignment

Make sure the malloc alignment is always aligned to the architecture's pointer size
This commit is contained in:
Alessio Centazzo
2019-09-29 15:08:48 -07:00
parent 46263fc148
commit fc14856321

View File

@@ -188,13 +188,26 @@ typedef struct GuardBytes
} Guard; } Guard;
#define UNITY_MALLOC_ALIGNMENT (UNITY_POINTER_WIDTH / 8)
static const char end[] = "END"; static const char end[] = "END";
static size_t unity_size_round_up(size_t size)
{
size_t rounded_size;
rounded_size = ((size + UNITY_MALLOC_ALIGNMENT - 1) / UNITY_MALLOC_ALIGNMENT) * UNITY_MALLOC_ALIGNMENT;
return rounded_size;
}
void* unity_malloc(size_t size) void* unity_malloc(size_t size)
{ {
char* mem; char* mem;
Guard* guard; Guard* guard;
size_t total_size = size + sizeof(Guard) + sizeof(end); size_t total_size;
total_size = sizeof(Guard) + unity_size_round_up(size + sizeof(end));
if (malloc_fail_countdown != MALLOC_DONT_FAIL) if (malloc_fail_countdown != MALLOC_DONT_FAIL)
{ {
@@ -243,9 +256,13 @@ static void release_memory(void* mem)
malloc_count--; malloc_count--;
#ifdef UNITY_EXCLUDE_STDLIB_MALLOC #ifdef UNITY_EXCLUDE_STDLIB_MALLOC
if (mem == unity_heap + heap_index - guard->size - sizeof(end)) size_t block_size;
block_size = unity_size_round_up(guard->size + sizeof(end));
if (mem == unity_heap + heap_index - block_size)
{ {
heap_index -= (guard->size + sizeof(Guard) + sizeof(end)); heap_index -= (sizeof(Guard) + block_size);
} }
#else #else
UNITY_FIXTURE_FREE(guard); UNITY_FIXTURE_FREE(guard);