xref: /aosp_15_r20/external/wayland/src/wayland-util.c (revision 84e872a0dc482bffdb63672969dd03a827d67c73)
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 <errno.h>
28 #include <stdlib.h>
29 #include <stdint.h>
30 #include <stdio.h>
31 #include <string.h>
32 #include <stdarg.h>
33 
34 #include "wayland-util.h"
35 #include "wayland-private.h"
36 
37 WL_EXPORT void
wl_list_init(struct wl_list * list)38 wl_list_init(struct wl_list *list)
39 {
40 	list->prev = list;
41 	list->next = list;
42 }
43 
44 WL_EXPORT void
wl_list_insert(struct wl_list * list,struct wl_list * elm)45 wl_list_insert(struct wl_list *list, struct wl_list *elm)
46 {
47 	elm->prev = list;
48 	elm->next = list->next;
49 	list->next = elm;
50 	elm->next->prev = elm;
51 }
52 
53 WL_EXPORT void
wl_list_remove(struct wl_list * elm)54 wl_list_remove(struct wl_list *elm)
55 {
56 	elm->prev->next = elm->next;
57 	elm->next->prev = elm->prev;
58 	elm->next = NULL;
59 	elm->prev = NULL;
60 }
61 
62 WL_EXPORT int
wl_list_length(const struct wl_list * list)63 wl_list_length(const struct wl_list *list)
64 {
65 	struct wl_list *e;
66 	int count;
67 
68 	count = 0;
69 	e = list->next;
70 	while (e != list) {
71 		e = e->next;
72 		count++;
73 	}
74 
75 	return count;
76 }
77 
78 WL_EXPORT int
wl_list_empty(const struct wl_list * list)79 wl_list_empty(const struct wl_list *list)
80 {
81 	return list->next == list;
82 }
83 
84 WL_EXPORT void
wl_list_insert_list(struct wl_list * list,struct wl_list * other)85 wl_list_insert_list(struct wl_list *list, struct wl_list *other)
86 {
87 	if (wl_list_empty(other))
88 		return;
89 
90 	other->next->prev = list;
91 	other->prev->next = list->next;
92 	list->next->prev = other->prev;
93 	list->next = other->next;
94 }
95 
96 WL_EXPORT void
wl_array_init(struct wl_array * array)97 wl_array_init(struct wl_array *array)
98 {
99 	memset(array, 0, sizeof *array);
100 }
101 
102 WL_EXPORT void
wl_array_release(struct wl_array * array)103 wl_array_release(struct wl_array *array)
104 {
105 	free(array->data);
106 	array->data = WL_ARRAY_POISON_PTR;
107 }
108 
109 WL_EXPORT void *
wl_array_add(struct wl_array * array,size_t size)110 wl_array_add(struct wl_array *array, size_t size)
111 {
112 	size_t alloc;
113 	void *data, *p;
114 
115 	if (array->alloc > 0)
116 		alloc = array->alloc;
117 	else
118 		alloc = 16;
119 
120 	while (alloc < array->size + size)
121 		alloc *= 2;
122 
123 	if (array->alloc < alloc) {
124 		if (array->alloc > 0)
125 			data = realloc(array->data, alloc);
126 		else
127 			data = malloc(alloc);
128 
129 		if (data == NULL)
130 			return NULL;
131 		array->data = data;
132 		array->alloc = alloc;
133 	}
134 
135 	p = (char *)array->data + array->size;
136 	array->size += size;
137 
138 	return p;
139 }
140 
141 WL_EXPORT int
wl_array_copy(struct wl_array * array,struct wl_array * source)142 wl_array_copy(struct wl_array *array, struct wl_array *source)
143 {
144 	if (array->size < source->size) {
145 		if (!wl_array_add(array, source->size - array->size))
146 			return -1;
147 	} else {
148 		array->size = source->size;
149 	}
150 
151 	if (source->size > 0)
152 		memcpy(array->data, source->data, source->size);
153 
154 	return 0;
155 }
156 
157 /** \cond */
158 
159 int
wl_interface_equal(const struct wl_interface * a,const struct wl_interface * b)160 wl_interface_equal(const struct wl_interface *a, const struct wl_interface *b)
161 {
162 	/* In most cases the pointer equality test is sufficient.
163 	 * However, in some cases, depending on how things are split
164 	 * across shared objects, we can end up with multiple
165 	 * instances of the interface metadata constants.  So if the
166 	 * pointers match, the interfaces are equal, if they don't
167 	 * match we have to compare the interface names.
168 	 */
169 	return a == b || strcmp(a->name, b->name) == 0;
170 }
171 
172 union map_entry {
173 	uintptr_t next;
174 	void *data;
175 };
176 
177 #define map_entry_is_free(entry) ((entry).next & 0x1)
178 #define map_entry_get_data(entry) ((void *)((entry).next & ~(uintptr_t)0x3))
179 #define map_entry_get_flags(entry) (((entry).next >> 1) & 0x1)
180 
181 void
wl_map_init(struct wl_map * map,uint32_t side)182 wl_map_init(struct wl_map *map, uint32_t side)
183 {
184 	memset(map, 0, sizeof *map);
185 	map->side = side;
186 }
187 
188 void
wl_map_release(struct wl_map * map)189 wl_map_release(struct wl_map *map)
190 {
191 	wl_array_release(&map->client_entries);
192 	wl_array_release(&map->server_entries);
193 }
194 
195 uint32_t
wl_map_insert_new(struct wl_map * map,uint32_t flags,void * data)196 wl_map_insert_new(struct wl_map *map, uint32_t flags, void *data)
197 {
198 	union map_entry *start, *entry;
199 	struct wl_array *entries;
200 	uint32_t base;
201 	uint32_t count;
202 
203 	if (map->side == WL_MAP_CLIENT_SIDE) {
204 		entries = &map->client_entries;
205 		base = 0;
206 	} else {
207 		entries = &map->server_entries;
208 		base = WL_SERVER_ID_START;
209 	}
210 
211 	if (map->free_list) {
212 		start = entries->data;
213 		entry = &start[map->free_list >> 1];
214 		map->free_list = entry->next;
215 	} else {
216 		entry = wl_array_add(entries, sizeof *entry);
217 		if (!entry)
218 			return 0;
219 		start = entries->data;
220 	}
221 
222 	/* wl_array only grows, so if we have too many objects at
223 	 * this point there's no way to clean up. We could be more
224 	 * pro-active about trying to avoid this allocation, but
225 	 * it doesn't really matter because at this point there is
226 	 * nothing to be done but disconnect the client and delete
227 	 * the whole array either way.
228 	 */
229 	count = entry - start;
230 	if (count > WL_MAP_MAX_OBJECTS) {
231 		/* entry->data is freshly malloced garbage, so we'd
232 		 * better make it a NULL so wl_map_for_each doesn't
233 		 * dereference it later. */
234 		entry->data = NULL;
235 		errno = ENOSPC;
236 		return 0;
237 	}
238 	entry->data = data;
239 	entry->next |= (flags & 0x1) << 1;
240 
241 	return count + base;
242 }
243 
244 int
wl_map_insert_at(struct wl_map * map,uint32_t flags,uint32_t i,void * data)245 wl_map_insert_at(struct wl_map *map, uint32_t flags, uint32_t i, void *data)
246 {
247 	union map_entry *start;
248 	uint32_t count;
249 	struct wl_array *entries;
250 
251 	if (i < WL_SERVER_ID_START) {
252 		entries = &map->client_entries;
253 	} else {
254 		entries = &map->server_entries;
255 		i -= WL_SERVER_ID_START;
256 	}
257 
258 	if (i > WL_MAP_MAX_OBJECTS) {
259 		errno = ENOSPC;
260 		return -1;
261 	}
262 
263 	count = entries->size / sizeof *start;
264 	if (count < i) {
265 		errno = EINVAL;
266 		return -1;
267 	}
268 
269 	if (count == i) {
270 		if (!wl_array_add(entries, sizeof *start))
271 			return -1;
272 	}
273 
274 	start = entries->data;
275 	start[i].data = data;
276 	start[i].next |= (flags & 0x1) << 1;
277 
278 	return 0;
279 }
280 
281 int
wl_map_reserve_new(struct wl_map * map,uint32_t i)282 wl_map_reserve_new(struct wl_map *map, uint32_t i)
283 {
284 	union map_entry *start;
285 	uint32_t count;
286 	struct wl_array *entries;
287 
288 	if (i < WL_SERVER_ID_START) {
289 		if (map->side == WL_MAP_CLIENT_SIDE) {
290 			errno = EINVAL;
291 			return -1;
292 		}
293 
294 		entries = &map->client_entries;
295 	} else {
296 		if (map->side == WL_MAP_SERVER_SIDE) {
297 			errno = EINVAL;
298 			return -1;
299 		}
300 
301 		entries = &map->server_entries;
302 		i -= WL_SERVER_ID_START;
303 	}
304 
305 	if (i > WL_MAP_MAX_OBJECTS) {
306 		errno = ENOSPC;
307 		return -1;
308 	}
309 
310 	count = entries->size / sizeof *start;
311 	if (count < i) {
312 		errno = EINVAL;
313 		return -1;
314 	}
315 
316 	if (count == i) {
317 		if (!wl_array_add(entries, sizeof *start))
318 			return -1;
319 
320 		start = entries->data;
321 		start[i].data = NULL;
322 	} else {
323 		start = entries->data;
324 		if (start[i].data != NULL) {
325 			errno = EINVAL;
326 			return -1;
327 		}
328 	}
329 
330 	return 0;
331 }
332 
333 void
wl_map_remove(struct wl_map * map,uint32_t i)334 wl_map_remove(struct wl_map *map, uint32_t i)
335 {
336 	union map_entry *start;
337 	struct wl_array *entries;
338 
339 	if (i < WL_SERVER_ID_START) {
340 		if (map->side == WL_MAP_SERVER_SIDE)
341 			return;
342 
343 		entries = &map->client_entries;
344 	} else {
345 		if (map->side == WL_MAP_CLIENT_SIDE)
346 			return;
347 
348 		entries = &map->server_entries;
349 		i -= WL_SERVER_ID_START;
350 	}
351 
352 	start = entries->data;
353 	start[i].next = map->free_list;
354 	map->free_list = (i << 1) | 1;
355 }
356 
357 void *
wl_map_lookup(struct wl_map * map,uint32_t i)358 wl_map_lookup(struct wl_map *map, uint32_t i)
359 {
360 	union map_entry *start;
361 	uint32_t count;
362 	struct wl_array *entries;
363 
364 	if (i < WL_SERVER_ID_START) {
365 		entries = &map->client_entries;
366 	} else {
367 		entries = &map->server_entries;
368 		i -= WL_SERVER_ID_START;
369 	}
370 
371 	start = entries->data;
372 	count = entries->size / sizeof *start;
373 
374 	if (i < count && !map_entry_is_free(start[i]))
375 		return map_entry_get_data(start[i]);
376 
377 	return NULL;
378 }
379 
380 uint32_t
wl_map_lookup_flags(struct wl_map * map,uint32_t i)381 wl_map_lookup_flags(struct wl_map *map, uint32_t i)
382 {
383 	union map_entry *start;
384 	uint32_t count;
385 	struct wl_array *entries;
386 
387 	if (i < WL_SERVER_ID_START) {
388 		entries = &map->client_entries;
389 	} else {
390 		entries = &map->server_entries;
391 		i -= WL_SERVER_ID_START;
392 	}
393 
394 	start = entries->data;
395 	count = entries->size / sizeof *start;
396 
397 	if (i < count && !map_entry_is_free(start[i]))
398 		return map_entry_get_flags(start[i]);
399 
400 	return 0;
401 }
402 
403 static enum wl_iterator_result
for_each_helper(struct wl_array * entries,wl_iterator_func_t func,void * data)404 for_each_helper(struct wl_array *entries, wl_iterator_func_t func, void *data)
405 {
406 	enum wl_iterator_result ret = WL_ITERATOR_CONTINUE;
407 	union map_entry entry, *start;
408 	size_t count;
409 
410 	start = (union map_entry *) entries->data;
411 	count = entries->size / sizeof(union map_entry);
412 
413 	for (size_t idx = 0; idx < count; idx++) {
414 		entry = start[idx];
415 		if (entry.data && !map_entry_is_free(entry)) {
416 			ret = func(map_entry_get_data(entry), data, map_entry_get_flags(entry));
417 			if (ret != WL_ITERATOR_CONTINUE)
418 				break;
419 		}
420 	}
421 
422 	return ret;
423 }
424 
425 void
wl_map_for_each(struct wl_map * map,wl_iterator_func_t func,void * data)426 wl_map_for_each(struct wl_map *map, wl_iterator_func_t func, void *data)
427 {
428 	enum wl_iterator_result ret;
429 
430 	ret = for_each_helper(&map->client_entries, func, data);
431 	if (ret == WL_ITERATOR_CONTINUE)
432 		for_each_helper(&map->server_entries, func, data);
433 }
434 
435 static void
wl_log_stderr_handler(const char * fmt,va_list arg)436 wl_log_stderr_handler(const char *fmt, va_list arg)
437 {
438 	vfprintf(stderr, fmt, arg);
439 }
440 
441 wl_log_func_t wl_log_handler = wl_log_stderr_handler;
442 
443 void
wl_log(const char * fmt,...)444 wl_log(const char *fmt, ...)
445 {
446 	va_list argp;
447 
448 	va_start(argp, fmt);
449 	wl_log_handler(fmt, argp);
450 	va_end(argp);
451 }
452 
453 void
wl_abort(const char * fmt,...)454 wl_abort(const char *fmt, ...)
455 {
456 	va_list argp;
457 
458 	va_start(argp, fmt);
459 	wl_log_handler(fmt, argp);
460 	va_end(argp);
461 
462 	abort();
463 }
464 
465 /** \endcond */
466