2
* Copyright (c) 2001-2004 Jakub Jermar
5
* Redistribution and use in source and binary forms, with or without
6
* modification, are permitted provided that the following conditions
9
* - Redistributions of source code must retain the above copyright
10
* notice, this list of conditions and the following disclaimer.
11
* - Redistributions in binary form must reproduce the above copyright
12
* notice, this list of conditions and the following disclaimer in the
13
* documentation and/or other materials provided with the distribution.
14
* - The name of the author may not be used to endorse or promote products
15
* derived from this software without specific prior written permission.
17
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40
/** Doubly linked list head and link type. */
42
struct link *prev; /**< Pointer to the previous item in the list. */
43
struct link *next; /**< Pointer to the next item in the list. */
46
/** Declare and initialize statically allocated list.
48
* @param name Name of the new statically allocated list.
50
#define LIST_INITIALIZE(name) link_t name = { \
55
/** Initialize doubly-linked circular list link
57
* Initialize doubly-linked list link.
59
* @param link Pointer to link_t structure to be initialized.
61
static inline void link_initialize(link_t *link)
67
/** Initialize doubly-linked circular list
69
* Initialize doubly-linked circular list.
71
* @param head Pointer to link_t structure representing head of the list.
73
static inline void list_initialize(link_t *head)
79
/** Add item to the beginning of doubly-linked circular list
81
* Add item to the beginning of doubly-linked circular list.
83
* @param link Pointer to link_t structure to be added.
84
* @param head Pointer to link_t structure representing head of the list.
86
static inline void list_prepend(link_t *link, link_t *head)
88
link->next = head->next;
90
head->next->prev = link;
94
/** Add item to the end of doubly-linked circular list
96
* Add item to the end of doubly-linked circular list.
98
* @param link Pointer to link_t structure to be added.
99
* @param head Pointer to link_t structure representing head of the list.
101
static inline void list_append(link_t *link, link_t *head)
103
link->prev = head->prev;
105
head->prev->next = link;
109
/** Insert item before another item in doubly-linked circular list. */
110
static inline void list_insert_before(link_t *l, link_t *r)
115
/** Insert item after another item in doubly-linked circular list. */
116
static inline void list_insert_after(link_t *r, link_t *l)
121
/** Remove item from doubly-linked circular list
123
* Remove item from doubly-linked circular list.
125
* @param link Pointer to link_t structure to be removed from the list it is contained in.
127
static inline void list_remove(link_t *link)
129
link->next->prev = link->prev;
130
link->prev->next = link->next;
131
link_initialize(link);
134
/** Query emptiness of doubly-linked circular list
136
* Query emptiness of doubly-linked circular list.
138
* @param head Pointer to link_t structure representing head of the list.
140
static inline int list_empty(link_t *head)
142
return ((head->next == head) ? 1 : 0);
146
/** Split or concatenate headless doubly-linked circular list
148
* Split or concatenate headless doubly-linked circular list.
150
* Note that the algorithm works both directions:
151
* concatenates splitted lists and splits concatenated lists.
153
* @param part1 Pointer to link_t structure leading the first (half of the headless) list.
154
* @param part2 Pointer to link_t structure leading the second (half of the headless) list.
156
static inline void headless_list_split_or_concat(link_t *part1, link_t *part2)
158
part1->prev->next = part2;
159
part2->prev->next = part1;
161
link_t *hlp = part1->prev;
163
part1->prev = part2->prev;
168
/** Split headless doubly-linked circular list
170
* Split headless doubly-linked circular list.
172
* @param part1 Pointer to link_t structure leading the first half of the headless list.
173
* @param part2 Pointer to link_t structure leading the second half of the headless list.
175
static inline void headless_list_split(link_t *part1, link_t *part2)
177
headless_list_split_or_concat(part1, part2);
180
/** Concatenate two headless doubly-linked circular lists
182
* Concatenate two headless doubly-linked circular lists.
184
* @param part1 Pointer to link_t structure leading the first headless list.
185
* @param part2 Pointer to link_t structure leading the second headless list.
187
static inline void headless_list_concat(link_t *part1, link_t *part2)
189
headless_list_split_or_concat(part1, part2);
192
#define list_get_instance(link, type, member) ((type *) (((void *)(link)) - ((void *) &(((type *) NULL)->member))))
194
extern int list_member(const link_t *link, const link_t *head);
195
extern void list_concat(link_t *head1, link_t *head2);
196
extern unsigned int list_count(const link_t *link);