mem: update slab allocator

the slab allocator will now free slabs if they are not the first slab
for a given size and they are empty
This commit is contained in:
Michael Shaw 2023-04-20 19:22:07 -04:00
commit 794c328809
2 changed files with 26 additions and 6 deletions

View file

@ -11,6 +11,7 @@ struct slab {
size_t free; /* number of free objects */
uintptr_t *nextfree;
struct slab *next;
struct slab *prev;
};
struct kmap_entry {

View file

@ -38,6 +38,9 @@ struct slab *slab_create(size_t size)
ret->nextfree = (uintptr_t *)((uintptr_t)ret + start);
ret->size = size;
ret->num = (asize - start) / size;
ret->next = NULL;
ret->prev = NULL;
uintptr_t *ptr = ret->nextfree;
while (ptr != NULL) {
@ -61,6 +64,7 @@ void *slab_alloc(struct slab *slab)
if (slab->nextfree == NULL) {
if (slab->next == NULL) {
slab->next = slab_create(slab->size);
slab->next->prev = slab;
if (slab->next == NULL)
return NULL;
}
@ -81,17 +85,32 @@ void slab_free(struct slab *slab, void *ptr)
if (ptr == NULL)
return;
uintptr_t low;
uintptr_t high;
slab_range(slab, &low, &high);
uintptr_t low = 0;
uintptr_t high = 0;
if ((uintptr_t)ptr < low || (uintptr_t)ptr > high) {
slab_free(slab->next, ptr);
return;
slab_range(slab, &low, &high);
while ((uintptr_t)ptr < low || (uintptr_t)ptr > high) {
slab_range(slab, &low, &high);
slab = slab->next;
if (slab == NULL)
return;
}
*(uintptr_t *)ptr = (uintptr_t)slab->nextfree;
slab->nextfree = ptr;
slab->free++;
/* delete the slab if it's empty and not a root slab */
if (slab->free == slab->num && slab->prev != NULL) {
struct slab *next = slab->next;
struct slab *prev = slab->prev;
prev->next = next;
if (next != NULL)
next->prev = prev;
buddy_free(slab);
}
}
void kmalloc_init()