Remove xproto build dependency
[platform/upstream/libxkbcommon.git] / src / list.h
1 /*
2  * Copyright © 2010 Intel Corporation
3  * Copyright © 2010 Francisco Jerez <currojerez@riseup.net>
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  * 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 list').
45  *
46  *     struct bar {
47  *          ...
48  *          struct list list_of_foos;
49  *          ...
50  *     }
51  *
52  *     struct foo {
53  *          ...
54  *          struct list entry;
55  *          ...
56  *     }
57  *
58  * Now we initialize the list head:
59  *
60  *     struct bar bar;
61  *     ...
62  *     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  *     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  *      list_del(&foo->entry);
73  *      free(foo);
74  *
75  * Note: calling 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  * list_foreach(iterator, &bar.list_of_foos, entry) {
83  *      if (iterator->something == ...)
84  *             ...
85  * }
86  *
87  * Note: You must not call 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  * list_foreach_safe(iterator, next, &bar.list_of_foos, entry) {
92  *      if (...)
93  *              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 list is required for both the head of the
101  * list and for each list node.
102  *
103  * Position and name of the struct 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 list can be a list
106  * head.
107  */
108 struct list {
109     struct list *next, *prev;
110 };
111
112 /**
113  * Initialize the list as an empty list.
114  *
115  * Example:
116  * list_init(&bar->list_of_foos);
117  *
118  * @param The list to initialized.
119  */
120 static void
121 list_init(struct list *list)
122 {
123     list->next = list->prev = list;
124 }
125
126 static inline void
127 __list_add(struct list *entry,
128                 struct list *prev, struct list *next)
129 {
130     next->prev = entry;
131     entry->next = next;
132     entry->prev = prev;
133     prev->next = entry;
134 }
135
136 /**
137  * Insert a new element after the given list head. The new element does not
138  * need to be initialised as empty list.
139  * The list changes from:
140  *      head → some element → ...
141  * to
142  *      head → new element → older element → ...
143  *
144  * Example:
145  * struct foo *newfoo = malloc(...);
146  * list_add(&newfoo->entry, &bar->list_of_foos);
147  *
148  * @param entry The new element to prepend to the list.
149  * @param head The existing list.
150  */
151 static inline void
152 list_add(struct list *entry, struct list *head)
153 {
154     __list_add(entry, head, head->next);
155 }
156
157 /**
158  * Append a new element to the end of the list given with this list head.
159  *
160  * The list changes from:
161  *      head → some element → ... → lastelement
162  * to
163  *      head → some element → ... → lastelement → new element
164  *
165  * Example:
166  * struct foo *newfoo = malloc(...);
167  * list_append(&newfoo->entry, &bar->list_of_foos);
168  *
169  * @param entry The new element to prepend to the list.
170  * @param head The existing list.
171  */
172 static inline void
173 list_append(struct list *entry, struct list *head)
174 {
175     __list_add(entry, head->prev, head);
176 }
177
178 static inline void
179 __list_del(struct list *prev, struct list *next)
180 {
181     next->prev = prev;
182     prev->next = next;
183 }
184
185 /**
186  * Remove the element from the list it is in. Using this function will reset
187  * the pointers to/from this element so it is removed from the list. It does
188  * NOT free the element itself or manipulate it otherwise.
189  *
190  * Using list_del on a pure list head (like in the example at the top of
191  * this file) will NOT remove the first element from
192  * the list but rather reset the list as empty list.
193  *
194  * Example:
195  * list_del(&foo->entry);
196  *
197  * @param entry The element to remove.
198  */
199 static inline void
200 list_del(struct list *entry)
201 {
202     __list_del(entry->prev, entry->next);
203     list_init(entry);
204 }
205
206 /**
207  * Check if the list is empty.
208  *
209  * Example:
210  * list_empty(&bar->list_of_foos);
211  *
212  * @return True if the list contains one or more elements or False otherwise.
213  */
214 static inline bool
215 list_empty(struct list *head)
216 {
217     return head->next == head;
218 }
219
220 /**
221  * Replace a list element by another one. This can also be used to replace
222  * the head of an existing list by another list head.
223  *
224  * Example:
225  * list_replace(&object_foo->entry, &object_bar->entry);
226  * list_replace(&from->list_head, &into->list_head);
227  *
228  * @param old The element being replaced.
229  * @param new The element to replace @old with.
230  */
231 static inline void
232 list_replace(struct list *old, struct list *new)
233 {
234     new->next = old->next;
235     new->next->prev = new;
236     new->prev = old->prev;
237     new->prev->next = new;
238 }
239
240 /**
241  * Returns a pointer to the container of this list element.
242  *
243  * Example:
244  * struct foo* f;
245  * f = container_of(&foo->entry, struct foo, entry);
246  * assert(f == foo);
247  *
248  * @param ptr Pointer to the struct list.
249  * @param type Data type of the list element.
250  * @param member Member name of the struct list field in the list element.
251  * @return A pointer to the data struct containing the list head.
252  */
253 #ifndef container_of
254 #define container_of(ptr, type, member) \
255     (type *)((char *)(ptr) - (char *) &((type *)0)->member)
256 #endif
257
258 /**
259  * Alias of container_of
260  */
261 #define list_entry(ptr, type, member) \
262     container_of(ptr, type, member)
263
264 /**
265  * Retrieve the first list entry for the given list pointer.
266  *
267  * Example:
268  * struct foo *first;
269  * first = list_first_entry(&bar->list_of_foos, struct foo, list_of_foos);
270  *
271  * @param ptr The list head
272  * @param type Data type of the list element to retrieve
273  * @param member Member name of the struct list field in the list element.
274  * @return A pointer to the first list element.
275  */
276 #define list_first_entry(ptr, type, member) \
277     list_entry((ptr)->next, type, member)
278
279 /**
280  * Retrieve the last list entry for the given listpointer.
281  *
282  * Example:
283  * struct foo *first;
284  * first = list_last_entry(&bar->list_of_foos, struct foo, list_of_foos);
285  *
286  * @param ptr The list head
287  * @param type Data type of the list element to retrieve
288  * @param member Member name of the struct list field in the list element.
289  * @return A pointer to the last list element.
290  */
291 #define list_last_entry(ptr, type, member) \
292     list_entry((ptr)->prev, type, member)
293
294 #define __container_of(ptr, sample, member)                             \
295     (void *)((char *)(ptr)                                              \
296              - offsetof(__typeof(*sample), member))
297 /**
298  * Loop through the list given by head and set pos to struct in the list.
299  *
300  * Example:
301  * struct foo *iterator;
302  * list_foreach(iterator, &bar->list_of_foos, entry) {
303  *      [modify iterator]
304  * }
305  *
306  * This macro is not safe for node deletion. Use list_foreach_safe
307  * instead.
308  *
309  * @param pos Iterator variable of the type of the list elements.
310  * @param head List head
311  * @param member Member name of the struct list in the list elements.
312  *
313  */
314 #define list_foreach(pos, head, member)                                 \
315     for (pos = __container_of((head)->next, pos, member);               \
316          &pos->member != (head);                                        \
317          pos = __container_of(pos->member.next, pos, member))
318
319 /**
320  * Loop through the list, keeping a backup pointer to the element. This
321  * macro allows for the deletion of a list element while looping through the
322  * list.
323  *
324  * See list_foreach for more details.
325  */
326 #define list_foreach_safe(pos, tmp, head, member)                       \
327     for (pos = __container_of((head)->next, pos, member),               \
328          tmp = __container_of(pos->member.next, pos, member);           \
329          &pos->member != (head);                                        \
330          pos = tmp, tmp = __container_of(pos->member.next, tmp, member))
331
332 /* NULL-Terminated List Interface
333  *
334  * The interface below does _not_ use the struct list as described above.
335  * It is mainly for legacy structures that cannot easily be switched to
336  * struct list.
337  *
338  * This interface is for structs like
339  *      struct foo {
340  *          [...]
341  *          struct foo *next;
342  *           [...]
343  *      };
344  *
345  * The position and field name of "next" are arbitrary.
346  */
347
348 /**
349  * Init the element as null-terminated list.
350  *
351  * Example:
352  * struct foo *list = malloc();
353  * nt_list_init(list, next);
354  *
355  * @param list The list element that will be the start of the list
356  * @param member Member name of the field pointing to next struct
357  */
358 #define nt_list_init(_list, _member) \
359         (_list)->_member = NULL
360
361 /**
362  * Returns the next element in the list or NULL on termination.
363  *
364  * Example:
365  * struct foo *element = list;
366  * while ((element = nt_list_next(element, next)) { }
367  *
368  * This macro is not safe for node deletion. Use list_foreach_safe
369  * instead.
370  *
371  * @param list The list or current element.
372  * @param member Member name of the field pointing to next struct.
373  */
374 #define nt_list_next(_list, _member) \
375         (_list)->_member
376
377 /**
378  * Iterate through each element in the list.
379  *
380  * Example:
381  * struct foo *iterator;
382  * nt_list_foreach(iterator, list, next) {
383  *      [modify iterator]
384  * }
385  *
386  * @param entry Assigned to the current list element
387  * @param list The list to iterate through.
388  * @param member Member name of the field pointing to next struct.
389  */
390 #define nt_list_foreach(_entry, _list, _member)                 \
391         for (_entry = _list; _entry; _entry = (_entry)->_member)
392
393 /**
394  * Iterate through each element in the list, keeping a backup pointer to the
395  * element. This macro allows for the deletion of a list element while
396  * looping through the list.
397  *
398  * See nt_list_foreach for more details.
399  *
400  * @param entry Assigned to the current list element
401  * @param tmp The pointer to the next element
402  * @param list The list to iterate through.
403  * @param member Member name of the field pointing to next struct.
404  */
405 #define nt_list_foreach_safe(_entry, _tmp, _list, _member)      \
406         for (_entry = _list, _tmp = (_entry) ? (_entry)->_member : NULL;\
407                 _entry;                                                 \
408                 _entry = _tmp, _tmp = (_tmp) ? (_tmp)->_member: NULL)
409
410 /**
411  * Append the element to the end of the list. This macro may be used to
412  * merge two lists.
413  *
414  * Example:
415  * struct foo *elem = malloc(...);
416  * nt_list_init(elem, next)
417  * nt_list_append(elem, list, struct foo, next);
418  *
419  * Resulting list order:
420  * list_item_0 -> list_item_1 -> ... -> elem_item_0 -> elem_item_1 ...
421  *
422  * @param entry An entry (or list) to append to the list
423  * @param list The list to append to. This list must be a valid list, not
424  * NULL.
425  * @param type The list type
426  * @param member Member name of the field pointing to next struct
427  */
428 #define nt_list_append(_entry, _list, _type, _member)                   \
429     do {                                                                \
430         _type *__iterator = _list;                                      \
431         while (__iterator->_member) { __iterator = __iterator->_member;}\
432         __iterator->_member = _entry;                                   \
433     } while (0)
434
435 /**
436  * Insert the element at the next position in the list. This macro may be
437  * used to insert a list into a list.
438  *
439  * struct foo *elem = malloc(...);
440  * nt_list_init(elem, next)
441  * nt_list_insert(elem, list, struct foo, next);
442  *
443  * Resulting list order:
444  * list_item_0 -> elem_item_0 -> elem_item_1 ... -> list_item_1 -> ...
445  *
446  * @param entry An entry (or list) to append to the list
447  * @param list The list to insert to. This list must be a valid list, not
448  * NULL.
449  * @param type The list type
450  * @param member Member name of the field pointing to next struct
451  */
452 #define nt_list_insert(_entry, _list, _type, _member)                   \
453     do {                                                                \
454         nt_list_append((_list)->_member, _entry, _type, _member);       \
455         (_list)->_member = _entry;                                      \
456     } while (0)
457
458 /**
459  * Delete the entry from the list by iterating through the list and
460  * removing any reference from the list to the entry.
461  *
462  * Example:
463  * struct foo *elem = <assign to right element>
464  * nt_list_del(elem, list, struct foo, next);
465  *
466  * @param entry The entry to delete from the list. entry is always
467  * re-initialized as a null-terminated list.
468  * @param list The list containing the entry, set to the new list without
469  * the removed entry.
470  * @param type The list type
471  * @param member Member name of the field pointing to the next entry
472  */
473 #define nt_list_del(_entry, _list, _type, _member)              \
474         do {                                                    \
475                 _type *__e = _entry;                            \
476                 if (__e == NULL || _list == NULL) break;        \
477                 if ((_list) == __e) {                           \
478                     _list = __e->_member;                       \
479                 } else {                                        \
480                     _type *__prev = _list;                      \
481                     while (__prev->_member && __prev->_member != __e)   \
482                         __prev = nt_list_next(__prev, _member); \
483                     if (__prev->_member)                        \
484                         __prev->_member = __e->_member;         \
485                 }                                               \
486                 nt_list_init(__e, _member);                     \
487         } while(0)
488
489 #endif