Move un-namespaced container_of into private header
[profile/ivi/wayland.git] / src / wayland-util.h
1 /*
2  * Copyright © 2008 Kristian Høgsberg
3  *
4  * Permission to use, copy, modify, distribute, and sell this software and its
5  * documentation for any purpose is hereby granted without fee, provided that
6  * the above copyright notice appear in all copies and that both that copyright
7  * notice and this permission notice appear in supporting documentation, and
8  * that the name of the copyright holders not be used in advertising or
9  * publicity pertaining to distribution of the software without specific,
10  * written prior permission.  The copyright holders make no representations
11  * about the suitability of this software for any purpose.  It is provided "as
12  * is" without express or implied warranty.
13  *
14  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16  * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
20  * OF THIS SOFTWARE.
21  */
22
23 #ifndef WAYLAND_UTIL_H
24 #define WAYLAND_UTIL_H
25
26 #ifdef  __cplusplus
27 extern "C" {
28 #endif
29
30 #include <math.h>
31 #include <stddef.h>
32 #include <inttypes.h>
33 #include <stdarg.h>
34
35 /* GCC visibility */
36 #if defined(__GNUC__) && __GNUC__ >= 4
37 #define WL_EXPORT __attribute__ ((visibility("default")))
38 #else
39 #define WL_EXPORT
40 #endif
41
42 struct wl_message {
43         const char *name;
44         const char *signature;
45         const struct wl_interface **types;
46 };
47
48 struct wl_interface {
49         const char *name;
50         int version;
51         int method_count;
52         const struct wl_message *methods;
53         int event_count;
54         const struct wl_message *events;
55 };
56
57 struct wl_object {
58         const struct wl_interface *interface;
59         void (* const * implementation)(void);
60         uint32_t id;
61 };
62
63 /**
64  * wl_list - linked list
65  *
66  * The list head is of "struct wl_list" type, and must be initialized
67  * using wl_list_init().  All entries in the list must be of the same
68  * type.  The item type must have a "struct wl_list" member. This
69  * member will be initialized by wl_list_insert(). There is no need to
70  * call wl_list_init() on the individual item. To query if the list is
71  * empty in O(1), use wl_list_empty().
72  *
73  * Let's call the list reference "struct wl_list foo_list", the item type as
74  * "item_t", and the item member as "struct wl_list link".
75  *
76  * The following code will initialize a list:
77  *
78  *      struct wl_list foo_list;
79  *
80  *      struct item_t {
81  *              int foo;
82  *              struct wl_list link;
83  *      };
84  *      struct item_t item1, item2, item3;
85  *
86  *      wl_list_init(&foo_list);
87  *      wl_list_insert(&foo_list, &item1.link); Pushes item1 at the head
88  *      wl_list_insert(&foo_list, &item2.link); Pushes item2 at the head
89  *      wl_list_insert(&item2.link, &item3.link); Pushes item3 after item2
90  *
91  * The list now looks like [item2, item3, item1]
92  *
93  * Will iterate the list in ascending order:
94  *
95  *      item_t *item;
96  *      wl_list_for_each(item, foo_list, link) {
97  *              Do_something_with_item(item);
98  *      }
99  */
100 struct wl_list {
101         struct wl_list *prev;
102         struct wl_list *next;
103 };
104
105 void wl_list_init(struct wl_list *list);
106 void wl_list_insert(struct wl_list *list, struct wl_list *elm);
107 void wl_list_remove(struct wl_list *elm);
108 int wl_list_length(struct wl_list *list);
109 int wl_list_empty(struct wl_list *list);
110 void wl_list_insert_list(struct wl_list *list, struct wl_list *other);
111
112 #ifdef __GNUC__
113 #define __wl_container_of(ptr, sample, member)                          \
114         (__typeof__(sample))((char *)(ptr)      -                       \
115                  ((char *)&(sample)->member - (char *)(sample)))
116 #else
117 #define __wl_container_of(ptr, sample, member)                          \
118         (void *)((char *)(ptr)  -                                       \
119                  ((char *)&(sample)->member - (char *)(sample)))
120 #endif
121
122 #define wl_list_for_each(pos, head, member)                             \
123         for (pos = 0, pos = __wl_container_of((head)->next, pos, member);       \
124              &pos->member != (head);                                    \
125              pos = __wl_container_of(pos->member.next, pos, member))
126
127 #define wl_list_for_each_safe(pos, tmp, head, member)                   \
128         for (pos = 0, tmp = 0,                                          \
129              pos = __wl_container_of((head)->next, pos, member),                \
130              tmp = __wl_container_of((pos)->member.next, tmp, member);  \
131              &pos->member != (head);                                    \
132              pos = tmp,                                                 \
133              tmp = __wl_container_of(pos->member.next, tmp, member))
134
135 #define wl_list_for_each_reverse(pos, head, member)                     \
136         for (pos = 0, pos = __wl_container_of((head)->prev, pos, member);       \
137              &pos->member != (head);                                    \
138              pos = __wl_container_of(pos->member.prev, pos, member))
139
140 #define wl_list_for_each_reverse_safe(pos, tmp, head, member)           \
141         for (pos = 0, tmp = 0,                                          \
142              pos = __wl_container_of((head)->prev, pos, member),        \
143              tmp = __wl_container_of((pos)->member.prev, tmp, member);  \
144              &pos->member != (head);                                    \
145              pos = tmp,                                                 \
146              tmp = __wl_container_of(pos->member.prev, tmp, member))
147
148 struct wl_array {
149         size_t size;
150         size_t alloc;
151         void *data;
152 };
153
154 #define wl_array_for_each(pos, array)                                   \
155         for (pos = (array)->data;                                       \
156              (const char *) pos < ((const char *) (array)->data + (array)->size); \
157              (pos)++)
158
159 void wl_array_init(struct wl_array *array);
160 void wl_array_release(struct wl_array *array);
161 void *wl_array_add(struct wl_array *array, size_t size);
162 int wl_array_copy(struct wl_array *array, struct wl_array *source);
163
164 typedef int32_t wl_fixed_t;
165
166 static inline double
167 wl_fixed_to_double (wl_fixed_t f)
168 {
169         union {
170                 double d;
171                 int64_t i;
172         } u;
173
174         u.i = ((1023LL + 44LL) << 52) + (1LL << 51) + f;
175
176         return u.d - (3LL << 43);
177 }
178
179 static inline wl_fixed_t
180 wl_fixed_from_double(double d)
181 {
182         union {
183                 double d;
184                 int64_t i;
185         } u;
186
187         u.d = d + (3LL << (51 - 8));
188
189         return u.i;
190 }
191
192 static inline int wl_fixed_to_int(wl_fixed_t f)
193 {
194         return f / 256;
195 }
196 static inline wl_fixed_t wl_fixed_from_int(int i)
197 {
198         return i * 256;
199 }
200
201 typedef void (*wl_log_func_t)(const char *, va_list);
202
203 #ifdef  __cplusplus
204 }
205 #endif
206
207 #endif