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:
parent
d7ed4bd777
commit
794c328809
2 changed files with 26 additions and 6 deletions
|
|
@ -11,6 +11,7 @@ struct slab {
|
||||||
size_t free; /* number of free objects */
|
size_t free; /* number of free objects */
|
||||||
uintptr_t *nextfree;
|
uintptr_t *nextfree;
|
||||||
struct slab *next;
|
struct slab *next;
|
||||||
|
struct slab *prev;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct kmap_entry {
|
struct kmap_entry {
|
||||||
|
|
|
||||||
31
mem/slab.c
31
mem/slab.c
|
|
@ -38,6 +38,9 @@ struct slab *slab_create(size_t size)
|
||||||
|
|
||||||
ret->nextfree = (uintptr_t *)((uintptr_t)ret + start);
|
ret->nextfree = (uintptr_t *)((uintptr_t)ret + start);
|
||||||
ret->size = size;
|
ret->size = size;
|
||||||
|
ret->num = (asize - start) / size;
|
||||||
|
ret->next = NULL;
|
||||||
|
ret->prev = NULL;
|
||||||
|
|
||||||
uintptr_t *ptr = ret->nextfree;
|
uintptr_t *ptr = ret->nextfree;
|
||||||
while (ptr != NULL) {
|
while (ptr != NULL) {
|
||||||
|
|
@ -61,6 +64,7 @@ void *slab_alloc(struct slab *slab)
|
||||||
if (slab->nextfree == NULL) {
|
if (slab->nextfree == NULL) {
|
||||||
if (slab->next == NULL) {
|
if (slab->next == NULL) {
|
||||||
slab->next = slab_create(slab->size);
|
slab->next = slab_create(slab->size);
|
||||||
|
slab->next->prev = slab;
|
||||||
if (slab->next == NULL)
|
if (slab->next == NULL)
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
@ -81,17 +85,32 @@ void slab_free(struct slab *slab, void *ptr)
|
||||||
if (ptr == NULL)
|
if (ptr == NULL)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
uintptr_t low;
|
uintptr_t low = 0;
|
||||||
uintptr_t high;
|
uintptr_t high = 0;
|
||||||
slab_range(slab, &low, &high);
|
|
||||||
|
|
||||||
if ((uintptr_t)ptr < low || (uintptr_t)ptr > high) {
|
slab_range(slab, &low, &high);
|
||||||
slab_free(slab->next, ptr);
|
while ((uintptr_t)ptr < low || (uintptr_t)ptr > high) {
|
||||||
return;
|
slab_range(slab, &low, &high);
|
||||||
|
slab = slab->next;
|
||||||
|
if (slab == NULL)
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
*(uintptr_t *)ptr = (uintptr_t)slab->nextfree;
|
*(uintptr_t *)ptr = (uintptr_t)slab->nextfree;
|
||||||
slab->nextfree = ptr;
|
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()
|
void kmalloc_init()
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue