]> glassweightruler.freedombox.rocks Git - Ventoy.git/blob - vtoycli/fat_io_lib/include/fat_list.h
Edited! (#2105)
[Ventoy.git] / vtoycli / fat_io_lib / include / fat_list.h
1 #ifndef __FAT_LIST_H__
2 #define __FAT_LIST_H__
3
4 #ifndef FAT_ASSERT
5 #define FAT_ASSERT(x)
6 #endif
7
8 #ifndef FAT_INLINE
9 #define FAT_INLINE
10 #endif
11
12 //-----------------------------------------------------------------
13 // Types
14 //-----------------------------------------------------------------
15 struct fat_list;
16
17 struct fat_node
18 {
19 struct fat_node *previous;
20 struct fat_node *next;
21 };
22
23 struct fat_list
24 {
25 struct fat_node *head;
26 struct fat_node *tail;
27 };
28
29 //-----------------------------------------------------------------
30 // Macros
31 //-----------------------------------------------------------------
32 #define fat_list_entry(p, t, m) p ? ((t *)((char *)(p)-(char*)(&((t *)0)->m))) : 0
33 #define fat_list_next(l, p) (p)->next
34 #define fat_list_prev(l, p) (p)->previous
35 #define fat_list_first(l) (l)->head
36 #define fat_list_last(l) (l)->tail
37 #define fat_list_for_each(l, p) for ((p) = (l)->head; (p); (p) = (p)->next)
38
39 //-----------------------------------------------------------------
40 // Inline Functions
41 //-----------------------------------------------------------------
42
43 //-----------------------------------------------------------------
44 // fat_list_init:
45 //-----------------------------------------------------------------
46 static FAT_INLINE void fat_list_init(struct fat_list *list)
47 {
48 FAT_ASSERT(list);
49
50 list->head = list->tail = 0;
51 }
52 //-----------------------------------------------------------------
53 // fat_list_remove:
54 //-----------------------------------------------------------------
55 static FAT_INLINE void fat_list_remove(struct fat_list *list, struct fat_node *node)
56 {
57 FAT_ASSERT(list);
58 FAT_ASSERT(node);
59
60 if(!node->previous)
61 list->head = node->next;
62 else
63 node->previous->next = node->next;
64
65 if(!node->next)
66 list->tail = node->previous;
67 else
68 node->next->previous = node->previous;
69 }
70 //-----------------------------------------------------------------
71 // fat_list_insert_after:
72 //-----------------------------------------------------------------
73 static FAT_INLINE void fat_list_insert_after(struct fat_list *list, struct fat_node *node, struct fat_node *new_node)
74 {
75 FAT_ASSERT(list);
76 FAT_ASSERT(node);
77 FAT_ASSERT(new_node);
78
79 new_node->previous = node;
80 new_node->next = node->next;
81 if (!node->next)
82 list->tail = new_node;
83 else
84 node->next->previous = new_node;
85 node->next = new_node;
86 }
87 //-----------------------------------------------------------------
88 // fat_list_insert_before:
89 //-----------------------------------------------------------------
90 static FAT_INLINE void fat_list_insert_before(struct fat_list *list, struct fat_node *node, struct fat_node *new_node)
91 {
92 FAT_ASSERT(list);
93 FAT_ASSERT(node);
94 FAT_ASSERT(new_node);
95
96 new_node->previous = node->previous;
97 new_node->next = node;
98 if (!node->previous)
99 list->head = new_node;
100 else
101 node->previous->next = new_node;
102 node->previous = new_node;
103 }
104 //-----------------------------------------------------------------
105 // fat_list_insert_first:
106 //-----------------------------------------------------------------
107 static FAT_INLINE void fat_list_insert_first(struct fat_list *list, struct fat_node *node)
108 {
109 FAT_ASSERT(list);
110 FAT_ASSERT(node);
111
112 if (!list->head)
113 {
114 list->head = node;
115 list->tail = node;
116 node->previous = 0;
117 node->next = 0;
118 }
119 else
120 fat_list_insert_before(list, list->head, node);
121 }
122 //-----------------------------------------------------------------
123 // fat_list_insert_last:
124 //-----------------------------------------------------------------
125 static FAT_INLINE void fat_list_insert_last(struct fat_list *list, struct fat_node *node)
126 {
127 FAT_ASSERT(list);
128 FAT_ASSERT(node);
129
130 if (!list->tail)
131 fat_list_insert_first(list, node);
132 else
133 fat_list_insert_after(list, list->tail, node);
134 }
135 //-----------------------------------------------------------------
136 // fat_list_is_empty:
137 //-----------------------------------------------------------------
138 static FAT_INLINE int fat_list_is_empty(struct fat_list *list)
139 {
140 FAT_ASSERT(list);
141
142 return !list->head;
143 }
144 //-----------------------------------------------------------------
145 // fat_list_pop_head:
146 //-----------------------------------------------------------------
147 static FAT_INLINE struct fat_node * fat_list_pop_head(struct fat_list *list)
148 {
149 struct fat_node * node;
150
151 FAT_ASSERT(list);
152
153 node = fat_list_first(list);
154 if (node)
155 fat_list_remove(list, node);
156
157 return node;
158 }
159
160 #endif
161