• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 = (char *)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 	uint32_t count;
199 
200 	if (map->side == WL_MAP_CLIENT_SIDE) {
201 		entries = &map->client_entries;
202 		base = 0;
203 	} else {
204 		entries = &map->server_entries;
205 		base = WL_SERVER_ID_START;
206 	}
207 
208 	if (map->free_list) {
209 		start = entries->data;
210 		entry = &start[map->free_list >> 1];
211 		map->free_list = entry->next;
212 	} else {
213 		entry = wl_array_add(entries, sizeof *entry);
214 		if (!entry)
215 			return 0;
216 		start = entries->data;
217 	}
218 
219 	/* wl_array only grows, so if we have too many objects at
220 	 * this point there's no way to clean up. We could be more
221 	 * pro-active about trying to avoid this allocation, but
222 	 * it doesn't really matter because at this point there is
223 	 * nothing to be done but disconnect the client and delete
224 	 * the whole array either way.
225 	 */
226 	count = entry - start;
227 	if (count > WL_MAP_MAX_OBJECTS) {
228 		/* entry->data is freshly malloced garbage, so we'd
229 		 * better make it a NULL so wl_map_for_each doesn't
230 		 * dereference it later. */
231 		entry->data = NULL;
232 		return 0;
233 	}
234 	entry->data = data;
235 	entry->next |= (flags & 0x1) << 1;
236 
237 	return count + base;
238 }
239 
240 int
wl_map_insert_at(struct wl_map * map,uint32_t flags,uint32_t i,void * data)241 wl_map_insert_at(struct wl_map *map, uint32_t flags, uint32_t i, void *data)
242 {
243 	union map_entry *start;
244 	uint32_t count;
245 	struct wl_array *entries;
246 
247 	if (i < WL_SERVER_ID_START) {
248 		entries = &map->client_entries;
249 	} else {
250 		entries = &map->server_entries;
251 		i -= WL_SERVER_ID_START;
252 	}
253 
254 	if (i > WL_MAP_MAX_OBJECTS)
255 		return -1;
256 
257 	count = entries->size / sizeof *start;
258 	if (count < i)
259 		return -1;
260 
261 	if (count == i)
262 		wl_array_add(entries, sizeof *start);
263 
264 	start = entries->data;
265 	start[i].data = data;
266 	start[i].next |= (flags & 0x1) << 1;
267 
268 	return 0;
269 }
270 
271 int
wl_map_reserve_new(struct wl_map * map,uint32_t i)272 wl_map_reserve_new(struct wl_map *map, uint32_t i)
273 {
274 	union map_entry *start;
275 	uint32_t count;
276 	struct wl_array *entries;
277 
278 	if (i < WL_SERVER_ID_START) {
279 		if (map->side == WL_MAP_CLIENT_SIDE)
280 			return -1;
281 
282 		entries = &map->client_entries;
283 	} else {
284 		if (map->side == WL_MAP_SERVER_SIDE)
285 			return -1;
286 
287 		entries = &map->server_entries;
288 		i -= WL_SERVER_ID_START;
289 	}
290 
291 	if (i > WL_MAP_MAX_OBJECTS)
292 		return -1;
293 
294 	count = entries->size / sizeof *start;
295 	if (count < i)
296 		return -1;
297 
298 	if (count == i) {
299 		wl_array_add(entries, sizeof *start);
300 		start = entries->data;
301 		start[i].data = NULL;
302 	} else {
303 		start = entries->data;
304 		if (start[i].data != NULL) {
305 			return -1;
306 		}
307 	}
308 
309 	return 0;
310 }
311 
312 void
wl_map_remove(struct wl_map * map,uint32_t i)313 wl_map_remove(struct wl_map *map, uint32_t i)
314 {
315 	union map_entry *start;
316 	struct wl_array *entries;
317 
318 	if (i < WL_SERVER_ID_START) {
319 		if (map->side == WL_MAP_SERVER_SIDE)
320 			return;
321 
322 		entries = &map->client_entries;
323 	} else {
324 		if (map->side == WL_MAP_CLIENT_SIDE)
325 			return;
326 
327 		entries = &map->server_entries;
328 		i -= WL_SERVER_ID_START;
329 	}
330 
331 	start = entries->data;
332 	start[i].next = map->free_list;
333 	map->free_list = (i << 1) | 1;
334 }
335 
336 void *
wl_map_lookup(struct wl_map * map,uint32_t i)337 wl_map_lookup(struct wl_map *map, uint32_t i)
338 {
339 	union map_entry *start;
340 	uint32_t count;
341 	struct wl_array *entries;
342 
343 	if (i < WL_SERVER_ID_START) {
344 		entries = &map->client_entries;
345 	} else {
346 		entries = &map->server_entries;
347 		i -= WL_SERVER_ID_START;
348 	}
349 
350 	start = entries->data;
351 	count = entries->size / sizeof *start;
352 
353 	if (i < count && !map_entry_is_free(start[i]))
354 		return map_entry_get_data(start[i]);
355 
356 	return NULL;
357 }
358 
359 uint32_t
wl_map_lookup_flags(struct wl_map * map,uint32_t i)360 wl_map_lookup_flags(struct wl_map *map, uint32_t i)
361 {
362 	union map_entry *start;
363 	uint32_t count;
364 	struct wl_array *entries;
365 
366 	if (i < WL_SERVER_ID_START) {
367 		entries = &map->client_entries;
368 	} else {
369 		entries = &map->server_entries;
370 		i -= WL_SERVER_ID_START;
371 	}
372 
373 	start = entries->data;
374 	count = entries->size / sizeof *start;
375 
376 	if (i < count && !map_entry_is_free(start[i]))
377 		return map_entry_get_flags(start[i]);
378 
379 	return 0;
380 }
381 
382 static enum wl_iterator_result
for_each_helper(struct wl_array * entries,wl_iterator_func_t func,void * data)383 for_each_helper(struct wl_array *entries, wl_iterator_func_t func, void *data)
384 {
385 	union map_entry *start, *end, *p;
386 	enum wl_iterator_result ret = WL_ITERATOR_CONTINUE;
387 
388 	start = entries->data;
389 	end = (union map_entry *) ((char *) entries->data + entries->size);
390 
391 	for (p = start; p < end; p++)
392 		if (p->data && !map_entry_is_free(*p)) {
393 			ret = func(map_entry_get_data(*p), data, map_entry_get_flags(*p));
394 			if (ret != WL_ITERATOR_CONTINUE)
395 				break;
396 		}
397 
398 	return ret;
399 }
400 
401 void
wl_map_for_each(struct wl_map * map,wl_iterator_func_t func,void * data)402 wl_map_for_each(struct wl_map *map, wl_iterator_func_t func, void *data)
403 {
404 	enum wl_iterator_result ret;
405 
406 	ret = for_each_helper(&map->client_entries, func, data);
407 	if (ret == WL_ITERATOR_CONTINUE)
408 		for_each_helper(&map->server_entries, func, data);
409 }
410 
411 static void
wl_log_stderr_handler(const char * fmt,va_list arg)412 wl_log_stderr_handler(const char *fmt, va_list arg)
413 {
414 	vfprintf(stderr, fmt, arg);
415 }
416 
417 wl_log_func_t wl_log_handler = wl_log_stderr_handler;
418 
419 void
wl_log(const char * fmt,...)420 wl_log(const char *fmt, ...)
421 {
422 	va_list argp;
423 
424 	va_start(argp, fmt);
425 	wl_log_handler(fmt, argp);
426 	va_end(argp);
427 }
428 
429 void
wl_abort(const char * fmt,...)430 wl_abort(const char *fmt, ...)
431 {
432 	va_list argp;
433 
434 	va_start(argp, fmt);
435 	wl_log_handler(fmt, argp);
436 	va_end(argp);
437 
438 	abort();
439 }
440 
441 /** \endcond */
442