1
0
mirror of https://gitlab.os-k.eu/os-k-team/os-k.git synced 2023-08-25 14:03:10 +02:00
os-k/include/extras/list.h

285 lines
6.1 KiB
C
Raw Normal View History

2019-01-21 15:00:04 +01:00
//----------------------------------------------------------------------------//
// GNU GPL OS/K //
// //
// Desc: Doubly linked lists implementation //
2019-02-16 23:36:33 +01:00
// //
// //
// Copyright © 2018-2019 The OS/K Team //
// //
// This file is part of OS/K. //
// //
// OS/K is free software: you can redistribute it and/or modify //
// it under the terms of the GNU General Public License as published by //
// the Free Software Foundation, either version 3 of the License, or //
// any later version. //
// //
// OS/K is distributed in the hope that it will be useful, //
// but WITHOUT ANY WARRANTY//without even the implied warranty of //
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
// GNU General Public License for more details. //
// //
// You should have received a copy of the GNU General Public License //
// along with OS/K. If not, see <https://www.gnu.org/licenses/>. //
2019-01-21 15:00:04 +01:00
//----------------------------------------------------------------------------//
#ifndef _KALBASE_H
#include <kalbase.h>
#endif
2019-02-06 14:07:38 +01:00
#ifndef _KALEXTRAS_MALLOC_H
#include <extras/malloc.h>
#endif
2019-01-21 15:00:04 +01:00
2019-02-16 23:36:33 +01:00
#ifndef _KALEXTRAS_LOCKS_H
#include <extras/locks.h>
#endif
2019-01-21 15:00:04 +01:00
#ifndef _KALEXTRAS_LIST_H
#define _KALEXTRAS_LIST_H
2019-02-16 23:36:33 +01:00
#ifdef __cplusplus
extern "C" {
#endif
typedef struct ListHead_t ListHead_t;
typedef struct ListNode_t ListNode_t;
2019-01-21 15:00:04 +01:00
//------------------------------------------//
2019-02-16 23:36:33 +01:00
struct ListHead_t
{
Lock_t *lock;
ulong length;
ListNode_t *first;
ListNode_t *last;
2019-02-16 23:36:33 +01:00
};
2019-01-21 15:00:04 +01:00
2019-02-16 23:36:33 +01:00
struct ListNode_t
{
void *data;
ListHead_t *head;
ListNode_t *prev;
ListNode_t *next;
2019-02-16 23:36:33 +01:00
};
2019-01-21 15:00:04 +01:00
//------------------------------------------//
//
// Create a list head with an extern lock
//
static inline ListHead_t
2019-03-24 14:44:59 +01:00
*ExCreateListHeadWithLock(Lock_t *lock)
2019-01-21 15:00:04 +01:00
{
2019-03-25 17:33:51 +01:00
ListHead_t *head = malloc(sizeof(ListHead_t));
2019-01-21 15:00:04 +01:00
if (head == NULL) return NULL;
head->first = head->last = NULL;
head->length = 0;
head->lock = lock;
return head;
}
//
2019-02-06 14:07:38 +01:00
// Create a list head
2019-01-21 15:00:04 +01:00
//
static inline ListHead_t
2019-03-24 14:44:59 +01:00
*ExCreateListHead(void)
2019-01-21 15:00:04 +01:00
{
2019-03-24 14:44:59 +01:00
return ExCreateListHeadWithLock(NULL);
2019-01-21 15:00:04 +01:00
}
//
// Create a node
//
static inline ListNode_t
2019-03-24 14:44:59 +01:00
*ExCreateNode(void *data)
2019-01-21 15:00:04 +01:00
{
2019-03-25 17:33:51 +01:00
ListNode_t *node = malloc(sizeof(ListNode_t));
2019-01-21 15:00:04 +01:00
if (node == NULL) return NULL;
node->data = data;
node->head = NULL;
node->prev = node->next = NULL;
return node;
}
//
// Prepend node at beginning of list
//
static inline ListHead_t
2019-03-24 14:44:59 +01:00
*ExPrependNode(ListHead_t *head, ListNode_t *node)
2019-01-21 15:00:04 +01:00
{
2019-03-25 17:33:51 +01:00
assert(head && node);
2019-01-21 15:00:04 +01:00
node->head = head;
node->prev = NULL;
if (head->length > 0) {
node->next = head->first;
head->first->prev = node;
head->first = node;
}
else {
head->first = node;
head->last = node;
node->next = NULL;
}
head->length++;
return head;
}
//
// Append node at end of list
//
static inline ListHead_t
2019-03-24 14:44:59 +01:00
*ExAppendNode(ListHead_t *head, ListNode_t *node)
2019-01-21 15:00:04 +01:00
{
2019-03-25 17:33:51 +01:00
assert(head && node);
2019-01-21 15:00:04 +01:00
node->head = head;
node->next = NULL;
if (head->length > 0) {
node->prev = head->last;
head->last->next = node;
head->last = node;
}
else {
head->first = node;
head->last = node;
node->prev = NULL;
}
head->length++;
return head;
}
//
// Insert node2 before node1
//
static inline ListHead_t
2019-03-24 14:44:59 +01:00
*ExAddNodeBefore(ListHead_t *head, ListNode_t *node1, ListNode_t *node2)
2019-01-21 15:00:04 +01:00
{
2019-03-25 17:33:51 +01:00
assert(head && node1 && node2 && node1->head == head);
2019-01-21 15:00:04 +01:00
if (head->first == node1) {
2019-03-24 14:44:59 +01:00
return ExPrependNode(head, node2);
2019-01-21 15:00:04 +01:00
}
node2->head = head;
node2->next = node1;
node2->prev = node1->prev;
// node1->prev does exist
// or node1 would be first
node1->prev->next = node2;
node1->prev = node2;
head->length++;
return head;
}
//
// Insert node2 after node1
//
static inline ListHead_t
2019-03-24 14:44:59 +01:00
*ExAddNodeAfter(ListHead_t *head, ListNode_t *node1, ListNode_t *node2)
2019-01-21 15:00:04 +01:00
{
2019-03-25 17:33:51 +01:00
assert(head && node1 && node2 && node1->head == head);
2019-01-21 15:00:04 +01:00
if (head->last == node1) {
2019-03-24 14:44:59 +01:00
return ExAppendNode(head, node2);
2019-01-21 15:00:04 +01:00
}
node2->head = head;
node2->prev = node1;
node2->next = node1->next;
node1->next->prev = node2;
node1->next = node2;
head->length++;
return head;
}
//
// Remove node of list (and frees it)
//
static inline ListHead_t
2019-03-24 14:44:59 +01:00
*ExRemoveNode(ListHead_t *head, ListNode_t *node)
2019-01-21 15:00:04 +01:00
{
2019-03-25 17:33:51 +01:00
assert(head && node && head->length > 0 && node->head == head);
2019-01-21 15:00:04 +01:00
if (head->length == 1) {
head->first = head->last = NULL;
goto leave;
}
if (head->first == node) {
head->first = node->next;
node->next->prev = NULL;
}
else if (head->last == node) {
head->last = node->prev;
node->prev->next = NULL;
}
else {
node->prev->next = node->next;
node->next->prev = node->prev;
}
leave:
head->length--;
2019-02-16 23:36:33 +01:00
KalFreeMemory(node);
2019-01-21 15:00:04 +01:00
return head;
}
//
// Free a node
//
static inline void
2019-03-24 14:44:59 +01:00
ExDestroyNode(ListNode_t *node)
2019-01-21 15:00:04 +01:00
{
2019-03-25 17:33:51 +01:00
assert(node);
free(node);
2019-01-21 15:00:04 +01:00
}
//
// Free a list head
//
static inline void
2019-03-24 14:44:59 +01:00
ExDestroyListHead(ListHead_t *head)
2019-01-21 15:00:04 +01:00
{
2019-03-25 17:33:51 +01:00
assert(head);
free(head);
2019-01-21 15:00:04 +01:00
}
//
// Access a node's data
//
2019-03-24 14:44:59 +01:00
#define ExGetNodeData(node, type) ((type)(node)->data)
2019-01-21 15:00:04 +01:00
//------------------------------------------//
2019-02-16 23:36:33 +01:00
#ifdef __cplusplus
}
#endif
2019-01-21 15:00:04 +01:00
#endif