Files
nt-lite/code/public/ntlite/ddk/list.h
T

52 lines
1.4 KiB
C

// Public nt-lite DDK intrusive list declarations.
#pragma once
#include <stddef.h>
// Doubly-linked intrusive list node.
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 node.
#define CONTAINING_RECORD(ptr, type, member) \
((type*)((char*)(ptr) - offsetof(type, member)))
// Inserts a node at the tail of a list.
static inline void ListInsertTail(LIST_ENTRY* head, LIST_ENTRY* node) {
node->Bwd = head->Bwd;
node->Fwd = head;
head->Bwd->Fwd = node;
head->Bwd = node;
}
// Inserts a node at the head of a list.
static inline void ListInsertHead(LIST_ENTRY* head, LIST_ENTRY* node) {
node->Fwd = head->Fwd;
node->Bwd = head;
head->Fwd->Bwd = node;
head->Fwd = node;
}
// Removes a node from its current list.
static inline void ListRemove(LIST_ENTRY* node) {
node->Bwd->Fwd = node->Fwd;
node->Fwd->Bwd = node->Bwd;
node->Fwd = node->Bwd = node;
}
// Removes and returns the first node in a list, or NULL when 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;
}