1 /* (c) 2008, Jack Lange <jarusl@cs.northwestern.edu> */
2 /* (c) 2008, The V3VEE Project <http://www.v3vee.org> */
10 #include <geekos/string.h>
13 #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
16 #define container_of(ptr, type, member) ({ \
17 const typeof( ((type *)0)->member ) *__mptr = (ptr); \
18 (type *)( (char *)__mptr - offsetof(type,member) );})
21 static inline void prefetch(const void *x) {;}
24 * These are non-NULL pointers that will result in page faults
25 * under normal circumstances, used to verify that nobody uses
26 * non-initialized list entries.
28 #define LIST_POISON1 ((void *) 0x00100100)
29 #define LIST_POISON2 ((void *) 0x00200200)
32 * Simple doubly linked list implementation.
34 * Some of the internal functions ("__xxx") are useful when
35 * manipulating whole lists rather than single entries, as
36 * sometimes we already know the next/prev entries and we can
37 * generate better code by using them directly rather than
38 * using the generic single-entry routines.
42 struct list_head *next, *prev;
45 #define LIST_HEAD_INIT(name) { &(name), &(name) }
47 #define LIST_HEAD(name) \
48 struct list_head name = LIST_HEAD_INIT(name)
50 static inline void INIT_LIST_HEAD(struct list_head *list)
57 * Insert a new entry between two known consecutive entries.
59 * This is only for internal list manipulation where we know
60 * the prev/next entries already!
62 static inline void __list_add(struct list_head *new,
63 struct list_head *prev,
64 struct list_head *next)
73 * list_add - add a new entry
74 * @new: new entry to be added
75 * @head: list head to add it after
77 * Insert a new entry after the specified head.
78 * This is good for implementing stacks.
80 static inline void list_add(struct list_head *new, struct list_head *head)
82 __list_add(new, head, head->next);
86 * list_add_tail - add a new entry
87 * @new: new entry to be added
88 * @head: list head to add it before
90 * Insert a new entry before the specified head.
91 * This is useful for implementing queues.
93 static inline void list_add_tail(struct list_head *new, struct list_head *head)
95 __list_add(new, head->prev, head);
99 * Delete a list entry by making the prev/next entries
100 * point to each other.
102 * This is only for internal list manipulation where we know
103 * the prev/next entries already!
105 static inline void __list_del(struct list_head * prev, struct list_head * next)
112 * list_del - deletes entry from list.
113 * @entry: the element to delete from the list.
114 * Note: list_empty on entry does not return true after this, the entry is
115 * in an undefined state.
117 static inline void list_del(struct list_head *entry)
119 __list_del(entry->prev, entry->next);
120 entry->next = LIST_POISON1;
121 entry->prev = LIST_POISON2;
125 * list_del_init - deletes entry from list and reinitialize it.
126 * @entry: the element to delete from the list.
128 static inline void list_del_init(struct list_head *entry)
130 __list_del(entry->prev, entry->next);
131 INIT_LIST_HEAD(entry);
135 * list_move - delete from one list and add as another's head
136 * @list: the entry to move
137 * @head: the head that will precede our entry
139 static inline void list_move(struct list_head *list, struct list_head *head)
141 __list_del(list->prev, list->next);
142 list_add(list, head);
146 * list_move_tail - delete from one list and add as another's tail
147 * @list: the entry to move
148 * @head: the head that will follow our entry
150 static inline void list_move_tail(struct list_head *list,
151 struct list_head *head)
153 __list_del(list->prev, list->next);
154 list_add_tail(list, head);
158 * list_empty - tests whether a list is empty
159 * @head: the list to test.
161 static inline int list_empty(const struct list_head *head)
163 return head->next == head;
167 * list_empty_careful - tests whether a list is
168 * empty _and_ checks that no other CPU might be
169 * in the process of still modifying either member
171 * NOTE: using list_empty_careful() without synchronization
172 * can only be safe if the only activity that can happen
173 * to the list entry is list_del_init(). Eg. it cannot be used
174 * if another CPU could re-list_add() it.
176 * @head: the list to test.
178 static inline int list_empty_careful(const struct list_head *head)
180 struct list_head *next = head->next;
181 return (next == head) && (next == head->prev);
184 static inline void __list_splice(struct list_head *list,
185 struct list_head *head)
187 struct list_head *first = list->next;
188 struct list_head *last = list->prev;
189 struct list_head *at = head->next;
199 * list_splice - join two lists
200 * @list: the new list to add.
201 * @head: the place to add it in the first list.
203 static inline void list_splice(struct list_head *list, struct list_head *head)
205 if (!list_empty(list))
206 __list_splice(list, head);
210 * list_splice_init - join two lists and reinitialise the emptied list.
211 * @list: the new list to add.
212 * @head: the place to add it in the first list.
214 * The list at @list is reinitialised
216 static inline void list_splice_init(struct list_head *list,
217 struct list_head *head)
219 if (!list_empty(list)) {
220 __list_splice(list, head);
221 INIT_LIST_HEAD(list);
226 * list_entry - get the struct for this entry
227 * @ptr: the &struct list_head pointer.
228 * @type: the type of the struct this is embedded in.
229 * @member: the name of the list_struct within the struct.
231 #define list_entry(ptr, type, member) \
232 container_of(ptr, type, member)
235 * list_for_each - iterate over a list
236 * @pos: the &struct list_head to use as a loop counter.
237 * @head: the head for your list.
239 #define list_for_each(pos, head) \
240 for (pos = (head)->next; prefetch(pos->next), pos != (head); \
244 * __list_for_each - iterate over a list
245 * @pos: the &struct list_head to use as a loop counter.
246 * @head: the head for your list.
248 * This variant differs from list_for_each() in that it's the
249 * simplest possible list iteration code, no prefetching is done.
250 * Use this for code that knows the list to be very short (empty
251 * or 1 entry) most of the time.
253 #define __list_for_each(pos, head) \
254 for (pos = (head)->next; pos != (head); pos = pos->next)
257 * list_for_each_prev - iterate over a list backwards
258 * @pos: the &struct list_head to use as a loop counter.
259 * @head: the head for your list.
261 #define list_for_each_prev(pos, head) \
262 for (pos = (head)->prev; prefetch(pos->prev), pos != (head); \
266 * list_for_each_safe - iterate over a list safe against removal of list entry
267 * @pos: the &struct list_head to use as a loop counter.
268 * @n: another &struct list_head to use as temporary storage
269 * @head: the head for your list.
271 #define list_for_each_safe(pos, n, head) \
272 for (pos = (head)->next, n = pos->next; pos != (head); \
273 pos = n, n = pos->next)
276 * list_for_each_entry - iterate over list of given type
277 * @pos: the type * to use as a loop counter.
278 * @head: the head for your list.
279 * @member: the name of the list_struct within the struct.
281 #define list_for_each_entry(pos, head, member) \
282 for (pos = list_entry((head)->next, typeof(*pos), member); \
283 prefetch(pos->member.next), &pos->member != (head); \
284 pos = list_entry(pos->member.next, typeof(*pos), member))
287 * list_for_each_entry_reverse - iterate backwards over list of given type.
288 * @pos: the type * to use as a loop counter.
289 * @head: the head for your list.
290 * @member: the name of the list_struct within the struct.
292 #define list_for_each_entry_reverse(pos, head, member) \
293 for (pos = list_entry((head)->prev, typeof(*pos), member); \
294 prefetch(pos->member.prev), &pos->member != (head); \
295 pos = list_entry(pos->member.prev, typeof(*pos), member))
298 * list_prepare_entry - prepare a pos entry for use as a start point in
299 * list_for_each_entry_continue
300 * @pos: the type * to use as a start point
301 * @head: the head of the list
302 * @member: the name of the list_struct within the struct.
304 #define list_prepare_entry(pos, head, member) \
305 ((pos) ? : list_entry(head, typeof(*pos), member))
308 * list_for_each_entry_continue - iterate over list of given type
309 * continuing after existing point
310 * @pos: the type * to use as a loop counter.
311 * @head: the head for your list.
312 * @member: the name of the list_struct within the struct.
314 #define list_for_each_entry_continue(pos, head, member) \
315 for (pos = list_entry(pos->member.next, typeof(*pos), member); \
316 prefetch(pos->member.next), &pos->member != (head); \
317 pos = list_entry(pos->member.next, typeof(*pos), member))
320 * list_for_each_entry_from - iterate over list of given type
321 * continuing from existing point
322 * @pos: the type * to use as a loop counter.
323 * @head: the head for your list.
324 * @member: the name of the list_struct within the struct.
326 #define list_for_each_entry_from(pos, head, member) \
327 for (; prefetch(pos->member.next), &pos->member != (head); \
328 pos = list_entry(pos->member.next, typeof(*pos), member))
331 * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
332 * @pos: the type * to use as a loop counter.
333 * @n: another type * to use as temporary storage
334 * @head: the head for your list.
335 * @member: the name of the list_struct within the struct.
337 #define list_for_each_entry_safe(pos, n, head, member) \
338 for (pos = list_entry((head)->next, typeof(*pos), member), \
339 n = list_entry(pos->member.next, typeof(*pos), member); \
340 &pos->member != (head); \
341 pos = n, n = list_entry(n->member.next, typeof(*n), member))
344 * list_for_each_entry_safe_continue - iterate over list of given type
345 * continuing after existing point safe against removal of list entry
346 * @pos: the type * to use as a loop counter.
347 * @n: another type * to use as temporary storage
348 * @head: the head for your list.
349 * @member: the name of the list_struct within the struct.
351 #define list_for_each_entry_safe_continue(pos, n, head, member) \
352 for (pos = list_entry(pos->member.next, typeof(*pos), member), \
353 n = list_entry(pos->member.next, typeof(*pos), member); \
354 &pos->member != (head); \
355 pos = n, n = list_entry(n->member.next, typeof(*n), member))
358 * list_for_each_entry_safe_from - iterate over list of given type
359 * from existing point safe against removal of list entry
360 * @pos: the type * to use as a loop counter.
361 * @n: another type * to use as temporary storage
362 * @head: the head for your list.
363 * @member: the name of the list_struct within the struct.
365 #define list_for_each_entry_safe_from(pos, n, head, member) \
366 for (n = list_entry(pos->member.next, typeof(*pos), member); \
367 &pos->member != (head); \
368 pos = n, n = list_entry(n->member.next, typeof(*n), member))
371 * list_for_each_entry_safe_reverse - iterate backwards over list of given type safe against
372 * removal of list entry
373 * @pos: the type * to use as a loop counter.
374 * @n: another type * to use as temporary storage
375 * @head: the head for your list.
376 * @member: the name of the list_struct within the struct.
378 #define list_for_each_entry_safe_reverse(pos, n, head, member) \
379 for (pos = list_entry((head)->prev, typeof(*pos), member), \
380 n = list_entry(pos->member.prev, typeof(*pos), member); \
381 &pos->member != (head); \
382 pos = n, n = list_entry(n->member.prev, typeof(*n), member))
385 * Double linked lists with a single pointer list head.
386 * Mostly useful for hash tables where the two pointer list head is
388 * You lose the ability to access the tail in O(1).
392 struct hlist_node *first;
396 struct hlist_node *next, **pprev;
399 #define HLIST_HEAD_INIT { .first = NULL }
400 #define HLIST_HEAD(name) struct hlist_head name = { .first = NULL }
401 #define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL)
402 static inline void INIT_HLIST_NODE(struct hlist_node *h)
408 static inline int hlist_unhashed(const struct hlist_node *h)
413 static inline int hlist_empty(const struct hlist_head *h)
418 static inline void __hlist_del(struct hlist_node *n)
420 struct hlist_node *next = n->next;
421 struct hlist_node **pprev = n->pprev;
427 static inline void hlist_del(struct hlist_node *n)
430 n->next = LIST_POISON1;
431 n->pprev = LIST_POISON2;
434 static inline void hlist_del_init(struct hlist_node *n)
436 if (!hlist_unhashed(n)) {
442 static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h)
444 struct hlist_node *first = h->first;
447 first->pprev = &n->next;
449 n->pprev = &h->first;
452 /* next must be != NULL */
453 static inline void hlist_add_before(struct hlist_node *n,
454 struct hlist_node *next)
456 n->pprev = next->pprev;
458 next->pprev = &n->next;
462 static inline void hlist_add_after(struct hlist_node *n,
463 struct hlist_node *next)
465 next->next = n->next;
467 next->pprev = &n->next;
470 next->next->pprev = &next->next;
473 #define hlist_entry(ptr, type, member) container_of(ptr,type,member)
475 #define hlist_for_each(pos, head) \
476 for (pos = (head)->first; pos && ({ prefetch(pos->next); 1; }); \
479 #define hlist_for_each_safe(pos, n, head) \
480 for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \
484 * hlist_for_each_entry - iterate over list of given type
485 * @tpos: the type * to use as a loop counter.
486 * @pos: the &struct hlist_node to use as a loop counter.
487 * @head: the head for your list.
488 * @member: the name of the hlist_node within the struct.
490 #define hlist_for_each_entry(tpos, pos, head, member) \
491 for (pos = (head)->first; \
492 pos && ({ prefetch(pos->next); 1;}) && \
493 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
497 * hlist_for_each_entry_continue - iterate over a hlist continuing after existing point
498 * @tpos: the type * to use as a loop counter.
499 * @pos: the &struct hlist_node to use as a loop counter.
500 * @member: the name of the hlist_node within the struct.
502 #define hlist_for_each_entry_continue(tpos, pos, member) \
503 for (pos = (pos)->next; \
504 pos && ({ prefetch(pos->next); 1;}) && \
505 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
509 * hlist_for_each_entry_from - iterate over a hlist continuing from existing point
510 * @tpos: the type * to use as a loop counter.
511 * @pos: the &struct hlist_node to use as a loop counter.
512 * @member: the name of the hlist_node within the struct.
514 #define hlist_for_each_entry_from(tpos, pos, member) \
515 for (; pos && ({ prefetch(pos->next); 1;}) && \
516 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
520 * hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry
521 * @tpos: the type * to use as a loop counter.
522 * @pos: the &struct hlist_node to use as a loop counter.
523 * @n: another &struct hlist_node to use as temporary storage
524 * @head: the head for your list.
525 * @member: the name of the hlist_node within the struct.
527 #define hlist_for_each_entry_safe(tpos, pos, n, head, member) \
528 for (pos = (head)->first; \
529 pos && ({ n = pos->next; 1; }) && \
530 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
535 //#endif // ! __V3VEE__