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