61 lines
1.1 KiB
C
61 lines
1.1 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_item_t *list_insert(list_t *list, void *item)
|
|
{
|
|
spinlock_acquire(&list->lock);
|
|
list_item_t *ret = slab_alloc(list_cache);
|
|
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);
|
|
}
|