Rev 999 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 999 | Rev 1392 | ||
---|---|---|---|
1 | /* |
1 | /* |
2 | * Copyright (C) 2001-2004 Jakub Jermar |
2 | * Copyright (C) 2001-2004 Jakub Jermar |
3 | * All rights reserved. |
3 | * All rights reserved. |
4 | * |
4 | * |
5 | * Redistribution and use in source and binary forms, with or without |
5 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions |
6 | * modification, are permitted provided that the following conditions |
7 | * are met: |
7 | * are met: |
8 | * |
8 | * |
9 | * - Redistributions of source code must retain the above copyright |
9 | * - Redistributions of source code must retain the above copyright |
10 | * notice, this list of conditions and the following disclaimer. |
10 | * notice, this list of conditions and the following disclaimer. |
11 | * - Redistributions in binary form must reproduce the above copyright |
11 | * - Redistributions in binary form must reproduce the above copyright |
12 | * notice, this list of conditions and the following disclaimer in the |
12 | * notice, this list of conditions and the following disclaimer in the |
13 | * documentation and/or other materials provided with the distribution. |
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 |
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. |
15 | * derived from this software without specific prior written permission. |
16 | * |
16 | * |
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
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 |
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
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 |
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
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 |
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. |
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | */ |
27 | */ |
28 | 28 | ||
29 | #ifndef __LIST_H__ |
29 | #ifndef __LIST_H__ |
30 | #define __LIST_H__ |
30 | #define __LIST_H__ |
31 | 31 | ||
32 | #include<unistd.h> |
32 | #include<unistd.h> |
33 | 33 | ||
34 | #ifndef true |
34 | #ifndef true |
35 | # define true 1 |
35 | # define true 1 |
36 | #endif |
36 | #endif |
37 | #ifndef false |
37 | #ifndef false |
38 | # define false 0 |
38 | # define false 0 |
39 | #endif |
39 | #endif |
40 | 40 | ||
41 | typedef struct link link_t; |
41 | typedef struct link link_t; |
42 | 42 | ||
43 | /** Doubly linked list head and link type. */ |
43 | /** Doubly linked list head and link type. */ |
44 | struct link { |
44 | struct link { |
45 | link_t *prev; /**< Pointer to the previous item in the list. */ |
45 | link_t *prev; /**< Pointer to the previous item in the list. */ |
46 | link_t *next; /**< Pointer to the next item in the list. */ |
46 | link_t *next; /**< Pointer to the next item in the list. */ |
47 | }; |
47 | }; |
48 | 48 | ||
49 | /** Declare and initialize statically allocated list. |
49 | /** Declare and initialize statically allocated list. |
50 | * |
50 | * |
51 | * @param name Name of the new statically allocated list. |
51 | * @param name Name of the new statically allocated list. |
52 | */ |
52 | */ |
53 | #define LIST_INITIALIZE(name) link_t name = { .prev = &name, .next = &name } |
53 | #define LIST_INITIALIZE(name) link_t name = { .prev = &name, .next = &name } |
54 | 54 | ||
55 | /** Initialize doubly-linked circular list link |
55 | /** Initialize doubly-linked circular list link |
56 | * |
56 | * |
57 | * Initialize doubly-linked list link. |
57 | * Initialize doubly-linked list link. |
58 | * |
58 | * |
59 | * @param link Pointer to link_t structure to be initialized. |
59 | * @param link Pointer to link_t structure to be initialized. |
60 | */ |
60 | */ |
61 | static inline void link_initialize(link_t *link) |
61 | static inline void link_initialize(link_t *link) |
62 | { |
62 | { |
63 | link->prev = NULL; |
63 | link->prev = NULL; |
64 | link->next = NULL; |
64 | link->next = NULL; |
65 | } |
65 | } |
66 | 66 | ||
67 | /** Initialize doubly-linked circular list |
67 | /** Initialize doubly-linked circular list |
68 | * |
68 | * |
69 | * Initialize doubly-linked circular list. |
69 | * Initialize doubly-linked circular list. |
70 | * |
70 | * |
71 | * @param head Pointer to link_t structure representing head of the list. |
71 | * @param head Pointer to link_t structure representing head of the list. |
72 | */ |
72 | */ |
73 | static inline void list_initialize(link_t *head) |
73 | static inline void list_initialize(link_t *head) |
74 | { |
74 | { |
75 | head->prev = head; |
75 | head->prev = head; |
76 | head->next = head; |
76 | head->next = head; |
77 | } |
77 | } |
78 | 78 | ||
79 | /** Add item to the beginning of doubly-linked circular list |
79 | /** Add item to the beginning of doubly-linked circular list |
80 | * |
80 | * |
81 | * Add item to the beginning of doubly-linked circular list. |
81 | * Add item to the beginning of doubly-linked circular list. |
82 | * |
82 | * |
83 | * @param link Pointer to link_t structure to be added. |
83 | * @param link Pointer to link_t structure to be added. |
84 | * @param head Pointer to link_t structure representing head of the list. |
84 | * @param head Pointer to link_t structure representing head of the list. |
85 | */ |
85 | */ |
86 | static inline void list_prepend(link_t *link, link_t *head) |
86 | static inline void list_prepend(link_t *link, link_t *head) |
87 | { |
87 | { |
88 | link->next = head->next; |
88 | link->next = head->next; |
89 | link->prev = head; |
89 | link->prev = head; |
90 | head->next->prev = link; |
90 | head->next->prev = link; |
91 | head->next = link; |
91 | head->next = link; |
92 | } |
92 | } |
93 | 93 | ||
94 | /** Add item to the end of doubly-linked circular list |
94 | /** Add item to the end of doubly-linked circular list |
95 | * |
95 | * |
96 | * Add item to the end of doubly-linked circular list. |
96 | * Add item to the end of doubly-linked circular list. |
97 | * |
97 | * |
98 | * @param link Pointer to link_t structure to be added. |
98 | * @param link Pointer to link_t structure to be added. |
99 | * @param head Pointer to link_t structure representing head of the list. |
99 | * @param head Pointer to link_t structure representing head of the list. |
100 | */ |
100 | */ |
101 | static inline void list_append(link_t *link, link_t *head) |
101 | static inline void list_append(link_t *link, link_t *head) |
102 | { |
102 | { |
103 | link->prev = head->prev; |
103 | link->prev = head->prev; |
104 | link->next = head; |
104 | link->next = head; |
105 | head->prev->next = link; |
105 | head->prev->next = link; |
106 | head->prev = link; |
106 | head->prev = link; |
107 | } |
107 | } |
108 | 108 | ||
109 | /** Remove item from doubly-linked circular list |
109 | /** Remove item from doubly-linked circular list |
110 | * |
110 | * |
111 | * Remove item from doubly-linked circular list. |
111 | * Remove item from doubly-linked circular list. |
112 | * |
112 | * |
113 | * @param link Pointer to link_t structure to be removed from the list it is contained in. |
113 | * @param link Pointer to link_t structure to be removed from the list it is contained in. |
114 | */ |
114 | */ |
115 | static inline void list_remove(link_t *link) |
115 | static inline void list_remove(link_t *link) |
116 | { |
116 | { |
117 | link->next->prev = link->prev; |
117 | link->next->prev = link->prev; |
118 | link->prev->next = link->next; |
118 | link->prev->next = link->next; |
119 | link_initialize(link); |
119 | link_initialize(link); |
120 | } |
120 | } |
121 | 121 | ||
122 | /** Query emptiness of doubly-linked circular list |
122 | /** Query emptiness of doubly-linked circular list |
123 | * |
123 | * |
124 | * Query emptiness of doubly-linked circular list. |
124 | * Query emptiness of doubly-linked circular list. |
125 | * |
125 | * |
126 | * @param head Pointer to link_t structure representing head of the list. |
126 | * @param head Pointer to link_t structure representing head of the list. |
127 | */ |
127 | */ |
128 | static inline int list_empty(link_t *head) |
128 | static inline int list_empty(link_t *head) |
129 | { |
129 | { |
130 | return head->next == head ? true : false; |
130 | return head->next == head ? true : false; |
131 | } |
131 | } |
132 | 132 | ||
133 | 133 | ||
134 | /** Split or concatenate headless doubly-linked circular list |
134 | /** Split or concatenate headless doubly-linked circular list |
135 | * |
135 | * |
136 | * Split or concatenate headless doubly-linked circular list. |
136 | * Split or concatenate headless doubly-linked circular list. |
137 | * |
137 | * |
138 | * Note that the algorithm works both directions: |
138 | * Note that the algorithm works both directions: |
139 | * concatenates splitted lists and splits concatenated lists. |
139 | * concatenates splitted lists and splits concatenated lists. |
140 | * |
140 | * |
141 | * @param part1 Pointer to link_t structure leading the first (half of the headless) list. |
141 | * @param part1 Pointer to link_t structure leading the first (half of the headless) list. |
142 | * @param part2 Pointer to link_t structure leading the second (half of the headless) list. |
142 | * @param part2 Pointer to link_t structure leading the second (half of the headless) list. |
143 | */ |
143 | */ |
144 | static inline void headless_list_split_or_concat(link_t *part1, link_t *part2) |
144 | static inline void headless_list_split_or_concat(link_t *part1, link_t *part2) |
145 | { |
145 | { |
146 | link_t *hlp; |
146 | link_t *hlp; |
147 | 147 | ||
148 | part1->prev->next = part2; |
148 | part1->prev->next = part2; |
149 | part2->prev->next = part1; |
149 | part2->prev->next = part1; |
150 | hlp = part1->prev; |
150 | hlp = part1->prev; |
151 | part1->prev = part2->prev; |
151 | part1->prev = part2->prev; |
152 | part2->prev = hlp; |
152 | part2->prev = hlp; |
153 | } |
153 | } |
154 | 154 | ||
155 | 155 | ||
156 | /** Split headless doubly-linked circular list |
156 | /** Split headless doubly-linked circular list |
157 | * |
157 | * |
158 | * Split headless doubly-linked circular list. |
158 | * Split headless doubly-linked circular list. |
159 | * |
159 | * |
160 | * @param part1 Pointer to link_t structure leading the first half of the headless list. |
160 | * @param part1 Pointer to link_t structure leading the first half of the headless list. |
161 | * @param part2 Pointer to link_t structure leading the second half of the headless list. |
161 | * @param part2 Pointer to link_t structure leading the second half of the headless list. |
162 | */ |
162 | */ |
163 | static inline void headless_list_split(link_t *part1, link_t *part2) |
163 | static inline void headless_list_split(link_t *part1, link_t *part2) |
164 | { |
164 | { |
165 | headless_list_split_or_concat(part1, part2); |
165 | headless_list_split_or_concat(part1, part2); |
166 | } |
166 | } |
167 | 167 | ||
168 | /** Concatenate two headless doubly-linked circular lists |
168 | /** Concatenate two headless doubly-linked circular lists |
169 | * |
169 | * |
170 | * Concatenate two headless doubly-linked circular lists. |
170 | * Concatenate two headless doubly-linked circular lists. |
171 | * |
171 | * |
172 | * @param part1 Pointer to link_t structure leading the first headless list. |
172 | * @param part1 Pointer to link_t structure leading the first headless list. |
173 | * @param part2 Pointer to link_t structure leading the second headless list. |
173 | * @param part2 Pointer to link_t structure leading the second headless list. |
174 | */ |
174 | */ |
175 | static inline void headless_list_concat(link_t *part1, link_t *part2) |
175 | static inline void headless_list_concat(link_t *part1, link_t *part2) |
176 | { |
176 | { |
177 | headless_list_split_or_concat(part1, part2); |
177 | headless_list_split_or_concat(part1, part2); |
178 | } |
178 | } |
179 | 179 | ||
180 | #define list_get_instance(link,type,member) (type *)(((char *)(link))-((char *)&(((type *)NULL)->member))) |
180 | #define list_get_instance(link,type,member) (type *)(((char *)(link))-((char *)&(((type *)NULL)->member))) |
181 | 181 | ||
182 | extern int list_member(const link_t *link, const link_t *head); |
182 | extern int list_member(const link_t *link, const link_t *head); |
183 | extern void list_concat(link_t *head1, link_t *head2); |
183 | extern void list_concat(link_t *head1, link_t *head2); |
184 | 184 | ||
185 | #endif |
185 | #endif |
186 | 186 |