From b5dc88c96620706b50c9c8679a46d88620c630e0 Mon Sep 17 00:00:00 2001 From: Felix Kopp Date: Wed, 2 Dec 2020 00:00:05 +0100 Subject: [PATCH] malloc: make malloc/free atomic --- lib/malloc.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/lib/malloc.c b/lib/malloc.c index 9121442..9cdecd9 100644 --- a/lib/malloc.c +++ b/lib/malloc.c @@ -156,13 +156,17 @@ void *malloc(size_t size) size = (size / MIN_BLKSZ) * MIN_BLKSZ; size += MIN_BLKSZ; + atomic_enter(); + list_for_each_entry(&memblk_free_list, blk, list) { /* blocks are sorted by size */ if (blk->size >= size) break; } - if (blk->size < size) + if (blk->size < size) { + atomic_leave(); return NULL; /* TODO: set errno to ENOMEM once we have it */ + } /* * If we've made it to here, we have found a sufficiently big block, @@ -179,6 +183,8 @@ void *malloc(size_t size) list_delete(&blk->list); + atomic_leave(); + /* Keep the size field intact */ return ((void *)blk) + MEMBLK_SIZE_LENGTH; } @@ -214,6 +220,8 @@ void free(void *ptr) if ((blk->size & 0x1u) == 0) return; /* TODO: Raise exception on double-free */ + atomic_enter(); + memblk_set_size(blk, without_lsb(blk->size)); /* check if our higher/right neighbor is allocated and merge if it is not */ @@ -238,6 +246,8 @@ void free(void *ptr) break; } list_insert_before(&tmp->list, &blk->list); + + atomic_leave(); } /*