scanner: Output type info for new_id arguments
[profile/ivi/wayland.git] / wayland / 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 <inttypes.h>
31
32 /* GCC visibility */
33 #if defined(__GNUC__) && __GNUC__ >= 4
34 #define WL_EXPORT __attribute__ ((visibility("default")))
35 #else
36 #define WL_EXPORT
37 #endif
38
39 #define ARRAY_LENGTH(a) (sizeof (a) / sizeof (a)[0])
40 #define ALIGN(n, a) ( ((n) + ((a) - 1)) & ~((a) - 1) )
41 #define DIV_ROUNDUP(n, a) ( ((n) + ((a) - 1)) / (a) )
42
43 #define container_of(ptr, type, member) ({                      \
44         const typeof( ((type *)0)->member ) *__mptr = (ptr);    \
45         (type *)( (char *)__mptr - offsetof(type,member) );})
46
47 struct wl_message {
48         const char *name;
49         const char *signature;
50         const struct wl_interface **types;
51 };
52
53 struct wl_interface {
54         const char *name;
55         int version;
56         int method_count;
57         const struct wl_message *methods;
58         int event_count;
59         const struct wl_message *events;
60 };
61
62 struct wl_object {
63         const struct wl_interface *interface;
64         void (**implementation)(void);
65         uint32_t id;
66 };
67
68 struct wl_hash_table;
69 struct wl_hash_table *wl_hash_table_create(void);
70 void wl_hash_table_destroy(struct wl_hash_table *ht);
71 void *wl_hash_table_lookup(struct wl_hash_table *ht, uint32_t hash);
72 int wl_hash_table_insert(struct wl_hash_table *ht, uint32_t hash, void *data);
73 void wl_hash_table_remove(struct wl_hash_table *ht, uint32_t hash);
74
75 /**
76  * wl_list - linked list
77  *
78  * The list head is of "struct wl_list" type, and must be initialized
79  * using wl_list_init().  All entries in the list must be of the same
80  * type.  The item type must have a "struct wl_list" member. This
81  * member will be initialized by wl_list_insert(). There is no need to
82  * call wl_list_init() on the individual item. To query if the list is
83  * empty in O(1), use wl_list_empty().
84  *
85  * Let's call the list reference "struct wl_list foo_list", the item type as
86  * "item_t", and the item member as "struct wl_list link". The following code
87  *
88  * The following code will initialize a list:
89  *
90  *      wl_list_init(foo_list);
91  *      wl_list_insert(foo_list, item1);        Pushes item1 at the head
92  *      wl_list_insert(foo_list, item2);        Pushes item2 at the head
93  *      wl_list_insert(item2, item3);           Pushes item3 after item2
94  *
95  * The list now looks like [item2, item3, item1]
96  *
97  * Will iterate the list in ascending order:
98  *
99  *      item_t *item;
100  *      wl_list_for_each(item, foo_list, link) {
101  *              Do_something_with_item(item);
102  *      }
103  */
104 struct wl_list {
105         struct wl_list *prev;
106         struct wl_list *next;
107 };
108
109 void wl_list_init(struct wl_list *list);
110 void wl_list_insert(struct wl_list *list, struct wl_list *elm);
111 void wl_list_remove(struct wl_list *elm);
112 int wl_list_length(struct wl_list *list);
113 int wl_list_empty(struct wl_list *list);
114
115 #define __container_of(ptr, sample, member)                             \
116         (void *)((char *)(ptr)  -                                       \
117                  ((char *)&(sample)->member - (char *)(sample)))
118
119 #define wl_list_for_each(pos, head, member)                             \
120         for (pos = 0, pos = __container_of((head)->next, pos, member);  \
121              &pos->member != (head);                                    \
122              pos = __container_of(pos->member.next, pos, member))
123
124 #define wl_list_for_each_safe(pos, tmp, head, member)                   \
125         for (pos = 0, tmp = 0,                                          \
126              pos = __container_of((head)->next, pos, member),           \
127              tmp = __container_of((pos)->member.next, tmp, member);     \
128              &pos->member != (head);                                    \
129              pos = tmp,                                                 \
130              tmp = __container_of(pos->member.next, tmp, member))
131
132 #define wl_list_for_each_reverse(pos, head, member)                     \
133         for (pos = 0, pos = __container_of((head)->prev, pos, member);  \
134              &pos->member != (head);                                    \
135              pos = __container_of(pos->member.prev, pos, member))
136
137 struct wl_array {
138         uint32_t size;
139         uint32_t alloc;
140         void *data;
141 };
142
143 void wl_array_init(struct wl_array *array);
144 void wl_array_release(struct wl_array *array);
145 void *wl_array_add(struct wl_array *array, int size);
146 void wl_array_copy(struct wl_array *array, struct wl_array *source);
147
148 #ifdef  __cplusplus
149 }
150 #endif
151
152 #endif