lib/obamp/src/list.h
changeset 2447 8e7887c1247f
child 2448ce6de529ee41
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/lib/obamp/src/list.h	Mon Aug 31 16:17:05 2009 +0200
     1.3 @@ -0,0 +1,279 @@
     1.4 +/*
     1.5 +OLSR OBAMP plugin.
     1.6 +Written by Saverio Proto <zioproto@gmail.com> and Claudio Pisa <clauz@ninux.org>.
     1.7 +
     1.8 +    This file is part of OLSR OBAMP PLUGIN.
     1.9 +
    1.10 +    The OLSR OBAMP PLUGIN is free software: you can redistribute it and/or modify
    1.11 +    it under the terms of the GNU General Public License as published by
    1.12 +    the Free Software Foundation, either version 3 of the License, or
    1.13 +    (at your option) any later version.
    1.14 +
    1.15 +    The OLSR OBAMP PLUGIN is distributed in the hope that it will be useful,
    1.16 +    but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.17 +    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.18 +    GNU General Public License for more details.
    1.19 +
    1.20 +    You should have received a copy of the GNU General Public License
    1.21 +    along with the OLSR OBAMP PLUGIN.  If not, see <http://www.gnu.org/licenses/>.
    1.22 +
    1.23 + */
    1.24 +
    1.25 +//This stuff was from the Kernel, with minor modifications was added here
    1.26 +
    1.27 +#ifndef _LINUX_LIST_H
    1.28 +#define _LINUX_LIST_H
    1.29 +
    1.30 +/* 
    1.31 + * XXX: Resolve conflict between this file and <sys/queue.h> on BSD systems.
    1.32 + */
    1.33 +#ifdef LIST_HEAD
    1.34 +#undef LIST_HEAD
    1.35 +#endif
    1.36 +
    1.37 +/*
    1.38 + * Simple doubly linked list implementation.
    1.39 + *
    1.40 + * Some of the internal functions ("__xxx") are useful when
    1.41 + * manipulating whole lists rather than single entries, as
    1.42 + * sometimes we already know the next/prev entries and we can
    1.43 + * generate better code by using them directly rather than
    1.44 + * using the generic single-entry routines.
    1.45 + */
    1.46 +
    1.47 +struct list_head {
    1.48 +	struct list_head *next, *prev;
    1.49 +};
    1.50 +
    1.51 +#define LIST_HEAD_INIT(name) { &(name), &(name) }
    1.52 +
    1.53 +#define LIST_HEAD(name) \
    1.54 +	struct list_head name = LIST_HEAD_INIT(name)
    1.55 +
    1.56 +#define INIT_LIST_HEAD(ptr) do { \
    1.57 +	(ptr)->next = (ptr); (ptr)->prev = (ptr); \
    1.58 +} while (0)
    1.59 +
    1.60 +/*
    1.61 + * Insert a new entry between two known consecutive entries. 
    1.62 + *
    1.63 + * This is only for internal list manipulation where we know
    1.64 + * the prev/next entries already!
    1.65 + */
    1.66 +static inline void __list_add(struct list_head *new,
    1.67 +			      struct list_head *prev,
    1.68 +			      struct list_head *next)
    1.69 +{
    1.70 +	next->prev = new;
    1.71 +	new->next = next;
    1.72 +	new->prev = prev;
    1.73 +	prev->next = new;
    1.74 +}
    1.75 +
    1.76 +/**
    1.77 + * list_add - add a new entry
    1.78 + * @new: new entry to be added
    1.79 + * @head: list head to add it after
    1.80 + *
    1.81 + * Insert a new entry after the specified head.
    1.82 + * This is good for implementing stacks.
    1.83 + */
    1.84 +static inline void list_add(struct list_head *new, struct list_head *head)
    1.85 +{
    1.86 +	__list_add(new, head, head->next);
    1.87 +}
    1.88 +
    1.89 +/**
    1.90 + * list_add_tail - add a new entry
    1.91 + * @new: new entry to be added
    1.92 + * @head: list head to add it before
    1.93 + *
    1.94 + * Insert a new entry before the specified head.
    1.95 + * This is useful for implementing queues.
    1.96 + */
    1.97 +static inline void list_add_tail(struct list_head *new, struct list_head *head)
    1.98 +{
    1.99 +	__list_add(new, head->prev, head);
   1.100 +}
   1.101 +
   1.102 +/*
   1.103 + * Delete a list entry by making the prev/next entries
   1.104 + * point to each other.
   1.105 + *
   1.106 + * This is only for internal list manipulation where we know
   1.107 + * the prev/next entries already!
   1.108 + */
   1.109 +static inline void __list_del(struct list_head *prev, struct list_head *next)
   1.110 +{
   1.111 +	next->prev = prev;
   1.112 +	prev->next = next;
   1.113 +}
   1.114 +
   1.115 +/**
   1.116 + * list_del - deletes entry from list.
   1.117 + * @entry: the element to delete from the list.
   1.118 + * Note: list_empty on entry does not return true after this, the entry is in an undefined state.
   1.119 + */
   1.120 +static inline void list_del(struct list_head *entry)
   1.121 +{
   1.122 +	__list_del(entry->prev, entry->next);
   1.123 +	//entry->next = (void *) 0;
   1.124 +	//entry->prev = (void *) 0;
   1.125 +}
   1.126 +
   1.127 +/**
   1.128 + * list_del_init - deletes entry from list and reinitialize it.
   1.129 + * @entry: the element to delete from the list.
   1.130 + */
   1.131 +static inline void list_del_init(struct list_head *entry)
   1.132 +{
   1.133 +	__list_del(entry->prev, entry->next);
   1.134 +	INIT_LIST_HEAD(entry); 
   1.135 +}
   1.136 +
   1.137 +/**
   1.138 + * list_move - delete from one list and add as another's head
   1.139 + * @list: the entry to move
   1.140 + * @head: the head that will precede our entry
   1.141 + */
   1.142 +static inline void list_move(struct list_head *list, struct list_head *head)
   1.143 +{
   1.144 +        __list_del(list->prev, list->next);
   1.145 +        list_add(list, head);
   1.146 +}
   1.147 +
   1.148 +/**
   1.149 + * list_move_tail - delete from one list and add as another's tail
   1.150 + * @list: the entry to move
   1.151 + * @head: the head that will follow our entry
   1.152 + */
   1.153 +static inline void list_move_tail(struct list_head *list,
   1.154 +				  struct list_head *head)
   1.155 +{
   1.156 +        __list_del(list->prev, list->next);
   1.157 +        list_add_tail(list, head);
   1.158 +}
   1.159 +
   1.160 +/**
   1.161 + * list_empty - tests whether a list is empty
   1.162 + * @head: the list to test.
   1.163 + */
   1.164 +static inline int list_empty(struct list_head *head)
   1.165 +{
   1.166 +	return head->next == head;
   1.167 +}
   1.168 +
   1.169 +static inline void __list_splice(struct list_head *list,
   1.170 +				 struct list_head *head)
   1.171 +{
   1.172 +	struct list_head *first = list->next;
   1.173 +	struct list_head *last = list->prev;
   1.174 +	struct list_head *at = head->next;
   1.175 +
   1.176 +	first->prev = head;
   1.177 +	head->next = first;
   1.178 +
   1.179 +	last->next = at;
   1.180 +	at->prev = last;
   1.181 +}
   1.182 +
   1.183 +/**
   1.184 + * list_splice - join two lists
   1.185 + * @list: the new list to add.
   1.186 + * @head: the place to add it in the first list.
   1.187 + */
   1.188 +static inline void list_splice(struct list_head *list, struct list_head *head)
   1.189 +{
   1.190 +	if (!list_empty(list))
   1.191 +		__list_splice(list, head);
   1.192 +}
   1.193 +
   1.194 +/**
   1.195 + * list_splice_init - join two lists and reinitialise the emptied list.
   1.196 + * @list: the new list to add.
   1.197 + * @head: the place to add it in the first list.
   1.198 + *
   1.199 + * The list at @list is reinitialised
   1.200 + */
   1.201 +static inline void list_splice_init(struct list_head *list,
   1.202 +				    struct list_head *head)
   1.203 +{
   1.204 +	if (!list_empty(list)) {
   1.205 +		__list_splice(list, head);
   1.206 +		INIT_LIST_HEAD(list);
   1.207 +	}
   1.208 +}
   1.209 +
   1.210 +/**
   1.211 + * list_entry - get the struct for this entry
   1.212 + * @ptr:	the &struct list_head pointer.
   1.213 + * @type:	the type of the struct this is embedded in.
   1.214 + * @member:	the name of the list_struct within the struct.
   1.215 + */
   1.216 +#define list_entry(ptr, type, member) \
   1.217 +	((type *)((char *)(ptr)-(unsigned long)(&((type *)0)->member)))
   1.218 +
   1.219 +/**
   1.220 + * list_for_each	-	iterate over a list
   1.221 + * @pos:	the &struct list_head to use as a loop counter.
   1.222 + * @head:	the head for your list.
   1.223 + */
   1.224 +#define list_for_each(pos, head) \
   1.225 +	for (pos = (head)->next; pos != (head); \
   1.226 +        	pos = pos->next)
   1.227 +/**
   1.228 + * list_for_each_prev	-	iterate over a list backwards
   1.229 + * @pos:	the &struct list_head to use as a loop counter.
   1.230 + * @head:	the head for your list.
   1.231 + */
   1.232 +#define list_for_each_prev(pos, head) \
   1.233 +	for (pos = (head)->prev; pos != (head); \
   1.234 +        	pos = pos->prev)
   1.235 +        	
   1.236 +/**
   1.237 + * list_for_each_safe	-	iterate over a list safe against removal of list entry
   1.238 + * @pos:	the &struct list_head to use as a loop counter.
   1.239 + * @n:		another &struct list_head to use as temporary storage
   1.240 + * @head:	the head for your list.
   1.241 + */
   1.242 +#define list_for_each_safe(pos, n, head) \
   1.243 +	for (pos = (head)->next, n = pos->next; pos != (head); \
   1.244 +		pos = n, n = pos->next)
   1.245 +
   1.246 +/**
   1.247 + * list_for_each_entry	-	iterate over list of given type
   1.248 + * @pos:	the type * to use as a loop counter.
   1.249 + * @head:	the head for your list.
   1.250 + * @member:	the name of the list_struct within the struct.
   1.251 + */
   1.252 +#define list_for_each_entry(pos, head, member)				\
   1.253 +	for (pos = list_entry((head)->next, typeof(*pos), member);	\
   1.254 +	     &pos->member != (head); 					\
   1.255 +	     pos = list_entry(pos->member.next, typeof(*pos), member))
   1.256 +
   1.257 +/**
   1.258 + * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
   1.259 + * @pos:	the type * to use as a loop counter.
   1.260 + * @n:		another type * to use as temporary storage
   1.261 + * @head:	the head for your list.
   1.262 + * @member:	the name of the list_struct within the struct.
   1.263 + */
   1.264 +#define list_for_each_entry_safe(pos, n, head, member)			\
   1.265 +	for (pos = list_entry((head)->next, typeof(*pos), member),	\
   1.266 +		n = list_entry(pos->member.next, typeof(*pos), member);	\
   1.267 +	     &pos->member != (head); 					\
   1.268 +	     pos = n, n = list_entry(n->member.next, typeof(*n), member))
   1.269 +
   1.270 +/**
   1.271 + * list_for_each_entry_continue -       iterate over list of given type
   1.272 + *                      continuing after existing point
   1.273 + * @pos:        the type * to use as a loop counter.
   1.274 + * @head:       the head for your list.
   1.275 + * @member:     the name of the list_struct within the struct.
   1.276 + */
   1.277 +#define list_for_each_entry_continue(pos, head, member)			\
   1.278 +	for (pos = list_entry(pos->member.next, typeof(*pos), member);	\
   1.279 +	     &pos->member != (head);					\
   1.280 +	     pos = list_entry(pos->member.next, typeof(*pos), member))
   1.281 +
   1.282 +#endif