xref: /aosp_15_r20/external/libxkbcommon/src/util-list.h (revision 2b949d0487e80d67f1fda82db69e101e761f8064)
1*2b949d04SAndroid Build Coastguard Worker /*
2*2b949d04SAndroid Build Coastguard Worker  * Copyright © 2008-2011 Kristian Høgsberg
3*2b949d04SAndroid Build Coastguard Worker  * Copyright © 2011 Intel Corporation
4*2b949d04SAndroid Build Coastguard Worker  * Copyright © 2013-2015 Red Hat, Inc.
5*2b949d04SAndroid Build Coastguard Worker  *
6*2b949d04SAndroid Build Coastguard Worker  * Permission is hereby granted, free of charge, to any person obtaining a
7*2b949d04SAndroid Build Coastguard Worker  * copy of this software and associated documentation files (the "Software"),
8*2b949d04SAndroid Build Coastguard Worker  * to deal in the Software without restriction, including without limitation
9*2b949d04SAndroid Build Coastguard Worker  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10*2b949d04SAndroid Build Coastguard Worker  * and/or sell copies of the Software, and to permit persons to whom the
11*2b949d04SAndroid Build Coastguard Worker  * Software is furnished to do so, subject to the following conditions:
12*2b949d04SAndroid Build Coastguard Worker  *
13*2b949d04SAndroid Build Coastguard Worker  * The above copyright notice and this permission notice (including the next
14*2b949d04SAndroid Build Coastguard Worker  * paragraph) shall be included in all copies or substantial portions of the
15*2b949d04SAndroid Build Coastguard Worker  * Software.
16*2b949d04SAndroid Build Coastguard Worker  *
17*2b949d04SAndroid Build Coastguard Worker  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18*2b949d04SAndroid Build Coastguard Worker  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19*2b949d04SAndroid Build Coastguard Worker  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
20*2b949d04SAndroid Build Coastguard Worker  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21*2b949d04SAndroid Build Coastguard Worker  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22*2b949d04SAndroid Build Coastguard Worker  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23*2b949d04SAndroid Build Coastguard Worker  * DEALINGS IN THE SOFTWARE.
24*2b949d04SAndroid Build Coastguard Worker  */
25*2b949d04SAndroid Build Coastguard Worker 
26*2b949d04SAndroid Build Coastguard Worker #pragma once
27*2b949d04SAndroid Build Coastguard Worker 
28*2b949d04SAndroid Build Coastguard Worker #include "config.h"
29*2b949d04SAndroid Build Coastguard Worker 
30*2b949d04SAndroid Build Coastguard Worker #include <stdbool.h>
31*2b949d04SAndroid Build Coastguard Worker #include <stddef.h>
32*2b949d04SAndroid Build Coastguard Worker 
33*2b949d04SAndroid Build Coastguard Worker /*
34*2b949d04SAndroid Build Coastguard Worker  * This list data structure is a verbatim copy from wayland-util.h from the
35*2b949d04SAndroid Build Coastguard Worker  * Wayland project; except that wl_ prefix has been removed.
36*2b949d04SAndroid Build Coastguard Worker  */
37*2b949d04SAndroid Build Coastguard Worker 
38*2b949d04SAndroid Build Coastguard Worker struct list {
39*2b949d04SAndroid Build Coastguard Worker 	struct list *prev;
40*2b949d04SAndroid Build Coastguard Worker 	struct list *next;
41*2b949d04SAndroid Build Coastguard Worker };
42*2b949d04SAndroid Build Coastguard Worker 
43*2b949d04SAndroid Build Coastguard Worker void list_init(struct list *list);
44*2b949d04SAndroid Build Coastguard Worker void list_insert(struct list *list, struct list *elm);
45*2b949d04SAndroid Build Coastguard Worker void list_append(struct list *list, struct list *elm);
46*2b949d04SAndroid Build Coastguard Worker void list_remove(struct list *elm);
47*2b949d04SAndroid Build Coastguard Worker bool list_empty(const struct list *list);
48*2b949d04SAndroid Build Coastguard Worker bool list_is_last(const struct list *list, const struct list *elm);
49*2b949d04SAndroid Build Coastguard Worker 
50*2b949d04SAndroid Build Coastguard Worker #define container_of(ptr, type, member)					\
51*2b949d04SAndroid Build Coastguard Worker 	(__typeof__(type) *)((char *)(ptr) -				\
52*2b949d04SAndroid Build Coastguard Worker 		 offsetof(__typeof__(type), member))
53*2b949d04SAndroid Build Coastguard Worker 
54*2b949d04SAndroid Build Coastguard Worker #define list_first_entry(head, pos, member)				\
55*2b949d04SAndroid Build Coastguard Worker 	container_of((head)->next, __typeof__(*pos), member)
56*2b949d04SAndroid Build Coastguard Worker 
57*2b949d04SAndroid Build Coastguard Worker #define list_last_entry(head, pos, member)				\
58*2b949d04SAndroid Build Coastguard Worker 	container_of((head)->prev, __typeof__(*pos), member)
59*2b949d04SAndroid Build Coastguard Worker 
60*2b949d04SAndroid Build Coastguard Worker #define list_for_each(pos, head, member)				\
61*2b949d04SAndroid Build Coastguard Worker 	for (pos = 0, pos = list_first_entry(head, pos, member);	\
62*2b949d04SAndroid Build Coastguard Worker 	     &pos->member != (head);					\
63*2b949d04SAndroid Build Coastguard Worker 	     pos = list_first_entry(&pos->member, pos, member))
64*2b949d04SAndroid Build Coastguard Worker 
65*2b949d04SAndroid Build Coastguard Worker #define list_for_each_safe(pos, tmp, head, member)			\
66*2b949d04SAndroid Build Coastguard Worker 	for (pos = 0, tmp = 0,						\
67*2b949d04SAndroid Build Coastguard Worker 	     pos = list_first_entry(head, pos, member),			\
68*2b949d04SAndroid Build Coastguard Worker 	     tmp = list_first_entry(&pos->member, tmp, member);		\
69*2b949d04SAndroid Build Coastguard Worker 	     &pos->member != (head);					\
70*2b949d04SAndroid Build Coastguard Worker 	     pos = tmp,							\
71*2b949d04SAndroid Build Coastguard Worker 	     tmp = list_first_entry(&pos->member, tmp, member))
72