lib/obamp/src/list.h
author Saverio Proto <zioproto@gmail.com>
Mon Aug 31 16:17:05 2009 +0200 (2 years ago)
changeset 2447 8e7887c1247f
child 2448ce6de529ee41
permissions -rw-r--r--
Adding the first implementation of the OBAMP plugin. See README file for documentation.
     1 /*
     2 OLSR OBAMP plugin.
     3 Written by Saverio Proto <zioproto@gmail.com> and Claudio Pisa <clauz@ninux.org>.
     4 
     5     This file is part of OLSR OBAMP PLUGIN.
     6 
     7     The OLSR OBAMP PLUGIN is free software: you can redistribute it and/or modify
     8     it under the terms of the GNU General Public License as published by
     9     the Free Software Foundation, either version 3 of the License, or
    10     (at your option) any later version.
    11 
    12     The OLSR OBAMP PLUGIN is distributed in the hope that it will be useful,
    13     but WITHOUT ANY WARRANTY; without even the implied warranty of
    14     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    15     GNU General Public License for more details.
    16 
    17     You should have received a copy of the GNU General Public License
    18     along with the OLSR OBAMP PLUGIN.  If not, see <http://www.gnu.org/licenses/>.
    19 
    20  */
    21 
    22 //This stuff was from the Kernel, with minor modifications was added here
    23 
    24 #ifndef _LINUX_LIST_H
    25 #define _LINUX_LIST_H
    26 
    27 /* 
    28  * XXX: Resolve conflict between this file and <sys/queue.h> on BSD systems.
    29  */
    30 #ifdef LIST_HEAD
    31 #undef LIST_HEAD
    32 #endif
    33 
    34 /*
    35  * Simple doubly linked list implementation.
    36  *
    37  * Some of the internal functions ("__xxx") are useful when
    38  * manipulating whole lists rather than single entries, as
    39  * sometimes we already know the next/prev entries and we can
    40  * generate better code by using them directly rather than
    41  * using the generic single-entry routines.
    42  */
    43 
    44 struct list_head {
    45 	struct list_head *next, *prev;
    46 };
    47 
    48 #define LIST_HEAD_INIT(name) { &(name), &(name) }
    49 
    50 #define LIST_HEAD(name) \
    51 	struct list_head name = LIST_HEAD_INIT(name)
    52 
    53 #define INIT_LIST_HEAD(ptr) do { \
    54 	(ptr)->next = (ptr); (ptr)->prev = (ptr); \
    55 } while (0)
    56 
    57 /*
    58  * Insert a new entry between two known consecutive entries. 
    59  *
    60  * This is only for internal list manipulation where we know
    61  * the prev/next entries already!
    62  */
    63 static inline void __list_add(struct list_head *new,
    64 			      struct list_head *prev,
    65 			      struct list_head *next)
    66 {
    67 	next->prev = new;
    68 	new->next = next;
    69 	new->prev = prev;
    70 	prev->next = new;
    71 }
    72 
    73 /**
    74  * list_add - add a new entry
    75  * @new: new entry to be added
    76  * @head: list head to add it after
    77  *
    78  * Insert a new entry after the specified head.
    79  * This is good for implementing stacks.
    80  */
    81 static inline void list_add(struct list_head *new, struct list_head *head)
    82 {
    83 	__list_add(new, head, head->next);
    84 }
    85 
    86 /**
    87  * list_add_tail - add a new entry
    88  * @new: new entry to be added
    89  * @head: list head to add it before
    90  *
    91  * Insert a new entry before the specified head.
    92  * This is useful for implementing queues.
    93  */
    94 static inline void list_add_tail(struct list_head *new, struct list_head *head)
    95 {
    96 	__list_add(new, head->prev, head);
    97 }
    98 
    99 /*
   100  * Delete a list entry by making the prev/next entries
   101  * point to each other.
   102  *
   103  * This is only for internal list manipulation where we know
   104  * the prev/next entries already!
   105  */
   106 static inline void __list_del(struct list_head *prev, struct list_head *next)
   107 {
   108 	next->prev = prev;
   109 	prev->next = next;
   110 }
   111 
   112 /**
   113  * list_del - deletes entry from list.
   114  * @entry: the element to delete from the list.
   115  * Note: list_empty on entry does not return true after this, the entry is in an undefined state.
   116  */
   117 static inline void list_del(struct list_head *entry)
   118 {
   119 	__list_del(entry->prev, entry->next);
   120 	//entry->next = (void *) 0;
   121 	//entry->prev = (void *) 0;
   122 }
   123 
   124 /**
   125  * list_del_init - deletes entry from list and reinitialize it.
   126  * @entry: the element to delete from the list.
   127  */
   128 static inline void list_del_init(struct list_head *entry)
   129 {
   130 	__list_del(entry->prev, entry->next);
   131 	INIT_LIST_HEAD(entry); 
   132 }
   133 
   134 /**
   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
   138  */
   139 static inline void list_move(struct list_head *list, struct list_head *head)
   140 {
   141         __list_del(list->prev, list->next);
   142         list_add(list, head);
   143 }
   144 
   145 /**
   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
   149  */
   150 static inline void list_move_tail(struct list_head *list,
   151 				  struct list_head *head)
   152 {
   153         __list_del(list->prev, list->next);
   154         list_add_tail(list, head);
   155 }
   156 
   157 /**
   158  * list_empty - tests whether a list is empty
   159  * @head: the list to test.
   160  */
   161 static inline int list_empty(struct list_head *head)
   162 {
   163 	return head->next == head;
   164 }
   165 
   166 static inline void __list_splice(struct list_head *list,
   167 				 struct list_head *head)
   168 {
   169 	struct list_head *first = list->next;
   170 	struct list_head *last = list->prev;
   171 	struct list_head *at = head->next;
   172 
   173 	first->prev = head;
   174 	head->next = first;
   175 
   176 	last->next = at;
   177 	at->prev = last;
   178 }
   179 
   180 /**
   181  * list_splice - join two lists
   182  * @list: the new list to add.
   183  * @head: the place to add it in the first list.
   184  */
   185 static inline void list_splice(struct list_head *list, struct list_head *head)
   186 {
   187 	if (!list_empty(list))
   188 		__list_splice(list, head);
   189 }
   190 
   191 /**
   192  * list_splice_init - join two lists and reinitialise the emptied list.
   193  * @list: the new list to add.
   194  * @head: the place to add it in the first list.
   195  *
   196  * The list at @list is reinitialised
   197  */
   198 static inline void list_splice_init(struct list_head *list,
   199 				    struct list_head *head)
   200 {
   201 	if (!list_empty(list)) {
   202 		__list_splice(list, head);
   203 		INIT_LIST_HEAD(list);
   204 	}
   205 }
   206 
   207 /**
   208  * list_entry - get the struct for this entry
   209  * @ptr:	the &struct list_head pointer.
   210  * @type:	the type of the struct this is embedded in.
   211  * @member:	the name of the list_struct within the struct.
   212  */
   213 #define list_entry(ptr, type, member) \
   214 	((type *)((char *)(ptr)-(unsigned long)(&((type *)0)->member)))
   215 
   216 /**
   217  * list_for_each	-	iterate over a list
   218  * @pos:	the &struct list_head to use as a loop counter.
   219  * @head:	the head for your list.
   220  */
   221 #define list_for_each(pos, head) \
   222 	for (pos = (head)->next; pos != (head); \
   223         	pos = pos->next)
   224 /**
   225  * list_for_each_prev	-	iterate over a list backwards
   226  * @pos:	the &struct list_head to use as a loop counter.
   227  * @head:	the head for your list.
   228  */
   229 #define list_for_each_prev(pos, head) \
   230 	for (pos = (head)->prev; pos != (head); \
   231         	pos = pos->prev)
   232         	
   233 /**
   234  * list_for_each_safe	-	iterate over a list safe against removal of list entry
   235  * @pos:	the &struct list_head to use as a loop counter.
   236  * @n:		another &struct list_head to use as temporary storage
   237  * @head:	the head for your list.
   238  */
   239 #define list_for_each_safe(pos, n, head) \
   240 	for (pos = (head)->next, n = pos->next; pos != (head); \
   241 		pos = n, n = pos->next)
   242 
   243 /**
   244  * list_for_each_entry	-	iterate over list of given type
   245  * @pos:	the type * to use as a loop counter.
   246  * @head:	the head for your list.
   247  * @member:	the name of the list_struct within the struct.
   248  */
   249 #define list_for_each_entry(pos, head, member)				\
   250 	for (pos = list_entry((head)->next, typeof(*pos), member);	\
   251 	     &pos->member != (head); 					\
   252 	     pos = list_entry(pos->member.next, typeof(*pos), member))
   253 
   254 /**
   255  * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
   256  * @pos:	the type * to use as a loop counter.
   257  * @n:		another type * to use as temporary storage
   258  * @head:	the head for your list.
   259  * @member:	the name of the list_struct within the struct.
   260  */
   261 #define list_for_each_entry_safe(pos, n, head, member)			\
   262 	for (pos = list_entry((head)->next, typeof(*pos), member),	\
   263 		n = list_entry(pos->member.next, typeof(*pos), member);	\
   264 	     &pos->member != (head); 					\
   265 	     pos = n, n = list_entry(n->member.next, typeof(*n), member))
   266 
   267 /**
   268  * list_for_each_entry_continue -       iterate over list of given type
   269  *                      continuing after existing point
   270  * @pos:        the type * to use as a loop counter.
   271  * @head:       the head for your list.
   272  * @member:     the name of the list_struct within the struct.
   273  */
   274 #define list_for_each_entry_continue(pos, head, member)			\
   275 	for (pos = list_entry(pos->member.next, typeof(*pos), member);	\
   276 	     &pos->member != (head);					\
   277 	     pos = list_entry(pos->member.next, typeof(*pos), member))
   278 
   279 #endif