2 * Copyright © 2008 Kristian Høgsberg
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.
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
23 #ifndef WAYLAND_UTIL_H
24 #define WAYLAND_UTIL_H
33 #if defined(__GNUC__) && __GNUC__ >= 4
34 #define WL_EXPORT __attribute__ ((visibility("default")))
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) )
43 #define container_of(ptr, type, member) ({ \
44 const typeof( ((type *)0)->member ) *__mptr = (ptr); \
45 (type *)( (char *)__mptr - offsetof(type,member) );})
49 const char *signature;
50 const struct wl_interface **types;
57 const struct wl_message *methods;
59 const struct wl_message *events;
63 const struct wl_interface *interface;
64 void (**implementation)(void);
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);
76 * wl_list - linked list
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().
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
88 * The following code will initialize a list:
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
95 * The list now looks like [item2, item3, item1]
97 * Will iterate the list in ascending order:
100 * wl_list_for_each(item, foo_list, link) {
101 * Do_something_with_item(item);
105 struct wl_list *prev;
106 struct wl_list *next;
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);
115 #define __container_of(ptr, sample, member) \
116 (void *)((char *)(ptr) - \
117 ((char *)&(sample)->member - (char *)(sample)))
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))
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); \
130 tmp = __container_of(pos->member.next, tmp, member))
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))
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);