From 49d941ad65b4b4b23bed807eb6d5e0904822e3f8 Mon Sep 17 00:00:00 2001 From: Bananymous Date: Thu, 28 Sep 2023 21:03:43 +0300 Subject: [PATCH] LibC: Fix a bug in malloc You could not allocate with size equal to one of the pool sizes. --- libc/malloc.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/libc/malloc.cpp b/libc/malloc.cpp index 5c95caff4..c1ac4eb63 100644 --- a/libc/malloc.cpp +++ b/libc/malloc.cpp @@ -123,7 +123,7 @@ void* malloc(size_t size) // find the first pool with size atleast size size_t first_usable_pool = 0; - while (s_malloc_pools[first_usable_pool].size < size) + while (s_malloc_pools[first_usable_pool].size - sizeof(malloc_node_t) < size) first_usable_pool++; // first_usable_pool = ceil(log(size/s_malloc_smallest_pool, s_malloc_pool_size_mult)) @@ -140,6 +140,7 @@ void* malloc(size_t size) continue; if (!allocate_pool(i)) break; + // NOTE: always works since we just created the pool return allocate_from_pool(i, size); }