libdrm: NOTE! Default branch is now main
[drm/libdrm.git] / util_double_list.h
blob7e48b26cb0ce2e7e851b08a4708b217d9a437205
1 /*
3 * Copyright 2006 Tungsten Graphics, Inc., Bismarck, ND. USA.
4 * All Rights Reserved.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
18 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20 * USE OR OTHER DEALINGS IN THE SOFTWARE.
22 * The above copyright notice and this permission notice (including the
23 * next paragraph) shall be included in all copies or substantial portions
24 * of the Software.
28 /**
29 * \file
30 * List macros heavily inspired by the Linux kernel
31 * list handling. No list looping yet.
33 * Is not threadsafe, so common operations need to
34 * be protected using an external mutex.
36 #ifndef _U_DOUBLE_LIST_H_
37 #define _U_DOUBLE_LIST_H_
39 #include <stddef.h>
41 struct list_head
43 struct list_head *prev;
44 struct list_head *next;
47 static inline void list_inithead(struct list_head *item)
49 item->prev = item;
50 item->next = item;
53 static inline void list_add(struct list_head *item, struct list_head *list)
55 item->prev = list;
56 item->next = list->next;
57 list->next->prev = item;
58 list->next = item;
61 static inline void list_addtail(struct list_head *item, struct list_head *list)
63 item->next = list;
64 item->prev = list->prev;
65 list->prev->next = item;
66 list->prev = item;
69 static inline void list_replace(struct list_head *from, struct list_head *to)
71 to->prev = from->prev;
72 to->next = from->next;
73 from->next->prev = to;
74 from->prev->next = to;
77 static inline void list_del(struct list_head *item)
79 item->prev->next = item->next;
80 item->next->prev = item->prev;
83 static inline void list_delinit(struct list_head *item)
85 item->prev->next = item->next;
86 item->next->prev = item->prev;
87 item->next = item;
88 item->prev = item;
91 #define LIST_INITHEAD(__item) list_inithead(__item)
92 #define LIST_ADD(__item, __list) list_add(__item, __list)
93 #define LIST_ADDTAIL(__item, __list) list_addtail(__item, __list)
94 #define LIST_REPLACE(__from, __to) list_replace(__from, __to)
95 #define LIST_DEL(__item) list_del(__item)
96 #define LIST_DELINIT(__item) list_delinit(__item)
98 #define LIST_ENTRY(__type, __item, __field) \
99 ((__type *)(((char *)(__item)) - offsetof(__type, __field)))
101 #define LIST_FIRST_ENTRY(__ptr, __type, __field) \
102 LIST_ENTRY(__type, (__ptr)->next, __field)
104 #define LIST_LAST_ENTRY(__ptr, __type, __field) \
105 LIST_ENTRY(__type, (__ptr)->prev, __field)
107 #define LIST_IS_EMPTY(__list) \
108 ((__list)->next == (__list))
110 #ifndef container_of
111 #define container_of(ptr, sample, member) \
112 (void *)((char *)(ptr) \
113 - ((char *)&((typeof(sample))0)->member))
114 #endif
116 #define LIST_FOR_EACH_ENTRY(pos, head, member) \
117 for (pos = container_of((head)->next, pos, member); \
118 &pos->member != (head); \
119 pos = container_of(pos->member.next, pos, member))
121 #define LIST_FOR_EACH_ENTRY_SAFE(pos, storage, head, member) \
122 for (pos = container_of((head)->next, pos, member), \
123 storage = container_of(pos->member.next, pos, member); \
124 &pos->member != (head); \
125 pos = storage, storage = container_of(storage->member.next, storage, member))
127 #define LIST_FOR_EACH_ENTRY_SAFE_REV(pos, storage, head, member) \
128 for (pos = container_of((head)->prev, pos, member), \
129 storage = container_of(pos->member.prev, pos, member); \
130 &pos->member != (head); \
131 pos = storage, storage = container_of(storage->member.prev, storage, member))
133 #define LIST_FOR_EACH_ENTRY_FROM(pos, start, head, member) \
134 for (pos = container_of((start), pos, member); \
135 &pos->member != (head); \
136 pos = container_of(pos->member.next, pos, member))
138 #define LIST_FOR_EACH_ENTRY_FROM_REV(pos, start, head, member) \
139 for (pos = container_of((start), pos, member); \
140 &pos->member != (head); \
141 pos = container_of(pos->member.prev, pos, member))
143 #endif /*_U_DOUBLE_LIST_H_*/