Store objects in wl_map data structure
[profile/ivi/wayland.git] / src / wayland-util.c
1 /*
2  * Copyright © 2008 Kristian Høgsberg
3  *
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.
13  *
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
20  * OF THIS SOFTWARE.
21  */
22
23 #include <stdlib.h>
24 #include <stdint.h>
25 #include <string.h>
26 #include "wayland-util.h"
27
28 WL_EXPORT void
29 wl_list_init(struct wl_list *list)
30 {
31         list->prev = list;
32         list->next = list;
33 }
34
35 WL_EXPORT void
36 wl_list_insert(struct wl_list *list, struct wl_list *elm)
37 {
38         elm->prev = list;
39         elm->next = list->next;
40         list->next = elm;
41         elm->next->prev = elm;
42 }
43
44 WL_EXPORT void
45 wl_list_remove(struct wl_list *elm)
46 {
47         elm->prev->next = elm->next;
48         elm->next->prev = elm->prev;
49 }
50
51 WL_EXPORT int
52 wl_list_length(struct wl_list *list)
53 {
54         struct wl_list *e;
55         int count;
56
57         count = 0;
58         e = list->next;
59         while (e != list) {
60                 e = e->next;
61                 count++;
62         }
63
64         return count;
65 }
66
67 WL_EXPORT int
68 wl_list_empty(struct wl_list *list)
69 {
70         return list->next == list;
71 }
72
73 WL_EXPORT void
74 wl_array_init(struct wl_array *array)
75 {
76         memset(array, 0, sizeof *array);
77 }
78
79 WL_EXPORT void
80 wl_array_release(struct wl_array *array)
81 {
82         free(array->data);
83 }
84
85 WL_EXPORT void *
86 wl_array_add(struct wl_array *array, int size)
87 {
88         int alloc;
89         void *data, *p;
90
91         if (array->alloc > 0)
92                 alloc = array->alloc;
93         else
94                 alloc = 16;
95
96         while (alloc < array->size + size)
97                 alloc *= 2;
98
99         if (array->alloc < alloc) {
100                 if (array->alloc > 0)
101                         data = realloc(array->data, alloc);
102                 else
103                         data = malloc(alloc);
104
105                 if (data == NULL)
106                         return 0;
107                 array->data = data;
108                 array->alloc = alloc;
109         }
110
111         p = array->data + array->size;
112         array->size += size;
113
114         return p;
115 }
116
117 WL_EXPORT void
118 wl_array_copy(struct wl_array *array, struct wl_array *source)
119 {
120         array->size = 0;
121         wl_array_add(array, source->size);
122         memcpy(array->data, source->data, source->size);
123 }
124
125 union map_entry {
126         uintptr_t next;
127         void *data;
128 };
129
130 WL_EXPORT void
131 wl_map_init(struct wl_map *map)
132 {
133         memset(map, 0, sizeof *map);
134 }
135
136 WL_EXPORT void
137 wl_map_release(struct wl_map *map)
138 {
139         wl_array_release(&map->entries);
140 }
141
142 WL_EXPORT uint32_t
143 wl_map_insert_new(struct wl_map *map, void *data)
144 {
145         union map_entry *start, *entry;
146
147         if (map->free_list) {
148                 start = map->entries.data;
149                 entry = &start[map->free_list >> 1];
150                 map->free_list = entry->next;
151         } else {
152                 entry = wl_array_add(&map->entries, sizeof *entry);
153                 start = map->entries.data;
154         }
155
156         entry->data = data;
157
158         return entry - start;
159 }
160
161 WL_EXPORT int
162 wl_map_insert_at(struct wl_map *map, uint32_t i, void *data)
163 {
164         union map_entry *start;
165         uint32_t count;
166
167         /* assert(map->free_list == NULL */
168         count = map->entries.size / sizeof *start;
169
170         if (count < i)
171                 return -1;
172
173         if (count == i)
174                 wl_array_add(&map->entries, sizeof *start);
175
176         start = map->entries.data;
177         start[i].data = data;
178
179         return 0;
180 }
181
182 WL_EXPORT void
183 wl_map_remove(struct wl_map *map, uint32_t i)
184 {
185         union map_entry *start;
186         uint32_t count;
187
188         start = map->entries.data;
189         count = map->entries.size / sizeof *start;
190
191         start[i].next = map->free_list;
192         map->free_list = (i << 1) | 1;
193 }
194
195 WL_EXPORT void *
196 wl_map_lookup(struct wl_map *map, uint32_t i)
197 {
198         union map_entry *start;
199         uint32_t count;
200
201         start = map->entries.data;
202         count = map->entries.size / sizeof *start;
203
204         if (i < count && !(start[i].next & 1))
205                 return start[i].data;
206
207         return NULL;
208 }
209
210 WL_EXPORT void
211 wl_map_for_each(struct wl_map *map, wl_iterator_func_t func, void *data)
212 {
213         union map_entry *start, *end, *p;
214
215         start = map->entries.data;
216         end = (union map_entry *) ((char *) map->entries.data + map->entries.size);
217
218         for (p = start; p < end; p++)
219                 if (p->data && !(p->next & 1))
220                         func(p->data, data);
221 }