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