Crinit -- Configurable Rootfs Init
list.h
Go to the documentation of this file.
1 // SPDX-License-Identifier: MIT
14 #ifndef CRINIT_LIST_H
15 #define CRINIT_LIST_H
16 
17 #include <stdlib.h>
18 
26 #define container_of(ptr, type, member) ((type *)((char *)(ptr)-offsetof(type, member)))
27 
31 typedef struct crinitList {
32  struct crinitList *prev;
33  struct crinitList *next;
35 
43 typedef int (*crinitListCmp_t)(crinitList_t *e1, crinitList_t *e2);
44 
51 #define CRINIT_LIST_INIT(list) \
52  { &(list), &(list) }
53 
60 static inline void crinitListInit(crinitList_t *list) {
61  list->next = list;
62  list->prev = list;
63 }
64 
73  entry->next = next;
74  entry->prev = prev;
75  next->prev = entry;
76  prev->next = entry;
77 }
78 
85 static inline void crinitListPrepend(crinitList_t *list, crinitList_t *entry) {
86  crinitListInsert(entry, list, list->next);
87 }
88 
95 static inline void crinitListAppend(crinitList_t *list, crinitList_t *entry) {
96  crinitListInsert(entry, list->prev, list->prev->next);
97 }
98 
106 static inline void crinitListInsertSorted(crinitList_t *list, crinitList_t *entry, crinitListCmp_t entryCmp) {
107  crinitList_t *cur;
108 
109  for (cur = list->next; cur != list; cur = cur->next) {
110  if (entryCmp(cur, entry) >= 0) {
111  crinitListInsert(entry, cur->prev, cur);
112  break;
113  }
114  }
115 
116  if (cur == list) {
117  crinitListAppend(list, entry);
118  }
119 }
120 
132 static inline void crinitListDelete(crinitList_t *entry) {
133  entry->next->prev = entry->prev;
134  entry->prev->next = entry->next;
135  entry->next = NULL;
136  entry->prev = NULL;
137 }
138 
148 static inline int crinitListIsEmpty(const crinitList_t *list) {
149  return list->next == list;
150 }
151 
159 #define crinitListEntry(entry, type, member) container_of(entry, type, member)
160 
168 #define crinitListFirstEntry(list, type, member) crinitListEntry((list)->next, type, member)
169 
177 #define crinitListLastEntry(list, type, member) crinitListEntry((list)->prev, type, member)
178 
185 #define crinitListPrevEntry(entry, member) crinitListEntry((entry)->member.prev, __typeof(*(entry)), member)
186 
193 #define crinitListNextEntry(entry, member) crinitListEntry((entry)->member.next, __typeof(*(entry)), member)
194 
202 #define crinitListEntryIsHead(entry, list, member) (&(entry)->member == (list))
203 
211 #define crinitListForEachEntry(entry, list, member) \
212  for ((entry) = crinitListFirstEntry(list, __typeof(*(entry)), member); \
213  !crinitListEntryIsHead(entry, list, member); (entry) = crinitListNextEntry(entry, member))
214 
226 #define crinitListForEachEntrySafe(entry, temp, list, member) \
227  for ((entry) = crinitListFirstEntry(list, __typeof(*(entry)), member), \
228  (temp) = crinitListNextEntry(entry, member); \
229  !crinitListEntryIsHead(entry, list, member); (entry) = (temp), (temp) = crinitListNextEntry(temp, member))
230 
231 #endif /* CRINIT_LIST_H */
struct crinitList crinitList_t
static void crinitListInsert(crinitList_t *entry, crinitList_t *prev, crinitList_t *next)
Definition: list.h:72
static int crinitListIsEmpty(const crinitList_t *list)
Definition: list.h:148
static void crinitListInsertSorted(crinitList_t *list, crinitList_t *entry, crinitListCmp_t entryCmp)
Definition: list.h:106
static void crinitListDelete(crinitList_t *entry)
Definition: list.h:132
static void crinitListInit(crinitList_t *list)
Definition: list.h:60
int(* crinitListCmp_t)(crinitList_t *e1, crinitList_t *e2)
Definition: list.h:43
static void crinitListAppend(crinitList_t *list, crinitList_t *entry)
Definition: list.h:95
static void crinitListPrepend(crinitList_t *list, crinitList_t *entry)
Definition: list.h:85
Definition: list.h:31
struct crinitList * next
Pointer to next entry in list.
Definition: list.h:33
struct crinitList * prev
Pointer to last entry in list.
Definition: list.h:32