slab: fix linked list

This commit is contained in:
Matheus
2023-07-03 22:44:26 -03:00
parent de3d23948b
commit 00cce59b1a
+25 -17
View File
@@ -70,6 +70,9 @@ static bool growcache(scache_t *cache) {
slab->next = cache->empty; slab->next = cache->empty;
slab->prev = NULL;
if (slab->next)
slab->next->prev = slab;
cache->empty = slab; cache->empty = slab;
return true; return true;
} }
@@ -159,25 +162,24 @@ void *slab_allocate(scache_t *cache) {
if (slab == cache->empty) { if (slab == cache->empty) {
cache->empty = slab->next; cache->empty = slab->next;
if (slab->prev)
slab->prev->next = slab->next;
if (slab->next) if (slab->next)
slab->next->prev = slab->prev; slab->next->prev = NULL;
if (cache->partial) if (cache->partial)
cache->partial->prev = slab; cache->partial->prev = slab;
slab->next = cache->partial; slab->next = cache->partial;
slab->prev = NULL; slab->prev = NULL;
cache->partial = slab; cache->partial = slab;
} } else if (slab->used == cache->slabobjcount) {
if (slab->used == cache->slabobjcount) {
cache->partial = slab->next; cache->partial = slab->next;
if (slab->prev)
slab->prev->next = slab->next;
if (slab->next) if (slab->next)
slab->next->prev = slab->prev; slab->next->prev = NULL;
if (cache->full) if (cache->full)
cache->full->prev = slab; cache->full->prev = slab;
slab->next = cache->full; slab->next = cache->full;
slab->prev = NULL; slab->prev = NULL;
cache->full = slab; cache->full = slab;
@@ -195,28 +197,34 @@ void slab_free(scache_t *cache, void *addr) {
__assert(slab); __assert(slab);
if (slab->used == 0) { if (slab->used == 0) {
cache->partial = slab->next; if (slab->prev == NULL)
if (slab->prev) cache->partial = slab->next;
else
slab->prev->next = slab->next; slab->prev->next = slab->next;
if (slab->next) if (slab->next)
slab->next->prev = slab->prev; slab->next->prev = slab->prev;
if (cache->empty)
cache->empty->prev = slab;
slab->next = cache->empty; slab->next = cache->empty;
slab->prev = NULL; slab->prev = NULL;
if (slab->next)
slab->next->prev = slab;
cache->empty = slab; cache->empty = slab;
} }
if (slab->used == cache->slabobjcount - 1) { if (slab->used == cache->slabobjcount - 1) {
cache->full = slab->next; if (slab->prev == NULL)
if (slab->prev) cache->full = slab->next;
else
slab->prev->next = slab->next; slab->prev->next = slab->next;
if (slab->next) if (slab->next)
slab->next->prev = slab->prev; slab->next->prev = slab->prev;
if (cache->partial)
cache->partial->prev = slab;
slab->next = cache->partial; slab->next = cache->partial;
slab->prev = NULL; slab->prev = NULL;
if (slab->next)
slab->next->prev = slab;
cache->partial = slab; cache->partial = slab;
} }
spinlock_release(&cache->lock); spinlock_release(&cache->lock);