blob: a5ab8be4a6e1ae4cf7f90e66397b01116c9a33df [file] [log] [blame]
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05001/*
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
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -040023#include <stdlib.h>
Kristian Høgsbergfabd4392008-12-22 18:06:49 -050024#include <stdint.h>
Kristian Høgsberg864c4682008-12-12 11:05:17 -050025#include <string.h>
Kristian Høgsbergfabd4392008-12-22 18:06:49 -050026#include "wayland-util.h"
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -040027
Kristian Høgsberg864c4682008-12-12 11:05:17 -050028struct wl_hash {
29 struct wl_object **objects;
30 uint32_t count, alloc;
31};
32
33struct wl_hash *
34wl_hash_create(void)
35{
36 struct wl_hash *hash;
37
38 hash = malloc(sizeof *hash);
39 if (hash == NULL)
40 return hash;
41
42 memset(hash, 0, sizeof *hash);
43
44 return hash;
45}
46
47void
48wl_hash_destroy(struct wl_hash *hash)
49{
50 free(hash);
51}
52
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -040053int wl_hash_insert(struct wl_hash *hash, struct wl_object *object)
54{
55 struct wl_object **objects;
56 uint32_t alloc;
57
58 if (hash->count == hash->alloc) {
59 if (hash->alloc == 0)
60 alloc = 16;
61 else
62 alloc = hash->alloc * 2;
63 objects = realloc(hash->objects, alloc * sizeof *objects);
64 if (objects == NULL)
65 return -1;
66
67 hash->objects = objects;
68 hash->alloc = alloc;
69 }
70
71 hash->objects[hash->count] = object;
72 hash->count++;
73
74 return 0;
75}
76
77struct wl_object *
78wl_hash_lookup(struct wl_hash *hash, uint32_t id)
79{
80 int i;
81
82 for (i = 0; i < hash->count; i++) {
83 if (hash->objects[i]->id == id)
84 return hash->objects[i];
85 }
86
87 return NULL;
88}
89
90void
91wl_hash_delete(struct wl_hash *hash, struct wl_object *object)
92{
93 /* writeme */
94}
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -050095
96
97void wl_list_init(struct wl_list *list)
98{
99 list->prev = list;
100 list->next = list;
101}
102
103void
104wl_list_insert(struct wl_list *list, struct wl_list *elm)
105{
106 elm->prev = list;
107 elm->next = list->next;
108 list->next = elm;
109 elm->next->prev = elm;
110}
111
112void
113wl_list_remove(struct wl_list *elm)
114{
115 elm->prev->next = elm->next;
116 elm->next->prev = elm->prev;
117}
118
119int
120wl_list_length(struct wl_list *list)
121{
122 struct wl_list *e;
123 int count;
124
Kristian Høgsberg3f169562008-12-14 15:52:34 -0500125 count = 0;
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500126 e = list->next;
127 while (e != list) {
128 e = e->next;
129 count++;
130 }
131
132 return count;
133}
Kristian Høgsberg4a298902008-11-28 18:35:25 -0500134
135int
136wl_list_empty(struct wl_list *list)
137{
138 return list->next == list;
139}
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -0500140
141void
142wl_array_init(struct wl_array *array)
143{
144 memset(array, 0, sizeof *array);
145}
146
147void
148wl_array_release(struct wl_array *array)
149{
150 free(array->data);
151}
152
153void *
154wl_array_add(struct wl_array *array, int size)
155{
156 int alloc;
157 void *data, *p;
158
159 if (array->alloc > 0)
160 alloc = array->alloc;
161 else
162 alloc = 16;
163
164 while (alloc < array->size + size)
165 alloc *= 2;
166
167 if (array->alloc < alloc) {
168 if (array->alloc > 0)
169 data = realloc(array->data, alloc);
170 else
171 data = malloc(alloc);
172
173 if (data == NULL)
174 return 0;
175 array->data = data;
176 array->alloc = alloc;
177 }
178
179 p = array->data + array->size;
180 array->size += size;
181
182 return p;
183}