vbos/lib/list.c
Michael Shaw eb41b17fea vfs: fix various logical errors in the vfs
Signed-off-by: Michael Shaw <m@michaelsshaw.com>
2026-07-05 15:18:34 +02:00

81 lines
1.4 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)
{
spinlock_acquire(&list->lock);
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;
spinlock_release(&list->lock);
}
list_item_t *list_insert(list_t *list, void *item)
{
spinlock_acquire(&list->lock);
list_item_t *ret = slab_alloc(list_cache);
if (!ret) {
spinlock_release(&list->lock);
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;
spinlock_release(&list->lock);
return ret;
}
void list_remove(list_item_t *item)
{
list_t *list = item->list;
spinlock_acquire(&list->lock);
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;
spinlock_release(&list->lock);
slab_free(list_cache, item);
}
void list_init()
{
list_cache = slab_create("list_cache", sizeof(list_item_t), 64 * KB, 0x8);
}