VirtualBox

source: vbox/trunk/src/VBox/Additions/x11/x11include/xorg-server-1.11.0/list.h

最後變更 在這個檔案是 38824,由 vboxsync 提交於 14 年 前

Additions/x11: header files for building X.Org Server 1.11 modules

  • 屬性 svn:eol-style 設為 native
檔案大小: 7.6 KB
 
1/*
2 * Copyright © 2010 Intel Corporation
3 * Copyright © 2010 Francisco Jerez <[email protected]>
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22 * IN THE SOFTWARE.
23 *
24 */
25
26#ifndef _LIST_H_
27#define _LIST_H_
28
29/**
30 * @file Classic doubly-link circular list implementation.
31 *
32 * Example:
33 * We need to keep a list of struct foo in the parent struct bar, i.e. what
34 * we want is something like this.
35 *
36 * struct bar {
37 * ...
38 * struct foo *foos; -----> struct foo {}, struct foo {}, struct foo{}
39 * ...
40 * }
41 *
42 * We need one list head in bar and a list element in all foos (both are of
43 * data type 'struct list').
44 *
45 * struct bar {
46 * ...
47 * struct list foos;
48 * ...
49 * }
50 *
51 * struct foo {
52 * ...
53 * struct list entry;
54 * ...
55 * }
56 *
57 * Now we initialize the list head:
58 *
59 * struct bar bar;
60 * ...
61 * list_init(&bar.foos);
62 *
63 * Then we create the first element and add it to this list:
64 *
65 * struct foo *foo = malloc(...);
66 * ....
67 * list_add(&foo->entry, &bar.foos);
68 *
69 * Repeat the above for each element you want to add to the list. Deleting
70 * works with the element itself.
71 * list_del(&foo->entry);
72 * free(foo);
73 *
74 * Note: calling list_del(&bar.foos) will set bar.foos to an empty
75 * list again.
76 *
77 * Looping through the list requires a 'struct foo' as iterator and the
78 * name of the field the subnodes use.
79 *
80 * struct foo *iterator;
81 * list_for_each_entry(iterator, &bar.foos, entry) {
82 * if (iterator->something == ...)
83 * ...
84 * }
85 *
86 * Note: You must not call list_del() on the iterator if you continue the
87 * loop. You need to run the safe for-each loop instead:
88 *
89 * struct foo *iterator, *next;
90 * list_for_each_entry_safe(iterator, next, &bar.foos, entry) {
91 * if (...)
92 * list_del(&iterator->entry);
93 * }
94 *
95 */
96
97/**
98 * The linkage struct for list nodes. This struct must be part of your
99 * to-be-linked struct.
100 *
101 * Example:
102 * struct foo {
103 * int a;
104 * void *b;
105 * struct list *mylist;
106 * }
107 *
108 * Position and name of the struct list field is irrelevant.
109 * There are no requirements that elements of a list are of the same type.
110 * There are no requirements for a list head, any struct list can be a list
111 * head.
112 */
113struct list {
114 struct list *next, *prev;
115};
116
117/**
118 * Initialize the list as an empty list.
119 *
120 * Example:
121 * list_init(&foo->mylist);
122 *
123 * @param The list to initialized.
124 */
125static void
126list_init(struct list *list)
127{
128 list->next = list->prev = list;
129}
130
131static inline void
132__list_add(struct list *entry,
133 struct list *prev,
134 struct list *next)
135{
136 next->prev = entry;
137 entry->next = next;
138 entry->prev = prev;
139 prev->next = entry;
140}
141
142/**
143 * Insert a new element after the given list head.
144 * The list changes from:
145 * head → some element → ...
146 * to
147 * head → new element → older element → ...
148 *
149 * Example:
150 * struct foo *newfoo = malloc(...);
151 * list_add(&newfoo->mylist, &foo->mylist);
152 *
153 * @param entry The new element to prepend to the list.
154 * @param head The existing list.
155 */
156static inline void
157list_add(struct list *entry, struct list *head)
158{
159 __list_add(entry, head, head->next);
160}
161
162static inline void
163__list_del(struct list *prev, struct list *next)
164{
165 next->prev = prev;
166 prev->next = next;
167}
168
169/**
170 * Remove the element from the list it is in. Using this function will reset
171 * the pointers to/from this element so it is removed from the list. It does
172 * NOT free the element itself or manipulate it otherwise.
173 *
174 * Using list_del on a pure list head (like in the example at the top of
175 * this file) will NOT remove the first element from
176 * the list but rather reset the list as empty list.
177 *
178 * Example:
179 * list_del(&newfoo->mylist);
180 *
181 * @param entry The element to remove.
182 */
183static inline void
184list_del(struct list *entry)
185{
186 __list_del(entry->prev, entry->next);
187 list_init(entry);
188}
189
190/**
191 * Check if the list is empty.
192 *
193 * Example:
194 * list_is_empty(&foo->mylist);
195 *
196 * @return True if the list contains one or more elements or False otherwise.
197 */
198static inline Bool
199list_is_empty(struct list *head)
200{
201 return head->next == head;
202}
203
204/**
205 * Returns a pointer to the container of this list element.
206 *
207 * Example:
208 * struct foo* f;
209 * f = container_of(&foo->mylist, struct foo, mylist);
210 * assert(f == foo);
211 *
212 * @param ptr Pointer to the struct list.
213 * @param type Data type of the list element.
214 * @param member Member name of the struct list field in the list element.
215 * @return A pointer to the data struct containing the list head.
216 */
217#ifndef container_of
218#define container_of(ptr, type, member) \
219 (type *)((char *)(ptr) - (char *) &((type *)0)->member)
220#endif
221
222/**
223 * Alias of container_of
224 */
225#define list_entry(ptr, type, member) \
226 container_of(ptr, type, member)
227
228/**
229 * Retrieve the first list entry for the given list pointer.
230 *
231 * Example:
232 * struct foo *first;
233 * first = list_first_entry(&foo->mylist, struct foo, mylist);
234 *
235 * @param ptr The list head
236 * @param type Data type of the list element to retrieve
237 * @param member Member name of the struct list field in the list element.
238 * @return A pointer to the first list element.
239 */
240#define list_first_entry(ptr, type, member) \
241 list_entry((ptr)->next, type, member)
242
243#define __container_of(ptr, sample, member) \
244 (void *)((char *)(ptr) \
245 - ((char *)&(sample)->member - (char *)(sample)))
246/**
247 * Loop through the list given by head and set pos to struct in the list.
248 *
249 * Example:
250 * struct foo *iterator;
251 * list_for_each_entry(iterator, &foo->mylist, mylist) {
252 * [modify iterator]
253 * }
254 *
255 * This macro is not safe for node deletion. Use list_for_each_entry_safe
256 * instead.
257 *
258 * @param pos Iterator variable of the type of the list elements.
259 * @param head List head
260 * @param member Member name of the struct list in the list elements.
261 *
262 */
263#define list_for_each_entry(pos, head, member) \
264 for (pos = __container_of((head)->next, pos, member); \
265 &pos->member != (head); \
266 pos = __container_of(pos->member.next, pos, member))
267
268/**
269 * Loop through the list, keeping a backup pointer to the element. This
270 * macro allows for the deletion of a list element while looping through the
271 * list.
272 *
273 * See list_for_each_entry for more details.
274 */
275#define list_for_each_entry_safe(pos, tmp, head, member) \
276 for (pos = __container_of((head)->next, pos, member), \
277 tmp = __container_of(pos->member.next, pos, member); \
278 &pos->member != (head); \
279 pos = tmp, tmp = __container_of(pos->member.next, tmp, member))
280
281#endif
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette