74 lines
2.7 KiB
C
74 lines
2.7 KiB
C
#ifndef INCLUDED_C_LIST_H
|
|
#define INCLUDED_C_LIST_H
|
|
|
|
#ifndef INCLUDED_C_BASE_H
|
|
#include <c_Base.h>
|
|
#endif /*INCLUDED_C_BASE_H*/
|
|
|
|
|
|
/* ------------------------------------------------------------------------------------------------------------------ */
|
|
/* */
|
|
|
|
typedef struct c_ListNode_t {
|
|
struct c_ListNode_t* prev;
|
|
struct c_ListNode_t* next;
|
|
}c_ListNode_t;
|
|
|
|
typedef c_ListNode_t c_List_t;
|
|
|
|
/* ------------------------------------------------------------------------------------------------------------------ */
|
|
/* */
|
|
|
|
#define c_List_Prev(n) (n)->prev
|
|
#define c_List_Next(n) (n)->next
|
|
#define c_List_PrevNext(n) c_List_Next(c_List_Prev(n))
|
|
#define c_List_NextPrev(n) c_List_Prev(c_List_Next(n))
|
|
|
|
/* ------------------------------------------------------------------------------------------------------------------ */
|
|
/* */
|
|
|
|
|
|
#define c_List_Init(n) do{ \
|
|
c_List_Prev(n) = c_List_Next(n) = (n); \
|
|
}while(0)
|
|
|
|
#define c_List_IsEmpty(n) ((c_List_Next(n) == (n)) && (c_List_Prev(n) == (n)))
|
|
|
|
#define c_List_InsertBefore(L, N) do { \
|
|
c_List_Next(N) = (L); \
|
|
c_List_Prev(N) = c_List_Prev(L); \
|
|
c_List_PrevNext(N) = (N); \
|
|
c_List_Prev(L) = (N); \
|
|
} while(0)
|
|
|
|
#define c_List_InsertAfter(L, N) do { \
|
|
c_List_Prev(N) = (L); \
|
|
c_List_Next(N) = c_List_Next(L); \
|
|
c_List_NextPrev(N) = (N); \
|
|
c_List_Next(L) = (N); \
|
|
} while(0)
|
|
|
|
#define c_List_Remove(n) do { \
|
|
c_List_PrevNext(n) = c_List_Next(n); \
|
|
c_List_NextPrev(n) = c_List_Prev(n); \
|
|
c_List_Init(n); \
|
|
} while(0)
|
|
|
|
#define c_List_Entry(ptr, type, member) \
|
|
((type *)((char *)(ptr) - offsetof(type, member)))
|
|
|
|
// Standard forward loop: Safe for lookups, UNSAFE for deletions
|
|
#define c_List_ForEachEntry(head, pos, type, member) \
|
|
for (pos = c_List_Entry(c_List_Next(head), type, member); \
|
|
&pos->member != (head); \
|
|
pos = c_List_Entry(c_List_Next(&pos->member), type, member))
|
|
|
|
// Safe forward loop: Explicitly safe to call c_List_Remove inside the loop body
|
|
#define c_List_ForEachEntrySafe(head, pos, n, type, member) \
|
|
for (pos = c_List_Entry(c_List_Next(head), type, member), \
|
|
n = c_List_Entry(c_List_Next(&pos->member), type, member); \
|
|
&pos->member != (head); \
|
|
pos = n, n = c_List_Entry(c_List_Next(&n->member), type, member))
|
|
|
|
#endif /*INCLUDED_C_LIST_H*/
|