Updated spec
[elliptics.git] / library / list.h
blobd068d5bfa2814bde37659f4ba531e1856e479b99
1 #ifndef _DNET_LIST_H
2 #define _DNET_LIST_H
4 #ifdef __cplusplus
5 extern "C" {
6 #endif
8 static inline void prefetch(const void *x __attribute__ ((unused))) {;}
9 #define LIST_POISON1 ((void *) 0x00100100)
10 #define LIST_POISON2 ((void *) 0x00200200)
12 /**
13 * container_of - cast a member of a structure out to the containing structure
14 * @ptr: the pointer to the member.
15 * @type: the type of the container struct this is embedded in.
16 * @member: the name of the member within the struct.
19 #define container_of(ptr, type, member) ({ \
20 const typeof( ((type *)0)->member ) *__mptr = (ptr); \
21 (type *)( (char *)__mptr - offsetof(type,member) );})
24 * Simple doubly linked list implementation.
26 * Some of the internal functions ("__xxx") are useful when
27 * manipulating whole lists rather than single entries, as
28 * sometimes we already know the next/prev entries and we can
29 * generate better code by using them directly rather than
30 * using the generic single-entry routines.
33 struct list_head {
34 struct list_head *next, *prev;
37 #define LIST_HEAD_INIT(name) { &(name), &(name) }
39 #define LIST_HEAD(name) \
40 struct list_head name = LIST_HEAD_INIT(name)
42 static inline void INIT_LIST_HEAD(struct list_head *list)
44 list->next = list;
45 list->prev = list;
49 * Insert a new entry between two known consecutive entries.
51 * This is only for internal list manipulation where we know
52 * the prev/next entries already!
54 #ifndef CONFIG_DEBUG_LIST
55 static inline void __list_add(struct list_head *newp,
56 struct list_head *prev,
57 struct list_head *next)
59 next->prev = newp;
60 newp->next = next;
61 newp->prev = prev;
62 prev->next = newp;
64 #else
65 extern void __list_add(struct list_head *newp,
66 struct list_head *prev,
67 struct list_head *next);
68 #endif
70 /**
71 * list_add - add a new entry
72 * @newp: new entry to be added
73 * @head: list head to add it after
75 * Insert a new entry after the specified head.
76 * This is good for implementing stacks.
78 #ifndef CONFIG_DEBUG_LIST
79 static inline void list_add(struct list_head *newp, struct list_head *head)
81 __list_add(newp, head, head->next);
83 #else
84 extern void list_add(struct list_head *newp, struct list_head *head);
85 #endif
88 /**
89 * list_add_tail - add a new entry
90 * @newp: new entry to be added
91 * @head: list head to add it before
93 * Insert a new entry before the specified head.
94 * This is useful for implementing queues.
96 static inline void list_add_tail(struct list_head *newp, struct list_head *head)
98 __list_add(newp, head->prev, head);
102 * Delete a list entry by making the prev/next entries
103 * point to each other.
105 * This is only for internal list manipulation where we know
106 * the prev/next entries already!
108 static inline void __list_del(struct list_head * prev, struct list_head * next)
110 next->prev = prev;
111 prev->next = next;
115 * list_del - deletes entry from list.
116 * @entry: the element to delete from the list.
117 * Note: list_empty on entry does not return true after this, the entry is
118 * in an undefined state.
120 #ifndef CONFIG_DEBUG_LIST
121 static inline void list_del(struct list_head *entry)
123 __list_del(entry->prev, entry->next);
124 entry->next = (struct list_head *)LIST_POISON1;
125 entry->prev = (struct list_head *)LIST_POISON2;
127 #else
128 extern void list_del(struct list_head *entry);
129 #endif
132 * list_replace - replace old entry by new one
133 * @oldp : the element to be replaced
134 * @newp : the new element to insert
135 * Note: if 'old' was empty, it will be overwritten.
137 static inline void list_replace(struct list_head *oldp,
138 struct list_head *newp)
140 newp->next = oldp->next;
141 newp->next->prev = newp;
142 newp->prev = oldp->prev;
143 newp->prev->next = newp;
146 static inline void list_replace_init(struct list_head *oldp,
147 struct list_head *newp)
149 list_replace(oldp, newp);
150 INIT_LIST_HEAD(oldp);
155 * list_del_init - deletes entry from list and reinitialize it.
156 * @entry: the element to delete from the list.
158 static inline void list_del_init(struct list_head *entry)
160 __list_del(entry->prev, entry->next);
161 INIT_LIST_HEAD(entry);
165 * list_move - delete from one list and add as another's head
166 * @list: the entry to move
167 * @head: the head that will precede our entry
169 static inline void list_move(struct list_head *list, struct list_head *head)
171 __list_del(list->prev, list->next);
172 list_add(list, head);
176 * list_move_tail - delete from one list and add as another's tail
177 * @list: the entry to move
178 * @head: the head that will follow our entry
180 static inline void list_move_tail(struct list_head *list,
181 struct list_head *head)
183 __list_del(list->prev, list->next);
184 list_add_tail(list, head);
188 * list_is_last - tests whether @list is the last entry in list @head
189 * @list: the entry to test
190 * @head: the head of the list
192 static inline int list_is_last(const struct list_head *list,
193 const struct list_head *head)
195 return list->next == head;
199 * list_empty - tests whether a list is empty
200 * @head: the list to test.
202 static inline int list_empty(const struct list_head *head)
204 return head->next == head;
208 * list_empty_careful - tests whether a list is empty and not being modified
209 * @head: the list to test
211 * Description:
212 * tests whether a list is empty _and_ checks that no other CPU might be
213 * in the process of modifying either member (next or prev)
215 * NOTE: using list_empty_careful() without synchronization
216 * can only be safe if the only activity that can happen
217 * to the list entry is list_del_init(). Eg. it cannot be used
218 * if another CPU could re-list_add() it.
220 static inline int list_empty_careful(const struct list_head *head)
222 struct list_head *next = head->next;
223 return (next == head) && (next == head->prev);
226 static inline void __list_splice(struct list_head *list,
227 struct list_head *head)
229 struct list_head *first = list->next;
230 struct list_head *last = list->prev;
231 struct list_head *at = head->next;
233 first->prev = head;
234 head->next = first;
236 last->next = at;
237 at->prev = last;
241 * list_splice - join two lists
242 * @list: the new list to add.
243 * @head: the place to add it in the first list.
245 static inline void list_splice(struct list_head *list, struct list_head *head)
247 if (!list_empty(list))
248 __list_splice(list, head);
252 * list_splice_init - join two lists and reinitialise the emptied list.
253 * @list: the new list to add.
254 * @head: the place to add it in the first list.
256 * The list at @list is reinitialised
258 static inline void list_splice_init(struct list_head *list,
259 struct list_head *head)
261 if (!list_empty(list)) {
262 __list_splice(list, head);
263 INIT_LIST_HEAD(list);
268 * list_entry - get the struct for this entry
269 * @ptr: the &struct list_head pointer.
270 * @type: the type of the struct this is embedded in.
271 * @member: the name of the list_struct within the struct.
273 #define list_entry(ptr, type, member) \
274 container_of(ptr, type, member)
277 * list_first_entry - get the first element from a list
278 * @ptr: the list head to take the element from.
279 * @type: the type of the struct this is embedded in.
280 * @member: the name of the list_struct within the struct.
282 * Note, that list is expected to be not empty.
284 #define list_first_entry(ptr, type, member) \
285 list_entry((ptr)->next, type, member)
289 * list_for_each - iterate over a list
290 * @pos: the &struct list_head to use as a loop cursor.
291 * @head: the head for your list.
293 #define list_for_each(pos, head) \
294 for (pos = (head)->next; prefetch(pos->next), pos != (head); \
295 pos = pos->next)
298 * __list_for_each - iterate over a list
299 * @pos: the &struct list_head to use as a loop cursor.
300 * @head: the head for your list.
302 * This variant differs from list_for_each() in that it's the
303 * simplest possible list iteration code, no prefetching is done.
304 * Use this for code that knows the list to be very short (empty
305 * or 1 entry) most of the time.
307 #define __list_for_each(pos, head) \
308 for (pos = (head)->next; pos != (head); pos = pos->next)
311 * list_for_each_prev - iterate over a list backwards
312 * @pos: the &struct list_head to use as a loop cursor.
313 * @head: the head for your list.
315 #define list_for_each_prev(pos, head) \
316 for (pos = (head)->prev; prefetch(pos->prev), pos != (head); \
317 pos = pos->prev)
320 * list_for_each_safe - iterate over a list safe against removal of list entry
321 * @pos: the &struct list_head to use as a loop cursor.
322 * @n: another &struct list_head to use as temporary storage
323 * @head: the head for your list.
325 #define list_for_each_safe(pos, n, head) \
326 for (pos = (head)->next, n = pos->next; pos != (head); \
327 pos = n, n = pos->next)
330 * list_for_each_entry - iterate over list of given type
331 * @pos: the type * to use as a loop cursor.
332 * @head: the head for your list.
333 * @member: the name of the list_struct within the struct.
335 #define list_for_each_entry(pos, head, member) \
336 for (pos = list_entry((head)->next, typeof(*pos), member); \
337 prefetch(pos->member.next), &pos->member != (head); \
338 pos = list_entry(pos->member.next, typeof(*pos), member))
341 * list_for_each_entry_reverse - iterate backwards over list of given type.
342 * @pos: the type * to use as a loop cursor.
343 * @head: the head for your list.
344 * @member: the name of the list_struct within the struct.
346 #define list_for_each_entry_reverse(pos, head, member) \
347 for (pos = list_entry((head)->prev, typeof(*pos), member); \
348 prefetch(pos->member.prev), &pos->member != (head); \
349 pos = list_entry(pos->member.prev, typeof(*pos), member))
352 * list_prepare_entry - prepare a pos entry for use in list_for_each_entry_continue
353 * @pos: the type * to use as a start point
354 * @head: the head of the list
355 * @member: the name of the list_struct within the struct.
357 * Prepares a pos entry for use as a start point in list_for_each_entry_continue.
359 #define list_prepare_entry(pos, head, member) \
360 ((pos) ? : list_entry(head, typeof(*pos), member))
363 * list_for_each_entry_continue - continue iteration over list of given type
364 * @pos: the type * to use as a loop cursor.
365 * @head: the head for your list.
366 * @member: the name of the list_struct within the struct.
368 * Continue to iterate over list of given type, continuing after
369 * the current position.
371 #define list_for_each_entry_continue(pos, head, member) \
372 for (pos = list_entry(pos->member.next, typeof(*pos), member); \
373 prefetch(pos->member.next), &pos->member != (head); \
374 pos = list_entry(pos->member.next, typeof(*pos), member))
377 * list_for_each_entry_from - iterate over list of given type from the current point
378 * @pos: the type * to use as a loop cursor.
379 * @head: the head for your list.
380 * @member: the name of the list_struct within the struct.
382 * Iterate over list of given type, continuing from current position.
384 #define list_for_each_entry_from(pos, head, member) \
385 for (; prefetch(pos->member.next), &pos->member != (head); \
386 pos = list_entry(pos->member.next, typeof(*pos), member))
389 * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
390 * @pos: the type * to use as a loop cursor.
391 * @n: another type * to use as temporary storage
392 * @head: the head for your list.
393 * @member: the name of the list_struct within the struct.
395 #define list_for_each_entry_safe(pos, n, head, member) \
396 for (pos = list_entry((head)->next, typeof(*pos), member), \
397 n = list_entry(pos->member.next, typeof(*pos), member); \
398 &pos->member != (head); \
399 pos = n, n = list_entry(n->member.next, typeof(*n), member))
402 * list_for_each_entry_safe_continue
403 * @pos: the type * to use as a loop cursor.
404 * @n: another type * to use as temporary storage
405 * @head: the head for your list.
406 * @member: the name of the list_struct within the struct.
408 * Iterate over list of given type, continuing after current point,
409 * safe against removal of list entry.
411 #define list_for_each_entry_safe_continue(pos, n, head, member) \
412 for (pos = list_entry(pos->member.next, typeof(*pos), member), \
413 n = list_entry(pos->member.next, typeof(*pos), member); \
414 &pos->member != (head); \
415 pos = n, n = list_entry(n->member.next, typeof(*n), member))
418 * list_for_each_entry_safe_from
419 * @pos: the type * to use as a loop cursor.
420 * @n: another type * to use as temporary storage
421 * @head: the head for your list.
422 * @member: the name of the list_struct within the struct.
424 * Iterate over list of given type from current point, safe against
425 * removal of list entry.
427 #define list_for_each_entry_safe_from(pos, n, head, member) \
428 for (n = list_entry(pos->member.next, typeof(*pos), member); \
429 &pos->member != (head); \
430 pos = n, n = list_entry(n->member.next, typeof(*n), member))
433 * list_for_each_entry_safe_reverse
434 * @pos: the type * to use as a loop cursor.
435 * @n: another type * to use as temporary storage
436 * @head: the head for your list.
437 * @member: the name of the list_struct within the struct.
439 * Iterate backwards over list of given type, safe against removal
440 * of list entry.
442 #define list_for_each_entry_safe_reverse(pos, n, head, member) \
443 for (pos = list_entry((head)->prev, typeof(*pos), member), \
444 n = list_entry(pos->member.prev, typeof(*pos), member); \
445 &pos->member != (head); \
446 pos = n, n = list_entry(n->member.prev, typeof(*n), member))
449 * Double linked lists with a single pointer list head.
450 * Mostly useful for hash tables where the two pointer list head is
451 * too wasteful.
452 * You lose the ability to access the tail in O(1).
455 struct hlist_head {
456 struct hlist_node *first;
459 struct hlist_node {
460 struct hlist_node *next, **pprev;
463 #define HLIST_HEAD_INIT { .first = NULL }
464 #define HLIST_HEAD(name) struct hlist_head name = { .first = NULL }
465 #define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL)
466 static inline void INIT_HLIST_NODE(struct hlist_node *h)
468 h->next = NULL;
469 h->pprev = NULL;
472 static inline int hlist_unhashed(const struct hlist_node *h)
474 return !h->pprev;
477 static inline int hlist_empty(const struct hlist_head *h)
479 return !h->first;
482 static inline void __hlist_del(struct hlist_node *n)
484 struct hlist_node *next = n->next;
485 struct hlist_node **pprev = n->pprev;
486 *pprev = next;
487 if (next)
488 next->pprev = pprev;
491 static inline void hlist_del(struct hlist_node *n)
493 __hlist_del(n);
494 n->next = (struct hlist_node *)LIST_POISON1;
495 n->pprev = (struct hlist_node **)LIST_POISON2;
498 static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h)
500 struct hlist_node *first = h->first;
501 n->next = first;
502 if (first)
503 first->pprev = &n->next;
504 h->first = n;
505 n->pprev = &h->first;
509 /* next must be != NULL */
510 static inline void hlist_add_before(struct hlist_node *n,
511 struct hlist_node *next)
513 n->pprev = next->pprev;
514 n->next = next;
515 next->pprev = &n->next;
516 *(n->pprev) = n;
519 static inline void hlist_add_after(struct hlist_node *n,
520 struct hlist_node *next)
522 next->next = n->next;
523 n->next = next;
524 next->pprev = &n->next;
526 if(next->next)
527 next->next->pprev = &next->next;
530 #define hlist_entry(ptr, type, member) container_of(ptr,type,member)
532 #define hlist_for_each(pos, head) \
533 for (pos = (head)->first; pos && ({ prefetch(pos->next); 1; }); \
534 pos = pos->next)
536 #define hlist_for_each_safe(pos, n, head) \
537 for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \
538 pos = n)
541 * hlist_for_each_entry - iterate over list of given type
542 * @tpos: the type * to use as a loop cursor.
543 * @pos: the &struct hlist_node to use as a loop cursor.
544 * @head: the head for your list.
545 * @member: the name of the hlist_node within the struct.
547 #define hlist_for_each_entry(tpos, pos, head, member) \
548 for (pos = (head)->first; \
549 pos && ({ prefetch(pos->next); 1;}) && \
550 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
551 pos = pos->next)
554 * hlist_for_each_entry_continue - iterate over a hlist continuing after current point
555 * @tpos: the type * to use as a loop cursor.
556 * @pos: the &struct hlist_node to use as a loop cursor.
557 * @member: the name of the hlist_node within the struct.
559 #define hlist_for_each_entry_continue(tpos, pos, member) \
560 for (pos = (pos)->next; \
561 pos && ({ prefetch(pos->next); 1;}) && \
562 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
563 pos = pos->next)
566 * hlist_for_each_entry_from - iterate over a hlist continuing from current point
567 * @tpos: the type * to use as a loop cursor.
568 * @pos: the &struct hlist_node to use as a loop cursor.
569 * @member: the name of the hlist_node within the struct.
571 #define hlist_for_each_entry_from(tpos, pos, member) \
572 for (; pos && ({ prefetch(pos->next); 1;}) && \
573 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
574 pos = pos->next)
577 * hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry
578 * @tpos: the type * to use as a loop cursor.
579 * @pos: the &struct hlist_node to use as a loop cursor.
580 * @n: another &struct hlist_node to use as temporary storage
581 * @head: the head for your list.
582 * @member: the name of the hlist_node within the struct.
584 #define hlist_for_each_entry_safe(tpos, pos, n, head, member) \
585 for (pos = (head)->first; \
586 pos && ({ n = pos->next; 1; }) && \
587 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
588 pos = n)
590 #ifdef __cplusplus
592 #endif
594 #endif /* _DNET_LIST_H */