vbos/lib/list.c
Michael Shaw 071a5651e0
list: change locking discipline
Signed-off-by: Michael Shaw <m@michaelsshaw.com>
2026-08-12 14:50:47 -04:00

81 lines
1.3 KiB
C

/* SPDX-License-Identifier: GPL-2.0-only */
#include <kernel/spinlock.h>
#include <kernel/types.h>
#include <kernel/slab.h>
#include <lib/list.h>
static mem_cache_t *list_cache;
void list_create(list_t *list)
{
list->head = NULL;
list->tail = NULL;
list->lock = 0;
}
void list_destroy(list_t *list)
{
list_item_t *item = list->head;
while (item) {
list_item_t *next = item->next;
slab_free(list_cache, item);
item = next;
}
list->head = NULL;
list->tail = NULL;
}
list_item_t *list_insert(list_t *list, void *item)
{
list_item_t *ret = slab_alloc(list_cache);
if (!ret)
return NULL;
ret->ptr = item;
ret->list = list;
ret->next = NULL;
ret->prev = NULL;
if (!list->head)
list->head = ret;
if (list->tail) {
list->tail->next = ret;
ret->prev = list->tail;
}
list->tail = ret;
return ret;
}
void list_remove(list_item_t *item)
{
list_t *list = item->list;
if (item == list->head)
list->head = item->next;
if (item == list->tail)
list->tail = item->prev;
if (item->next)
item->next->prev = item->prev;
if (item->prev)
item->prev->next = item->next;
slab_free(list_cache, item);
}
void list_lock(list_t *list)
{
spinlock_acquire(&list->lock);
}
void list_unlock(list_t *list)
{
spinlock_release(&list->lock);
}
void list_init()
{
list_cache = slab_create("list_cache", sizeof(list_item_t), 64 * KB, 0x8);
}