2 * Copyright © 2008-2011 Kristian Høgsberg
3 * Copyright © 2011 Intel Corporation
5 * Permission to use, copy, modify, distribute, and sell this software and its
6 * documentation for any purpose is hereby granted without fee, provided that
7 * the above copyright notice appear in all copies and that both that copyright
8 * notice and this permission notice appear in supporting documentation, and
9 * that the name of the copyright holders not be used in advertising or
10 * publicity pertaining to distribution of the software without specific,
11 * written prior permission. The copyright holders make no representations
12 * about the suitability of this software for any purpose. It is provided "as
13 * is" without express or implied warranty.
15 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
16 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
17 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
18 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
19 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
20 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
29 #include "wayland-util.h"
30 #include "wayland-private.h"
33 wl_list_init(struct wl_list *list)
40 wl_list_insert(struct wl_list *list, struct wl_list *elm)
43 elm->next = list->next;
45 elm->next->prev = elm;
49 wl_list_remove(struct wl_list *elm)
51 elm->prev->next = elm->next;
52 elm->next->prev = elm->prev;
58 wl_list_length(struct wl_list *list)
74 wl_list_empty(struct wl_list *list)
76 return list->next == list;
80 wl_list_insert_list(struct wl_list *list, struct wl_list *other)
82 if (wl_list_empty(other))
85 other->next->prev = list;
86 other->prev->next = list->next;
87 list->next->prev = other->prev;
88 list->next = other->next;
92 wl_array_init(struct wl_array *array)
94 memset(array, 0, sizeof *array);
98 wl_array_release(struct wl_array *array)
104 wl_array_add(struct wl_array *array, size_t size)
109 if (array->alloc > 0)
110 alloc = array->alloc;
114 while (alloc < array->size + size)
117 if (array->alloc < alloc) {
118 if (array->alloc > 0)
119 data = realloc(array->data, alloc);
121 data = malloc(alloc);
126 array->alloc = alloc;
129 p = array->data + array->size;
136 wl_array_copy(struct wl_array *array, struct wl_array *source)
139 wl_array_add(array, source->size);
140 memcpy(array->data, source->data, source->size);
149 wl_map_init(struct wl_map *map)
151 memset(map, 0, sizeof *map);
155 wl_map_release(struct wl_map *map)
157 wl_array_release(&map->client_entries);
158 wl_array_release(&map->server_entries);
162 wl_map_insert_new(struct wl_map *map, uint32_t side, void *data)
164 union map_entry *start, *entry;
165 struct wl_array *entries;
168 if (side == WL_MAP_CLIENT_SIDE) {
169 entries = &map->client_entries;
172 entries = &map->server_entries;
173 base = WL_SERVER_ID_START;
176 if (map->free_list) {
177 start = entries->data;
178 entry = &start[map->free_list >> 1];
179 map->free_list = entry->next;
181 entry = wl_array_add(entries, sizeof *entry);
182 start = entries->data;
187 return (entry - start) + base;
191 wl_map_insert_at(struct wl_map *map, uint32_t i, void *data)
193 union map_entry *start;
195 struct wl_array *entries;
197 if (i < WL_SERVER_ID_START) {
198 entries = &map->client_entries;
200 entries = &map->server_entries;
201 i -= WL_SERVER_ID_START;
204 count = entries->size / sizeof *start;
209 wl_array_add(entries, sizeof *start);
211 start = entries->data;
212 start[i].data = data;
218 wl_map_reserve_new(struct wl_map *map, uint32_t i)
220 union map_entry *start;
222 struct wl_array *entries;
224 if (i < WL_SERVER_ID_START) {
225 entries = &map->client_entries;
227 entries = &map->server_entries;
228 i -= WL_SERVER_ID_START;
231 count = entries->size / sizeof *start;
237 wl_array_add(entries, sizeof *start);
238 start = entries->data;
239 start[i].data = NULL;
241 start = entries->data;
242 if (start[i].data != NULL) {
251 wl_map_remove(struct wl_map *map, uint32_t i)
253 union map_entry *start;
254 struct wl_array *entries;
256 if (i < WL_SERVER_ID_START) {
257 entries = &map->client_entries;
259 entries = &map->server_entries;
260 i -= WL_SERVER_ID_START;
263 start = entries->data;
264 start[i].next = map->free_list;
265 map->free_list = (i << 1) | 1;
269 wl_map_lookup(struct wl_map *map, uint32_t i)
271 union map_entry *start;
273 struct wl_array *entries;
275 if (i < WL_SERVER_ID_START) {
276 entries = &map->client_entries;
278 entries = &map->server_entries;
279 i -= WL_SERVER_ID_START;
282 start = entries->data;
283 count = entries->size / sizeof *start;
285 if (i < count && !(start[i].next & 1))
286 return start[i].data;
292 for_each_helper(struct wl_array *entries, wl_iterator_func_t func, void *data)
294 union map_entry *start, *end, *p;
296 start = entries->data;
297 end = (union map_entry *) ((char *) entries->data + entries->size);
299 for (p = start; p < end; p++)
300 if (p->data && !(p->next & 1))
305 wl_map_for_each(struct wl_map *map, wl_iterator_func_t func, void *data)
307 for_each_helper(&map->client_entries, func, data);
308 for_each_helper(&map->server_entries, func, data);
312 wl_log_noop_handler(const char *fmt, va_list arg)
316 wl_log_func_t wl_log_handler = wl_log_noop_handler;
319 wl_log(const char *fmt, ...)
324 wl_log_handler(fmt, argp);