LibC: page align mmap backed malloc sizes

This will make reallocations cleaner
This commit is contained in:
2026-07-20 06:58:30 +03:00
parent bd5be982e9
commit 8c6e402ee8
+9 -2
View File
@@ -229,7 +229,11 @@ void* malloc(size_t total_size)
{
if (total_size >= s_mmap_threshold)
{
const size_t mmap_size = sizeof(MmapAllocationHeader) + total_size;
const size_t page_size = getpagesize();
size_t mmap_size = sizeof(MmapAllocationHeader) + total_size;
if (const auto rem = mmap_size % page_size)
mmap_size += page_size - rem;
void* address = mmap(nullptr, mmap_size, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
if (address == MAP_FAILED)
@@ -399,7 +403,11 @@ void* aligned_alloc(size_t alignment, size_t size)
static_assert(sizeof(MmapAllocationHeader) <= alignof(max_align_t));
const size_t page_size = getpagesize();
size_t mmap_size = alignment + size;
if (const auto rem = mmap_size % page_size)
mmap_size += page_size - rem;
void* addr_ptr = mmap(nullptr, mmap_size, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
if (addr_ptr == MAP_FAILED)
@@ -411,7 +419,6 @@ void* aligned_alloc(size_t alignment, size_t size)
if (const auto rem = data % alignment)
data += alignment - rem;
const size_t page_size = getpagesize();
if ((data - sizeof(MmapAllocationHeader)) - addr >= page_size)
{
const size_t pages_to_free = ((data - sizeof(MmapAllocationHeader)) - addr) / page_size;