malloc: make malloc/free atomic

This commit is contained in:
Felix Kopp 2020-12-02 00:00:05 +01:00
parent e98382dde2
commit b5dc88c966
No known key found for this signature in database
GPG key ID: C478BA0A85F75728

View file

@ -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();
}
/*