VirtualBox

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

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

Additions/xorg: support X.Org Server 1.12.

  • 屬性 svn:eol-style 設為 native
檔案大小: 14.2 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 _XORG_LIST_H_
27#define _XORG_LIST_H_
28
29/**
30 * @file Classic doubly-link circular list implementation.
31 * For real usage examples of the linked list, see the file test/list.c
32 *
33 * Example:
34 * We need to keep a list of struct foo in the parent struct bar, i.e. what
35 * we want is something like this.
36 *
37 * struct bar {
38 * ...
39 * struct foo *list_of_foos; -----> struct foo {}, struct foo {}, struct foo{}
40 * ...
41 * }
42 *
43 * We need one list head in bar and a list element in all list_of_foos (both are of
44 * data type 'struct xorg_list').
45 *
46 * struct bar {
47 * ...
48 * struct xorg_list list_of_foos;
49 * ...
50 * }
51 *
52 * struct foo {
53 * ...
54 * struct xorg_list entry;
55 * ...
56 * }
57 *
58 * Now we initialize the list head:
59 *
60 * struct bar bar;
61 * ...
62 * xorg_list_init(&bar.list_of_foos);
63 *
64 * Then we create the first element and add it to this list:
65 *
66 * struct foo *foo = malloc(...);
67 * ....
68 * xorg_list_add(&foo->entry, &bar.list_of_foos);
69 *
70 * Repeat the above for each element you want to add to the list. Deleting
71 * works with the element itself.
72 * xorg_list_del(&foo->entry);
73 * free(foo);
74 *
75 * Note: calling xorg_list_del(&bar.list_of_foos) will set bar.list_of_foos to an empty
76 * list again.
77 *
78 * Looping through the list requires a 'struct foo' as iterator and the
79 * name of the field the subnodes use.
80 *
81 * struct foo *iterator;
82 * xorg_list_for_each_entry(iterator, &bar.list_of_foos, entry) {
83 * if (iterator->something == ...)
84 * ...
85 * }
86 *
87 * Note: You must not call xorg_list_del() on the iterator if you continue the
88 * loop. You need to run the safe for-each loop instead:
89 *
90 * struct foo *iterator, *next;
91 * xorg_list_for_each_entry_safe(iterator, next, &bar.list_of_foos, entry) {
92 * if (...)
93 * xorg_list_del(&iterator->entry);
94 * }
95 *
96 */
97
98/**
99 * The linkage struct for list nodes. This struct must be part of your
100 * to-be-linked struct. struct xorg_list is required for both the head of the
101 * list and for each list node.
102 *
103 * Position and name of the struct xorg_list field is irrelevant.
104 * There are no requirements that elements of a list are of the same type.
105 * There are no requirements for a list head, any struct xorg_list can be a list
106 * head.
107 */
108struct xorg_list {
109 struct xorg_list *next, *prev;
110};
111
112/**
113 * Initialize the list as an empty list.
114 *
115 * Example:
116 * xorg_list_init(&bar->list_of_foos);
117 *
118 * @param The list to initialized.
119 */
120static void
121xorg_list_init(struct xorg_list *list)
122{
123 list->next = list->prev = list;
124}
125
126static inline void
127__xorg_list_add(struct xorg_list *entry,
128 struct xorg_list *prev,
129 struct xorg_list *next)
130{
131 next->prev = entry;
132 entry->next = next;
133 entry->prev = prev;
134 prev->next = entry;
135}
136
137/**
138 * Insert a new element after the given list head. The new element does not
139 * need to be initialised as empty list.
140 * The list changes from:
141 * head → some element → ...
142 * to
143 * head → new element → older element → ...
144 *
145 * Example:
146 * struct foo *newfoo = malloc(...);
147 * xorg_list_add(&newfoo->entry, &bar->list_of_foos);
148 *
149 * @param entry The new element to prepend to the list.
150 * @param head The existing list.
151 */
152static inline void
153xorg_list_add(struct xorg_list *entry, struct xorg_list *head)
154{
155 __xorg_list_add(entry, head, head->next);
156}
157
158/**
159 * Append a new element to the end of the list given with this list head.
160 *
161 * The list changes from:
162 * head → some element → ... → lastelement
163 * to
164 * head → some element → ... → lastelement → new element
165 *
166 * Example:
167 * struct foo *newfoo = malloc(...);
168 * xorg_list_append(&newfoo->entry, &bar->list_of_foos);
169 *
170 * @param entry The new element to prepend to the list.
171 * @param head The existing list.
172 */
173static inline void
174xorg_list_append(struct xorg_list *entry, struct xorg_list *head)
175{
176 __xorg_list_add(entry, head->prev, head);
177}
178
179
180static inline void
181__xorg_list_del(struct xorg_list *prev, struct xorg_list *next)
182{
183 next->prev = prev;
184 prev->next = next;
185}
186
187/**
188 * Remove the element from the list it is in. Using this function will reset
189 * the pointers to/from this element so it is removed from the list. It does
190 * NOT free the element itself or manipulate it otherwise.
191 *
192 * Using xorg_list_del on a pure list head (like in the example at the top of
193 * this file) will NOT remove the first element from
194 * the list but rather reset the list as empty list.
195 *
196 * Example:
197 * xorg_list_del(&foo->entry);
198 *
199 * @param entry The element to remove.
200 */
201static inline void
202xorg_list_del(struct xorg_list *entry)
203{
204 __xorg_list_del(entry->prev, entry->next);
205 xorg_list_init(entry);
206}
207
208/**
209 * Check if the list is empty.
210 *
211 * Example:
212 * xorg_list_is_empty(&bar->list_of_foos);
213 *
214 * @return True if the list contains one or more elements or False otherwise.
215 */
216static inline Bool
217xorg_list_is_empty(struct xorg_list *head)
218{
219 return head->next == head;
220}
221
222/**
223 * Returns a pointer to the container of this list element.
224 *
225 * Example:
226 * struct foo* f;
227 * f = container_of(&foo->entry, struct foo, entry);
228 * assert(f == foo);
229 *
230 * @param ptr Pointer to the struct xorg_list.
231 * @param type Data type of the list element.
232 * @param member Member name of the struct xorg_list field in the list element.
233 * @return A pointer to the data struct containing the list head.
234 */
235#ifndef container_of
236#define container_of(ptr, type, member) \
237 (type *)((char *)(ptr) - (char *) &((type *)0)->member)
238#endif
239
240/**
241 * Alias of container_of
242 */
243#define xorg_list_entry(ptr, type, member) \
244 container_of(ptr, type, member)
245
246/**
247 * Retrieve the first list entry for the given list pointer.
248 *
249 * Example:
250 * struct foo *first;
251 * first = xorg_list_first_entry(&bar->list_of_foos, struct foo, list_of_foos);
252 *
253 * @param ptr The list head
254 * @param type Data type of the list element to retrieve
255 * @param member Member name of the struct xorg_list field in the list element.
256 * @return A pointer to the first list element.
257 */
258#define xorg_list_first_entry(ptr, type, member) \
259 xorg_list_entry((ptr)->next, type, member)
260
261/**
262 * Retrieve the last list entry for the given listpointer.
263 *
264 * Example:
265 * struct foo *first;
266 * first = xorg_list_last_entry(&bar->list_of_foos, struct foo, list_of_foos);
267 *
268 * @param ptr The list head
269 * @param type Data type of the list element to retrieve
270 * @param member Member name of the struct xorg_list field in the list element.
271 * @return A pointer to the last list element.
272 */
273#define xorg_list_last_entry(ptr, type, member) \
274 xorg_list_entry((ptr)->prev, type, member)
275
276#define __container_of(ptr, sample, member) \
277 (void *)((char *)(ptr) \
278 - ((char *)&(sample)->member - (char *)(sample)))
279/**
280 * Loop through the list given by head and set pos to struct in the list.
281 *
282 * Example:
283 * struct foo *iterator;
284 * xorg_list_for_each_entry(iterator, &bar->list_of_foos, entry) {
285 * [modify iterator]
286 * }
287 *
288 * This macro is not safe for node deletion. Use xorg_list_for_each_entry_safe
289 * instead.
290 *
291 * @param pos Iterator variable of the type of the list elements.
292 * @param head List head
293 * @param member Member name of the struct xorg_list in the list elements.
294 *
295 */
296#define xorg_list_for_each_entry(pos, head, member) \
297 for (pos = __container_of((head)->next, pos, member); \
298 &pos->member != (head); \
299 pos = __container_of(pos->member.next, pos, member))
300
301/**
302 * Loop through the list, keeping a backup pointer to the element. This
303 * macro allows for the deletion of a list element while looping through the
304 * list.
305 *
306 * See xorg_list_for_each_entry for more details.
307 */
308#define xorg_list_for_each_entry_safe(pos, tmp, head, member) \
309 for (pos = __container_of((head)->next, pos, member), \
310 tmp = __container_of(pos->member.next, pos, member); \
311 &pos->member != (head); \
312 pos = tmp, tmp = __container_of(pos->member.next, tmp, member))
313
314
315
316/* NULL-Terminated List Interface
317 *
318 * The interface below does _not_ use the struct xorg_list as described above.
319 * It is mainly for legacy structures that cannot easily be switched to
320 * struct xorg_list.
321 *
322 * This interface is for structs like
323 * struct foo {
324 * [...]
325 * struct foo *next;
326 * [...]
327 * };
328 *
329 * The position and field name of "next" are arbitrary.
330 */
331
332/**
333 * Init the element as null-terminated list.
334 *
335 * Example:
336 * struct foo *list = malloc();
337 * nt_list_init(list, next);
338 *
339 * @param list The list element that will be the start of the list
340 * @param member Member name of the field pointing to next struct
341 */
342#define nt_list_init(_list, _member) \
343 (_list)->_member = NULL
344
345/**
346 * Returns the next element in the list or NULL on termination.
347 *
348 * Example:
349 * struct foo *element = list;
350 * while ((element = nt_list_next(element, next)) { }
351 *
352 * This macro is not safe for node deletion. Use xorg_list_for_each_entry_safe
353 * instead.
354 *
355 * @param list The list or current element.
356 * @param member Member name of the field pointing to next struct.
357 */
358#define nt_list_next(_list, _member) \
359 (_list)->_member
360
361/**
362 * Iterate through each element in the list.
363 *
364 * Example:
365 * struct foo *iterator;
366 * nt_list_for_each_entry(iterator, list, next) {
367 * [modify iterator]
368 * }
369 *
370 * @param entry Assigned to the current list element
371 * @param list The list to iterate through.
372 * @param member Member name of the field pointing to next struct.
373 */
374#define nt_list_for_each_entry(_entry, _list, _member) \
375 for (_entry = _list; _entry; _entry = (_entry)->_member)
376
377/**
378 * Iterate through each element in the list, keeping a backup pointer to the
379 * element. This macro allows for the deletion of a list element while
380 * looping through the list.
381 *
382 * See nt_list_for_each_entry for more details.
383 *
384 * @param entry Assigned to the current list element
385 * @param tmp The pointer to the next element
386 * @param list The list to iterate through.
387 * @param member Member name of the field pointing to next struct.
388 */
389#define nt_list_for_each_entry_safe(_entry, _tmp, _list, _member) \
390 for (_entry = _list, _tmp = (_entry) ? (_entry)->_member : NULL;\
391 _entry; \
392 _entry = _tmp, _tmp = (_tmp) ? (_tmp)->_member: NULL)
393
394
395/**
396 * Append the element to the end of the list. This macro may be used to
397 * merge two lists.
398 *
399 * Example:
400 * struct foo *elem = malloc(...);
401 * nt_list_init(elem, next)
402 * nt_list_append(elem, list, struct foo, next);
403 *
404 * Resulting list order:
405 * list_item_0 -> list_item_1 -> ... -> elem_item_0 -> elem_item_1 ...
406 *
407 * @param entry An entry (or list) to append to the list
408 * @param list The list to append to. This list must be a valid list, not
409 * NULL.
410 * @param type The list type
411 * @param member Member name of the field pointing to next struct
412 */
413#define nt_list_append(_entry, _list, _type, _member) \
414 do { \
415 _type *__iterator = _list; \
416 while (__iterator->_member) { __iterator = __iterator->_member;}\
417 __iterator->_member = _entry; \
418 } while (0)
419
420/**
421 * Insert the element at the next position in the list. This macro may be
422 * used to insert a list into a list.
423 *
424 * struct foo *elem = malloc(...);
425 * nt_list_init(elem, next)
426 * nt_list_insert(elem, list, struct foo, next);
427 *
428 * Resulting list order:
429 * list_item_0 -> elem_item_0 -> elem_item_1 ... -> list_item_1 -> ...
430 *
431 * @param entry An entry (or list) to append to the list
432 * @param list The list to insert to. This list must be a valid list, not
433 * NULL.
434 * @param type The list type
435 * @param member Member name of the field pointing to next struct
436 */
437#define nt_list_insert(_entry, _list, _type, _member) \
438 do { \
439 nt_list_append((_list)->_member, _entry, _type, _member); \
440 (_list)->_member = _entry; \
441 } while (0)
442
443/**
444 * Delete the entry from the list by iterating through the list and
445 * removing any reference from the list to the entry.
446 *
447 * Example:
448 * struct foo *elem = <assign to right element>
449 * nt_list_del(elem, list, struct foo, next);
450 *
451 * @param entry The entry to delete from the list. entry is always
452 * re-initialized as a null-terminated list.
453 * @param list The list containing the entry, set to the new list without
454 * the removed entry.
455 * @param type The list type
456 * @param member Member name of the field pointing to the next entry
457 */
458#define nt_list_del(_entry, _list, _type, _member) \
459 do { \
460 _type *__e = _entry; \
461 if (__e == NULL) break; \
462 if ((_list) == __e) { \
463 _list = __e->_member; \
464 } else { \
465 _type *__prev = _list; \
466 while (__prev->_member && __prev->_member != __e) \
467 __prev = nt_list_next(__prev, _member); \
468 if (__prev->_member) \
469 __prev->_member = __e->_member; \
470 } \
471 nt_list_init(__e, _member); \
472 } while(0)
473
474/**
475 * DO NOT USE THIS.
476 * This is a remainder of the xfree86 DDX attempt of having a set of generic
477 * list functions. Unfortunately, the xf86OptionRec uses it and we can't
478 * easily get rid of it. Do not use for new code.
479 */
480typedef struct generic_list_rec
481{
482 void *next;
483}
484GenericListRec, *GenericListPtr, *glp;
485
486#endif
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

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