blob: 1a154ac9dd8b8d82a79b87039b375837a18cf979 [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øgsberg864c4682008-12-12 11:05:17 -050024#include <string.h>
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -040025#include "wayland.h"
26
Kristian Høgsberg864c4682008-12-12 11:05:17 -050027struct wl_hash {
28 struct wl_object **objects;
29 uint32_t count, alloc;
30};
31
32struct wl_hash *
33wl_hash_create(void)
34{
35 struct wl_hash *hash;
36
37 hash = malloc(sizeof *hash);
38 if (hash == NULL)
39 return hash;
40
41 memset(hash, 0, sizeof *hash);
42
43 return hash;
44}
45
46void
47wl_hash_destroy(struct wl_hash *hash)
48{
49 free(hash);
50}
51
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -040052int wl_hash_insert(struct wl_hash *hash, struct wl_object *object)
53{
54 struct wl_object **objects;
55 uint32_t alloc;
56
57 if (hash->count == hash->alloc) {
58 if (hash->alloc == 0)
59 alloc = 16;
60 else
61 alloc = hash->alloc * 2;
62 objects = realloc(hash->objects, alloc * sizeof *objects);
63 if (objects == NULL)
64 return -1;
65
66 hash->objects = objects;
67 hash->alloc = alloc;
68 }
69
70 hash->objects[hash->count] = object;
71 hash->count++;
72
73 return 0;
74}
75
76struct wl_object *
77wl_hash_lookup(struct wl_hash *hash, uint32_t id)
78{
79 int i;
80
81 for (i = 0; i < hash->count; i++) {
82 if (hash->objects[i]->id == id)
83 return hash->objects[i];
84 }
85
86 return NULL;
87}
88
89void
90wl_hash_delete(struct wl_hash *hash, struct wl_object *object)
91{
92 /* writeme */
93}
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -050094
95
96void wl_list_init(struct wl_list *list)
97{
98 list->prev = list;
99 list->next = list;
100}
101
102void
103wl_list_insert(struct wl_list *list, struct wl_list *elm)
104{
105 elm->prev = list;
106 elm->next = list->next;
107 list->next = elm;
108 elm->next->prev = elm;
109}
110
111void
112wl_list_remove(struct wl_list *elm)
113{
114 elm->prev->next = elm->next;
115 elm->next->prev = elm->prev;
116}
117
118int
119wl_list_length(struct wl_list *list)
120{
121 struct wl_list *e;
122 int count;
123
124 e = list->next;
125 while (e != list) {
126 e = e->next;
127 count++;
128 }
129
130 return count;
131}
Kristian Høgsberg4a298902008-11-28 18:35:25 -0500132
133int
134wl_list_empty(struct wl_list *list)
135{
136 return list->next == list;
137}