1 /*
2 * Copyright © 2008-2011 Kristian Høgsberg
3 * Copyright © 2011 Intel Corporation
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial
15 * portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
21 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
22 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 * SOFTWARE.
25 */
26
27 #include <stdlib.h>
28 #include <stdint.h>
29 #include <stdio.h>
30 #include <string.h>
31 #include <stdarg.h>
32
33 #include "wayland-util.h"
34 #include "wayland-private.h"
35
36 WL_EXPORT void
wl_list_init(struct wl_list * list)37 wl_list_init(struct wl_list *list)
38 {
39 list->prev = list;
40 list->next = list;
41 }
42
43 WL_EXPORT void
wl_list_insert(struct wl_list * list,struct wl_list * elm)44 wl_list_insert(struct wl_list *list, struct wl_list *elm)
45 {
46 elm->prev = list;
47 elm->next = list->next;
48 list->next = elm;
49 elm->next->prev = elm;
50 }
51
52 WL_EXPORT void
wl_list_remove(struct wl_list * elm)53 wl_list_remove(struct wl_list *elm)
54 {
55 elm->prev->next = elm->next;
56 elm->next->prev = elm->prev;
57 elm->next = NULL;
58 elm->prev = NULL;
59 }
60
61 WL_EXPORT int
wl_list_length(const struct wl_list * list)62 wl_list_length(const struct wl_list *list)
63 {
64 struct wl_list *e;
65 int count;
66
67 count = 0;
68 e = list->next;
69 while (e != list) {
70 e = e->next;
71 count++;
72 }
73
74 return count;
75 }
76
77 WL_EXPORT int
wl_list_empty(const struct wl_list * list)78 wl_list_empty(const struct wl_list *list)
79 {
80 return list->next == list;
81 }
82
83 WL_EXPORT void
wl_list_insert_list(struct wl_list * list,struct wl_list * other)84 wl_list_insert_list(struct wl_list *list, struct wl_list *other)
85 {
86 if (wl_list_empty(other))
87 return;
88
89 other->next->prev = list;
90 other->prev->next = list->next;
91 list->next->prev = other->prev;
92 list->next = other->next;
93 }
94
95 WL_EXPORT void
wl_array_init(struct wl_array * array)96 wl_array_init(struct wl_array *array)
97 {
98 memset(array, 0, sizeof *array);
99 }
100
101 WL_EXPORT void
wl_array_release(struct wl_array * array)102 wl_array_release(struct wl_array *array)
103 {
104 free(array->data);
105 array->data = WL_ARRAY_POISON_PTR;
106 }
107
108 WL_EXPORT void *
wl_array_add(struct wl_array * array,size_t size)109 wl_array_add(struct wl_array *array, size_t size)
110 {
111 size_t alloc;
112 void *data, *p;
113
114 if (array->alloc > 0)
115 alloc = array->alloc;
116 else
117 alloc = 16;
118
119 while (alloc < array->size + size)
120 alloc *= 2;
121
122 if (array->alloc < alloc) {
123 if (array->alloc > 0)
124 data = realloc(array->data, alloc);
125 else
126 data = malloc(alloc);
127
128 if (data == NULL)
129 return NULL;
130 array->data = data;
131 array->alloc = alloc;
132 }
133
134 p = array->data + array->size;
135 array->size += size;
136
137 return p;
138 }
139
140 WL_EXPORT int
wl_array_copy(struct wl_array * array,struct wl_array * source)141 wl_array_copy(struct wl_array *array, struct wl_array *source)
142 {
143 if (array->size < source->size) {
144 if (!wl_array_add(array, source->size - array->size))
145 return -1;
146 } else {
147 array->size = source->size;
148 }
149
150 memcpy(array->data, source->data, source->size);
151 return 0;
152 }
153
154 /** \cond */
155
156 int
wl_interface_equal(const struct wl_interface * a,const struct wl_interface * b)157 wl_interface_equal(const struct wl_interface *a, const struct wl_interface *b)
158 {
159 /* In most cases the pointer equality test is sufficient.
160 * However, in some cases, depending on how things are split
161 * across shared objects, we can end up with multiple
162 * instances of the interface metadata constants. So if the
163 * pointers match, the interfaces are equal, if they don't
164 * match we have to compare the interface names.
165 */
166 return a == b || strcmp(a->name, b->name) == 0;
167 }
168
169 union map_entry {
170 uintptr_t next;
171 void *data;
172 };
173
174 #define map_entry_is_free(entry) ((entry).next & 0x1)
175 #define map_entry_get_data(entry) ((void *)((entry).next & ~(uintptr_t)0x3))
176 #define map_entry_get_flags(entry) (((entry).next >> 1) & 0x1)
177
178 void
wl_map_init(struct wl_map * map,uint32_t side)179 wl_map_init(struct wl_map *map, uint32_t side)
180 {
181 memset(map, 0, sizeof *map);
182 map->side = side;
183 }
184
185 void
wl_map_release(struct wl_map * map)186 wl_map_release(struct wl_map *map)
187 {
188 wl_array_release(&map->client_entries);
189 wl_array_release(&map->server_entries);
190 }
191
192 uint32_t
wl_map_insert_new(struct wl_map * map,uint32_t flags,void * data)193 wl_map_insert_new(struct wl_map *map, uint32_t flags, void *data)
194 {
195 union map_entry *start, *entry;
196 struct wl_array *entries;
197 uint32_t base;
198
199 if (map->side == WL_MAP_CLIENT_SIDE) {
200 entries = &map->client_entries;
201 base = 0;
202 } else {
203 entries = &map->server_entries;
204 base = WL_SERVER_ID_START;
205 }
206
207 if (map->free_list) {
208 start = entries->data;
209 entry = &start[map->free_list >> 1];
210 map->free_list = entry->next;
211 } else {
212 entry = wl_array_add(entries, sizeof *entry);
213 if (!entry)
214 return 0;
215 start = entries->data;
216 }
217
218 entry->data = data;
219 entry->next |= (flags & 0x1) << 1;
220
221 return (entry - start) + base;
222 }
223
224 int
wl_map_insert_at(struct wl_map * map,uint32_t flags,uint32_t i,void * data)225 wl_map_insert_at(struct wl_map *map, uint32_t flags, uint32_t i, void *data)
226 {
227 union map_entry *start;
228 uint32_t count;
229 struct wl_array *entries;
230
231 if (i < WL_SERVER_ID_START) {
232 entries = &map->client_entries;
233 } else {
234 entries = &map->server_entries;
235 i -= WL_SERVER_ID_START;
236 }
237
238 count = entries->size / sizeof *start;
239 if (count < i)
240 return -1;
241
242 if (count == i)
243 wl_array_add(entries, sizeof *start);
244
245 start = entries->data;
246 start[i].data = data;
247 start[i].next |= (flags & 0x1) << 1;
248
249 return 0;
250 }
251
252 int
wl_map_reserve_new(struct wl_map * map,uint32_t i)253 wl_map_reserve_new(struct wl_map *map, uint32_t i)
254 {
255 union map_entry *start;
256 uint32_t count;
257 struct wl_array *entries;
258
259 if (i < WL_SERVER_ID_START) {
260 if (map->side == WL_MAP_CLIENT_SIDE)
261 return -1;
262
263 entries = &map->client_entries;
264 } else {
265 if (map->side == WL_MAP_SERVER_SIDE)
266 return -1;
267
268 entries = &map->server_entries;
269 i -= WL_SERVER_ID_START;
270 }
271
272 count = entries->size / sizeof *start;
273
274 if (count < i)
275 return -1;
276
277 if (count == i) {
278 wl_array_add(entries, sizeof *start);
279 start = entries->data;
280 start[i].data = NULL;
281 } else {
282 start = entries->data;
283 if (start[i].data != NULL) {
284 return -1;
285 }
286 }
287
288 return 0;
289 }
290
291 void
wl_map_remove(struct wl_map * map,uint32_t i)292 wl_map_remove(struct wl_map *map, uint32_t i)
293 {
294 union map_entry *start;
295 struct wl_array *entries;
296
297 if (i < WL_SERVER_ID_START) {
298 if (map->side == WL_MAP_SERVER_SIDE)
299 return;
300
301 entries = &map->client_entries;
302 } else {
303 if (map->side == WL_MAP_CLIENT_SIDE)
304 return;
305
306 entries = &map->server_entries;
307 i -= WL_SERVER_ID_START;
308 }
309
310 start = entries->data;
311 start[i].next = map->free_list;
312 map->free_list = (i << 1) | 1;
313 }
314
315 void *
wl_map_lookup(struct wl_map * map,uint32_t i)316 wl_map_lookup(struct wl_map *map, uint32_t i)
317 {
318 union map_entry *start;
319 uint32_t count;
320 struct wl_array *entries;
321
322 if (i < WL_SERVER_ID_START) {
323 entries = &map->client_entries;
324 } else {
325 entries = &map->server_entries;
326 i -= WL_SERVER_ID_START;
327 }
328
329 start = entries->data;
330 count = entries->size / sizeof *start;
331
332 if (i < count && !map_entry_is_free(start[i]))
333 return map_entry_get_data(start[i]);
334
335 return NULL;
336 }
337
338 uint32_t
wl_map_lookup_flags(struct wl_map * map,uint32_t i)339 wl_map_lookup_flags(struct wl_map *map, uint32_t i)
340 {
341 union map_entry *start;
342 uint32_t count;
343 struct wl_array *entries;
344
345 if (i < WL_SERVER_ID_START) {
346 entries = &map->client_entries;
347 } else {
348 entries = &map->server_entries;
349 i -= WL_SERVER_ID_START;
350 }
351
352 start = entries->data;
353 count = entries->size / sizeof *start;
354
355 if (i < count && !map_entry_is_free(start[i]))
356 return map_entry_get_flags(start[i]);
357
358 return 0;
359 }
360
361 static enum wl_iterator_result
for_each_helper(struct wl_array * entries,wl_iterator_func_t func,void * data)362 for_each_helper(struct wl_array *entries, wl_iterator_func_t func, void *data)
363 {
364 union map_entry *start, *end, *p;
365 enum wl_iterator_result ret = WL_ITERATOR_CONTINUE;
366
367 start = entries->data;
368 end = (union map_entry *) ((char *) entries->data + entries->size);
369
370 for (p = start; p < end; p++)
371 if (p->data && !map_entry_is_free(*p)) {
372 ret = func(map_entry_get_data(*p), data, map_entry_get_flags(*p));
373 if (ret != WL_ITERATOR_CONTINUE)
374 break;
375 }
376
377 return ret;
378 }
379
380 void
wl_map_for_each(struct wl_map * map,wl_iterator_func_t func,void * data)381 wl_map_for_each(struct wl_map *map, wl_iterator_func_t func, void *data)
382 {
383 enum wl_iterator_result ret;
384
385 ret = for_each_helper(&map->client_entries, func, data);
386 if (ret == WL_ITERATOR_CONTINUE)
387 for_each_helper(&map->server_entries, func, data);
388 }
389
390 static void
wl_log_stderr_handler(const char * fmt,va_list arg)391 wl_log_stderr_handler(const char *fmt, va_list arg)
392 {
393 vfprintf(stderr, fmt, arg);
394 }
395
396 wl_log_func_t wl_log_handler = wl_log_stderr_handler;
397
398 void
wl_log(const char * fmt,...)399 wl_log(const char *fmt, ...)
400 {
401 va_list argp;
402
403 va_start(argp, fmt);
404 wl_log_handler(fmt, argp);
405 va_end(argp);
406 }
407
408 void
wl_abort(const char * fmt,...)409 wl_abort(const char *fmt, ...)
410 {
411 va_list argp;
412
413 va_start(argp, fmt);
414 wl_log_handler(fmt, argp);
415 va_end(argp);
416
417 abort();
418 }
419
420 /** \endcond */
421