Skip to content

Commit

Permalink
zmalloc: also add alloc at begin of first fit to test differences in
Browse files Browse the repository at this point in the history
speed and memory fragmentation. alloc at end is faster during loading.
no data yet on memory fragmentation.
  • Loading branch information
ivop committed Sep 10, 2024
1 parent f2851bd commit 3a6c721
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion lib/zmalloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,20 @@ void *zmalloc(size_t size) {
for (p = base; p && !(p->free && p->size >= size); p = p->next) ; // find
if (!p) return NULL;

if (p->size - size > block_info_size) { // split, alloc at end
if (p->size - size > block_info_size) {
#if 0
// split, alloc at begin
struct block_info *q = (void *) p + block_info_size + size;
q->next = p->next;
p->next = q;
q->prev = p;
if (q->next) q->next->prev = q;
q->size = p->size - block_info_size - size;
p->size = size;
q->free = 1;
#endif
#if 1
// split, alloc at end
struct block_info *q = (void *) p + p->size - size;
p->size -= size + block_info_size;
q->size = size;
Expand All @@ -54,6 +67,7 @@ void *zmalloc(size_t size) {
q->prev = p;
if (q->next) q->next->prev = q;
p = q;
#endif
}

p->free = 0;
Expand Down

0 comments on commit 3a6c721

Please sign in to comment.