blob: 06efd323173fc51321ec1cd1c0a64cca210465c0 [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
Kristian Høgsberg59fa3462009-09-18 09:47:34 -040097WL_EXPORT void
98wl_list_init(struct wl_list *list)
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -050099{
100 list->prev = list;
101 list->next = list;
102}
103
Kristian Høgsberg59fa3462009-09-18 09:47:34 -0400104WL_EXPORT void
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500105wl_list_insert(struct wl_list *list, struct wl_list *elm)
106{
107 elm->prev = list;
108 elm->next = list->next;
109 list->next = elm;
110 elm->next->prev = elm;
111}
112
Kristian Høgsberg59fa3462009-09-18 09:47:34 -0400113WL_EXPORT void
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500114wl_list_remove(struct wl_list *elm)
115{
116 elm->prev->next = elm->next;
117 elm->next->prev = elm->prev;
118}
119
Kristian Høgsberg59fa3462009-09-18 09:47:34 -0400120WL_EXPORT int
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500121wl_list_length(struct wl_list *list)
122{
123 struct wl_list *e;
124 int count;
125
Kristian Høgsberg3f169562008-12-14 15:52:34 -0500126 count = 0;
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500127 e = list->next;
128 while (e != list) {
129 e = e->next;
130 count++;
131 }
132
133 return count;
134}
Kristian Høgsberg4a298902008-11-28 18:35:25 -0500135
Kristian Høgsberg59fa3462009-09-18 09:47:34 -0400136WL_EXPORT int
Kristian Høgsberg4a298902008-11-28 18:35:25 -0500137wl_list_empty(struct wl_list *list)
138{
139 return list->next == list;
140}
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -0500141
Kristian Høgsberg59fa3462009-09-18 09:47:34 -0400142WL_EXPORT void
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -0500143wl_array_init(struct wl_array *array)
144{
145 memset(array, 0, sizeof *array);
146}
147
Kristian Høgsberg59fa3462009-09-18 09:47:34 -0400148WL_EXPORT void
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -0500149wl_array_release(struct wl_array *array)
150{
151 free(array->data);
152}
153
Kristian Høgsberg59fa3462009-09-18 09:47:34 -0400154WL_EXPORT void *
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -0500155wl_array_add(struct wl_array *array, int size)
156{
157 int alloc;
158 void *data, *p;
159
160 if (array->alloc > 0)
161 alloc = array->alloc;
162 else
163 alloc = 16;
164
165 while (alloc < array->size + size)
166 alloc *= 2;
167
168 if (array->alloc < alloc) {
169 if (array->alloc > 0)
170 data = realloc(array->data, alloc);
171 else
172 data = malloc(alloc);
173
174 if (data == NULL)
175 return 0;
176 array->data = data;
177 array->alloc = alloc;
178 }
179
180 p = array->data + array->size;
181 array->size += size;
182
183 return p;
184}