cynix

x86 UNIX-like OS
git clone git://git.2f30.org/cynix
Log | Files | Refs | README | LICENSE

list.h (1823B)


      1 #ifndef __LIST_H__
      2 #define __LIST_H__
      3 
      4 #include <common.h>
      5 
      6 /**
      7  * list_entry - get the struct for this entry
      8  * @ptr: the &struct list_head pointer.
      9  * @type:   the type of the struct this is embedded in.
     10  * @member: the name of the list_struct within the struct.
     11  */
     12 #define list_entry(ptr, type, member) \
     13 	container_of(ptr, type, member)
     14 
     15 /**
     16  * list_for_each  -  iterate over a list
     17  * @pos: the &struct list_head to use as a loop cursor.
     18  * @head:   the head for your list.
     19  */
     20 #define list_for_each(pos, head) \
     21 	for (pos = (head)->next; pos != (head); \
     22 			pos = pos->next)
     23 /**
     24  * list_for_each_safe - iterate over a list safe against removal of list entry
     25  * @pos: the &struct list_head to use as a loop cursor.
     26  * @n:      another &struct list_head to use as temporary storage
     27  * @head:   the head for your list.
     28  */
     29 #define list_for_each_safe(pos, n, head) \
     30 	for (pos = (head)->next, n = pos->next; pos != (head); \
     31 			pos = n, n = pos->next)
     32 
     33 #define LIST_HEAD(name) \
     34 	struct list_head name = LIST_HEAD_INIT(name)
     35 
     36 #define LIST_HEAD_INIT(name) { &(name), &(name) } /* assign both next and prev to point to &name */
     37 
     38 struct list_head {
     39 	struct list_head *next;
     40 	struct list_head *prev;
     41 };
     42 
     43 static inline void
     44 INIT_LIST_HEAD(struct list_head *list)
     45 {
     46 	list->next = list;
     47 	list->prev = list;
     48 }
     49 
     50 static inline void
     51 list_add(struct list_head *new, struct list_head *head)
     52 {
     53 	head->next->prev = new;
     54 	new->next = head->next;
     55 	new->prev = head;
     56 	head->next = new;
     57 }
     58 
     59 static inline void
     60 list_add_tail(struct list_head *new, struct list_head *head)
     61 {
     62 	head->prev->next = new;
     63 	new->next = head;
     64 	new->prev = head->prev;
     65 	head->prev = new;
     66 }
     67 
     68 static inline void
     69 list_del(struct list_head *entry)
     70 {
     71 	entry->prev->next = entry->next;
     72 	entry->next->prev = entry->prev;
     73 	entry->next = NULL;
     74 	entry->prev = NULL;
     75 }
     76 
     77 #endif
     78