2 * Copyright © 2008 Kristian Høgsberg
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that copyright
7 * notice and this permission notice appear in supporting documentation, and
8 * that the name of the copyright holders not be used in advertising or
9 * publicity pertaining to distribution of the software without specific,
10 * written prior permission. The copyright holders make no representations
11 * about the suitability of this software for any purpose. It is provided "as
12 * is" without express or implied warranty.
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
26 #include "wayland-util.h"
29 wl_list_init(struct wl_list *list)
36 wl_list_insert(struct wl_list *list, struct wl_list *elm)
39 elm->next = list->next;
41 elm->next->prev = elm;
45 wl_list_remove(struct wl_list *elm)
47 elm->prev->next = elm->next;
48 elm->next->prev = elm->prev;
52 wl_list_length(struct wl_list *list)
68 wl_list_empty(struct wl_list *list)
70 return list->next == list;
74 wl_array_init(struct wl_array *array)
76 memset(array, 0, sizeof *array);
80 wl_array_release(struct wl_array *array)
86 wl_array_add(struct wl_array *array, int size)
96 while (alloc < array->size + size)
99 if (array->alloc < alloc) {
100 if (array->alloc > 0)
101 data = realloc(array->data, alloc);
103 data = malloc(alloc);
108 array->alloc = alloc;
111 p = array->data + array->size;
118 wl_array_copy(struct wl_array *array, struct wl_array *source)
121 wl_array_add(array, source->size);
122 memcpy(array->data, source->data, source->size);
131 wl_map_init(struct wl_map *map)
133 memset(map, 0, sizeof *map);
137 wl_map_release(struct wl_map *map)
139 wl_array_release(&map->entries);
143 wl_map_insert_new(struct wl_map *map, void *data)
145 union map_entry *start, *entry;
147 if (map->free_list) {
148 start = map->entries.data;
149 entry = &start[map->free_list >> 1];
150 map->free_list = entry->next;
152 entry = wl_array_add(&map->entries, sizeof *entry);
153 start = map->entries.data;
158 return entry - start;
162 wl_map_insert_at(struct wl_map *map, uint32_t i, void *data)
164 union map_entry *start;
167 /* assert(map->free_list == NULL */
168 count = map->entries.size / sizeof *start;
174 wl_array_add(&map->entries, sizeof *start);
176 start = map->entries.data;
177 start[i].data = data;
183 wl_map_remove(struct wl_map *map, uint32_t i)
185 union map_entry *start;
188 start = map->entries.data;
189 count = map->entries.size / sizeof *start;
191 start[i].next = map->free_list;
192 map->free_list = (i << 1) | 1;
196 wl_map_lookup(struct wl_map *map, uint32_t i)
198 union map_entry *start;
201 start = map->entries.data;
202 count = map->entries.size / sizeof *start;
204 if (i < count && !(start[i].next & 1))
205 return start[i].data;
211 wl_map_for_each(struct wl_map *map, wl_iterator_func_t func, void *data)
213 union map_entry *start, *end, *p;
215 start = map->entries.data;
216 end = (union map_entry *) ((char *) map->entries.data + map->entries.size);
218 for (p = start; p < end; p++)
219 if (p->data && !(p->next & 1))