45 lines
1.4 KiB
C
45 lines
1.4 KiB
C
#pragma once
|
|
// ke_list.h - intrusive doubly-linked list
|
|
// Used throughout the kernel for run queues, wait lists, thread lists, etc.
|
|
#include <stddef.h>
|
|
|
|
typedef struct LIST_ENTRY {
|
|
struct LIST_ENTRY* Fwd;
|
|
struct LIST_ENTRY* Bwd;
|
|
} LIST_ENTRY;
|
|
|
|
#define LIST_INIT(head) ((head)->Fwd = (head)->Bwd = (head))
|
|
#define LIST_IS_EMPTY(head) ((head)->Fwd == (head))
|
|
|
|
// Recover the containing struct pointer from an embedded LIST_ENTRY.
|
|
#define CONTAINING_RECORD(ptr, type, member) \
|
|
((type *)((char *)(ptr) - offsetof(type, member)))
|
|
|
|
static inline void ListInsertTail(LIST_ENTRY* head, LIST_ENTRY* node) {
|
|
node->Bwd = head->Bwd;
|
|
node->Fwd = head;
|
|
head->Bwd->Fwd = node;
|
|
head->Bwd = node;
|
|
}
|
|
|
|
static inline void ListInsertHead(LIST_ENTRY* head, LIST_ENTRY* node) {
|
|
node->Fwd = head->Fwd;
|
|
node->Bwd = head;
|
|
head->Fwd->Bwd = node;
|
|
head->Fwd = node;
|
|
}
|
|
|
|
static inline void ListRemove(LIST_ENTRY* node) {
|
|
node->Bwd->Fwd = node->Fwd;
|
|
node->Fwd->Bwd = node->Bwd;
|
|
node->Fwd = node->Bwd = node; /* self-loop: poison against double-remove */
|
|
}
|
|
|
|
// Pop the first entry off the list, or NULL if empty.
|
|
static inline LIST_ENTRY* ListPopHead(LIST_ENTRY* head) {
|
|
if (LIST_IS_EMPTY(head)) return (LIST_ENTRY *)0;
|
|
LIST_ENTRY* node = head->Fwd;
|
|
ListRemove(node);
|
|
return node;
|
|
}
|