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
28 #include "wayland-util.h"
29 #include "wayland-private.h"
32 wl_list_init(struct wl_list *list)
39 wl_list_insert(struct wl_list *list, struct wl_list *elm)
42 elm->next = list->next;
44 elm->next->prev = elm;
48 wl_list_remove(struct wl_list *elm)
50 elm->prev->next = elm->next;
51 elm->next->prev = elm->prev;
55 wl_list_length(struct wl_list *list)
71 wl_list_empty(struct wl_list *list)
73 return list->next == list;
77 wl_list_insert_list(struct wl_list *list, struct wl_list *other)
79 other->next->prev = list;
80 other->prev->next = list->next;
81 list->next->prev = other->prev;
82 list->next = other->next;
86 wl_array_init(struct wl_array *array)
88 memset(array, 0, sizeof *array);
92 wl_array_release(struct wl_array *array)
98 wl_array_add(struct wl_array *array, int size)
103 if (array->alloc > 0)
104 alloc = array->alloc;
108 while (alloc < array->size + size)
111 if (array->alloc < alloc) {
112 if (array->alloc > 0)
113 data = realloc(array->data, alloc);
115 data = malloc(alloc);
120 array->alloc = alloc;
123 p = array->data + array->size;
130 wl_array_copy(struct wl_array *array, struct wl_array *source)
133 wl_array_add(array, source->size);
134 memcpy(array->data, source->data, source->size);
143 wl_map_init(struct wl_map *map)
145 memset(map, 0, sizeof *map);
149 wl_map_release(struct wl_map *map)
151 wl_array_release(&map->entries);
155 wl_map_insert_new(struct wl_map *map, void *data)
157 union map_entry *start, *entry;
159 if (map->free_list) {
160 start = map->entries.data;
161 entry = &start[map->free_list >> 1];
162 map->free_list = entry->next;
164 entry = wl_array_add(&map->entries, sizeof *entry);
165 start = map->entries.data;
170 return entry - start;
174 wl_map_insert_at(struct wl_map *map, uint32_t i, void *data)
176 union map_entry *start;
179 /* assert(map->free_list == NULL */
180 count = map->entries.size / sizeof *start;
186 wl_array_add(&map->entries, sizeof *start);
188 start = map->entries.data;
189 start[i].data = data;
195 wl_map_remove(struct wl_map *map, uint32_t i)
197 union map_entry *start;
199 start = map->entries.data;
200 start[i].next = map->free_list;
201 map->free_list = (i << 1) | 1;
205 wl_map_lookup(struct wl_map *map, uint32_t i)
207 union map_entry *start;
210 start = map->entries.data;
211 count = map->entries.size / sizeof *start;
213 if (i < count && !(start[i].next & 1))
214 return start[i].data;
220 wl_map_for_each(struct wl_map *map, wl_iterator_func_t func, void *data)
222 union map_entry *start, *end, *p;
224 start = map->entries.data;
225 end = (union map_entry *) ((char *) map->entries.data + map->entries.size);
227 for (p = start; p < end; p++)
228 if (p->data && !(p->next & 1))