LibC: Make malloc actually allign to s_malloc_default_align boundary

This commit is contained in:
Bananymous 2025-06-28 15:46:41 +03:00
parent f73bb242f3
commit f26a445ce6
1 changed files with 5 additions and 0 deletions

View File

@ -138,6 +138,8 @@ static void shrink_node_if_needed(malloc_pool_t& pool, malloc_node_t* node, size
uint8_t* node_end = (uint8_t*)node->next(); uint8_t* node_end = (uint8_t*)node->next();
node->size = sizeof(malloc_node_t) + size; node->size = sizeof(malloc_node_t) + size;
if (auto rem = (node->size + sizeof(malloc_node_t)) % s_malloc_default_align)
node->size += s_malloc_default_align - rem;
auto* next = node->next(); auto* next = node->next();
next->allocated = false; next->allocated = false;
@ -176,6 +178,8 @@ static void* allocate_from_pool(size_t pool_index, size_t size)
remove_node_from_pool_free_list(pool, node); remove_node_from_pool_free_list(pool, node);
shrink_node_if_needed(pool, node, size); shrink_node_if_needed(pool, node, size);
assert(((uintptr_t)node->data & (s_malloc_default_align - 1)) == 0);
return node->data; return node->data;
} }
@ -390,6 +394,7 @@ int posix_memalign(void** memptr, size_t alignment, size_t size)
pthread_mutex_unlock(&s_malloc_mutex); pthread_mutex_unlock(&s_malloc_mutex);
assert(((uintptr_t)node->data & (alignment - 1)) == 0);
*memptr = node->data; *memptr = node->data;
return 0; return 0;
} }