source: mainline/uspace/lib/c/include/adt/list.h@ 8f466fc

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 8f466fc was 8f466fc, checked in by Martin Decky <martin@…>, 14 years ago

cstyle (no change in functionality)

  • Property mode set to 100644
File size: 6.0 KB
Line 
1/*
2 * Copyright (c) 2001-2004 Jakub Jermar
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
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.
16 *
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.
27 */
28
29/** @addtogroup libc
30 * @{
31 */
32/** @file
33 */
34
35#ifndef LIBC_LIST_H_
36#define LIBC_LIST_H_
37
38#include <unistd.h>
39
40/** Doubly linked list head and link type. */
41typedef struct link {
42 struct link *prev; /**< Pointer to the previous item in the list. */
43 struct link *next; /**< Pointer to the next item in the list. */
44} link_t;
45
46/** Declare and initialize statically allocated list.
47 *
48 * @param name Name of the new statically allocated list.
49 *
50 */
51#define LIST_INITIALIZE(name) link_t name = { \
52 .prev = &name, \
53 .next = &name \
54}
55
56/** Initialize doubly-linked circular list link
57 *
58 * Initialize doubly-linked list link.
59 *
60 * @param link Pointer to link_t structure to be initialized.
61 *
62 */
63static inline void link_initialize(link_t *link)
64{
65 link->prev = NULL;
66 link->next = NULL;
67}
68
69/** Initialize doubly-linked circular list
70 *
71 * Initialize doubly-linked circular list.
72 *
73 * @param head Pointer to link_t structure representing head of the list.
74 *
75 */
76static inline void list_initialize(link_t *head)
77{
78 head->prev = head;
79 head->next = head;
80}
81
82/** Add item to the beginning of doubly-linked circular list
83 *
84 * Add item to the beginning of doubly-linked circular list.
85 *
86 * @param link Pointer to link_t structure to be added.
87 * @param head Pointer to link_t structure representing head of the list.
88 *
89 */
90static inline void list_prepend(link_t *link, link_t *head)
91{
92 link->next = head->next;
93 link->prev = head;
94 head->next->prev = link;
95 head->next = link;
96}
97
98/** Add item to the end of doubly-linked circular list
99 *
100 * Add item to the end of doubly-linked circular list.
101 *
102 * @param link Pointer to link_t structure to be added.
103 * @param head Pointer to link_t structure representing head of the list.
104 *
105 */
106static inline void list_append(link_t *link, link_t *head)
107{
108 link->prev = head->prev;
109 link->next = head;
110 head->prev->next = link;
111 head->prev = link;
112}
113
114/** Insert item before another item in doubly-linked circular list. */
115static inline void list_insert_before(link_t *l, link_t *r)
116{
117 list_append(l, r);
118}
119
120/** Insert item after another item in doubly-linked circular list. */
121static inline void list_insert_after(link_t *r, link_t *l)
122{
123 list_prepend(l, r);
124}
125
126/** Remove item from doubly-linked circular list
127 *
128 * Remove item from doubly-linked circular list.
129 *
130 * @param link Pointer to link_t structure to be removed from the list
131 * it is contained in.
132 *
133 */
134static inline void list_remove(link_t *link)
135{
136 link->next->prev = link->prev;
137 link->prev->next = link->next;
138 link_initialize(link);
139}
140
141/** Query emptiness of doubly-linked circular list
142 *
143 * Query emptiness of doubly-linked circular list.
144 *
145 * @param head Pointer to link_t structure representing head of the list.
146 *
147 */
148static inline int list_empty(link_t *head)
149{
150 return ((head->next == head) ? 1 : 0);
151}
152
153/** Split or concatenate headless doubly-linked circular list
154 *
155 * Split or concatenate headless doubly-linked circular list.
156 *
157 * Note that the algorithm works both directions:
158 * concatenates splitted lists and splits concatenated lists.
159 *
160 * @param part1 Pointer to link_t structure leading the first
161 * (half of the headless) list.
162 * @param part2 Pointer to link_t structure leading the second
163 * (half of the headless) list.
164 *
165 */
166static inline void headless_list_split_or_concat(link_t *part1, link_t *part2)
167{
168 part1->prev->next = part2;
169 part2->prev->next = part1;
170
171 link_t *hlp = part1->prev;
172
173 part1->prev = part2->prev;
174 part2->prev = hlp;
175}
176
177/** Split headless doubly-linked circular list
178 *
179 * Split headless doubly-linked circular list.
180 *
181 * @param part1 Pointer to link_t structure leading
182 * the first half of the headless list.
183 * @param part2 Pointer to link_t structure leading
184 * the second half of the headless list.
185 *
186 */
187static inline void headless_list_split(link_t *part1, link_t *part2)
188{
189 headless_list_split_or_concat(part1, part2);
190}
191
192/** Concatenate two headless doubly-linked circular lists
193 *
194 * Concatenate two headless doubly-linked circular lists.
195 *
196 * @param part1 Pointer to link_t structure leading
197 * the first headless list.
198 * @param part2 Pointer to link_t structure leading
199 * the second headless list.
200 *
201 */
202static inline void headless_list_concat(link_t *part1, link_t *part2)
203{
204 headless_list_split_or_concat(part1, part2);
205}
206
207#define list_get_instance(link, type, member) \
208 ((type *) (((void *)(link)) - ((void *) &(((type *) NULL)->member))))
209
210extern int list_member(const link_t *link, const link_t *head);
211extern void list_concat(link_t *head1, link_t *head2);
212extern unsigned int list_count(const link_t *link);
213
214#endif
215
216/** @}
217 */
Note: See TracBrowser for help on using the repository browser.