1/* 2 * Copied from the Linux kernel source tree, version 2.6.0-test1. 3 * 4 * Licensed under the GPL v2 as per the whole kernel source tree. 5 * 6 */ 7 8#ifndef _LIST_H 9#define _LIST_H 10 11#undef offsetof 12#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER) 13 14/** 15 * container_of - cast a member of a structure out to the containing structure 16 * 17 * @ptr: the pointer to the member. 18 * @type: the type of the container struct this is embedded in. 19 * @member: the name of the member within the struct. 20 * 21 */ 22#define container_of(ptr, type, member) ({ \ 23 const typeof( ((type *)0)->member ) *__mptr = (ptr); \ 24 (type *)( (char *)__mptr - offsetof(type,member) );}) 25 26/* 27 * Simple doubly linked list implementation. 28 * 29 * Some of the internal functions ("__xxx") are useful when 30 * manipulating whole lists rather than single entries, as 31 * sometimes we already know the next/prev entries and we can 32 * generate better code by using them directly rather than 33 * using the generic single-entry routines. 34 */ 35 36struct list_head { 37 struct list_head *next, *prev; 38}; 39 40#define LIST_HEAD_INIT(name) { &(name), &(name) } 41 42#define LIST_HEAD(name) \ 43 struct list_head name = LIST_HEAD_INIT(name) 44 45#define INIT_LIST_HEAD(ptr) do { \ 46 (ptr)->next = (ptr); (ptr)->prev = (ptr); \ 47} while (0) 48 49/* 50 * Insert a new entry between two known consecutive entries. 51 * 52 * This is only for internal list manipulation where we know 53 * the prev/next entries already! 54 */ 55static inline void __list_add(struct list_head *new, 56 struct list_head *prev, 57 struct list_head *next) 58{ 59 next->prev = new; 60 new->next = next; 61 new->prev = prev; 62 prev->next = new; 63} 64 65/** 66 * list_add - add a new entry 67 * @new: new entry to be added 68 * @head: list head to add it after 69 * 70 * Insert a new entry after the specified head. 71 * This is good for implementing stacks. 72 */ 73static inline void list_add(struct list_head *new, struct list_head *head) 74{ 75 __list_add(new, head, head->next); 76} 77 78/** 79 * list_add_tail - add a new entry 80 * @new: new entry to be added 81 * @head: list head to add it before 82 * 83 * Insert a new entry before the specified head. 84 * This is useful for implementing queues. 85 */ 86static inline void list_add_tail(struct list_head *new, struct list_head *head) 87{ 88 __list_add(new, head->prev, head); 89} 90 91/* 92 * Delete a list entry by making the prev/next entries 93 * point to each other. 94 * 95 * This is only for internal list manipulation where we know 96 * the prev/next entries already! 97 */ 98static inline void __list_del(struct list_head *prev, struct list_head *next) 99{ 100 next->prev = prev; 101 prev->next = next; 102} 103 104/** 105 * list_del - deletes entry from list. 106 * @entry: the element to delete from the list. 107 * Note: list_empty on entry does not return true after this, the entry is 108 * in an undefined state. 109 */ 110static inline void list_del(struct list_head *entry) 111{ 112 __list_del(entry->prev, entry->next); 113} 114 115/** 116 * list_empty - tests whether a list is empty 117 * @head: the list to test. 118 */ 119static inline int list_empty(struct list_head *head) 120{ 121 return head->next == head; 122} 123 124/** 125 * list_entry - get the struct for this entry 126 * @ptr: the &struct list_head pointer. 127 * @type: the type of the struct this is embedded in. 128 * @member: the name of the list_struct within the struct. 129 */ 130#define list_entry(ptr, type, member) \ 131 container_of(ptr, type, member) 132 133/** 134 * list_for_each - iterate over a list 135 * @pos: the &struct list_head to use as a loop counter. 136 * @head: the head for your list. 137 */ 138#define list_for_each(pos, head) \ 139 for (pos = (head)->next; pos != (head); pos = pos->next) 140 141/** 142 * list_for_each_safe - iterate over a list safe against removal of list entry 143 * @pos: the &struct list_head to use as a loop counter. 144 * @n: another &struct list_head to use as temporary storage 145 * @head: the head for your list. 146 */ 147#define list_for_each_safe(pos, n, head) \ 148 for (pos = (head)->next, n = pos->next; pos != (head); \ 149 pos = n, n = pos->next) 150 151#endif /* _LIST_H */ 152 153