diff options
author | Anton Kling <anton@kling.gg> | 2024-07-08 20:08:40 +0200 |
---|---|---|
committer | Anton Kling <anton@kling.gg> | 2024-07-08 20:08:40 +0200 |
commit | 35292a486c2b44862cac6887441d6fa18148b249 (patch) | |
tree | c3a60d5ea9758512ff19e46e3843b986159b54bc | |
parent | 3289c4434c608e85b8e03f097f6639f4fba96bd1 (diff) |
LibC: Optimize malloc
-rw-r--r-- | userland/libc/malloc/malloc.c | 19 |
1 files changed, 11 insertions, 8 deletions
diff --git a/userland/libc/malloc/malloc.c b/userland/libc/malloc/malloc.c index 19fc91e..e368edd 100644 --- a/userland/libc/malloc/malloc.c +++ b/userland/libc/malloc/malloc.c @@ -111,6 +111,7 @@ MallocHeader *next_close_header(MallocHeader *a) { return next_header(a); } +int merge_headers(MallocHeader *b); MallocHeader *find_free_entry(u32 s) { // A new header is required as well as the newly allocated chunk s += sizeof(MallocHeader); @@ -125,6 +126,11 @@ MallocHeader *find_free_entry(u32 s) { } u64 required_size = s; if (p->size < required_size) { + for (; merge_headers(p);) + ; + if (p->size >= required_size) { + return p; + } continue; } return p; @@ -132,30 +138,27 @@ MallocHeader *find_free_entry(u32 s) { return NULL; } -void merge_headers(MallocHeader *b) { +int merge_headers(MallocHeader *b) { if (!(b->flags & IS_FREE)) { - return; + return 0; } MallocHeader *n = next_close_header(b); if (!n) { - return; + return 0; } if (!(n->flags & IS_FREE)) { - return; + return 0; } b->size += n->size; b->flags |= n->flags & IS_FINAL; b->n = n->n; - assert(b->magic == 0xdde51ab9410268b1); - if (b->n) { - assert(b->n->magic == 0xdde51ab9410268b1); - } if (n == final) { final = b; } + return 1; } void *int_malloc(size_t s, int recursion) { |