Drop libdrm CFLAGS where no longer necessary.
[profile/ivi/wayland.git] / 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 struct wl_hash {
29         struct wl_object **objects;
30         uint32_t count, alloc;
31 };
32
33 struct wl_hash *
34 wl_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
47 void
48 wl_hash_destroy(struct wl_hash *hash)
49 {
50         free(hash);
51 }
52
53 int 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
77 struct wl_object *
78 wl_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
90 void
91 wl_hash_delete(struct wl_hash *hash, struct wl_object *object)
92 {
93         /* writeme */
94 }
95
96
97 void wl_list_init(struct wl_list *list)
98 {
99         list->prev = list;
100         list->next = list;
101 }
102
103 void
104 wl_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
112 void
113 wl_list_remove(struct wl_list *elm)
114 {
115         elm->prev->next = elm->next;
116         elm->next->prev = elm->prev;
117 }
118
119 int
120 wl_list_length(struct wl_list *list)
121 {
122         struct wl_list *e;
123         int count;
124
125         count = 0;
126         e = list->next;
127         while (e != list) {
128                 e = e->next;
129                 count++;
130         }
131
132         return count;
133 }
134
135 int
136 wl_list_empty(struct wl_list *list)
137 {
138         return list->next == list;
139 }