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