Kristian Høgsberg | ffd710e | 2008-12-02 15:15:01 -0500 | [diff] [blame] | 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 | |
Kristian Høgsberg | 97f1ebe | 2008-09-30 09:46:10 -0400 | [diff] [blame] | 23 | #include <stdlib.h> |
Kristian Høgsberg | fabd439 | 2008-12-22 18:06:49 -0500 | [diff] [blame] | 24 | #include <stdint.h> |
Kristian Høgsberg | 864c468 | 2008-12-12 11:05:17 -0500 | [diff] [blame] | 25 | #include <string.h> |
Kristian Høgsberg | fabd439 | 2008-12-22 18:06:49 -0500 | [diff] [blame] | 26 | #include "wayland-util.h" |
Kristian Høgsberg | 97f1ebe | 2008-09-30 09:46:10 -0400 | [diff] [blame] | 27 | |
Kristian Høgsberg | 864c468 | 2008-12-12 11:05:17 -0500 | [diff] [blame] | 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 | |
Kristian Høgsberg | 97f1ebe | 2008-09-30 09:46:10 -0400 | [diff] [blame] | 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 |
Kristian Høgsberg | f15ce9f | 2009-09-18 16:57:55 -0400 | [diff] [blame^] | 91 | wl_hash_remove(struct wl_hash *hash, struct wl_object *object) |
Kristian Høgsberg | 97f1ebe | 2008-09-30 09:46:10 -0400 | [diff] [blame] | 92 | { |
Kristian Høgsberg | f15ce9f | 2009-09-18 16:57:55 -0400 | [diff] [blame^] | 93 | int i; |
Kristian Høgsberg | 1e4b86a | 2008-11-23 23:41:08 -0500 | [diff] [blame] | 94 | |
Kristian Høgsberg | f15ce9f | 2009-09-18 16:57:55 -0400 | [diff] [blame^] | 95 | for (i = 0; i < hash->count; i++) { |
| 96 | if (hash->objects[i]->id == object->id) { |
| 97 | hash->objects[i] = hash->objects[hash->count - 1]; |
| 98 | hash->count--; |
| 99 | break; |
| 100 | } |
| 101 | } |
| 102 | } |
Kristian Høgsberg | 1e4b86a | 2008-11-23 23:41:08 -0500 | [diff] [blame] | 103 | |
Kristian Høgsberg | 59fa346 | 2009-09-18 09:47:34 -0400 | [diff] [blame] | 104 | WL_EXPORT void |
| 105 | wl_list_init(struct wl_list *list) |
Kristian Høgsberg | 1e4b86a | 2008-11-23 23:41:08 -0500 | [diff] [blame] | 106 | { |
| 107 | list->prev = list; |
| 108 | list->next = list; |
| 109 | } |
| 110 | |
Kristian Høgsberg | 59fa346 | 2009-09-18 09:47:34 -0400 | [diff] [blame] | 111 | WL_EXPORT void |
Kristian Høgsberg | 1e4b86a | 2008-11-23 23:41:08 -0500 | [diff] [blame] | 112 | wl_list_insert(struct wl_list *list, struct wl_list *elm) |
| 113 | { |
| 114 | elm->prev = list; |
| 115 | elm->next = list->next; |
| 116 | list->next = elm; |
| 117 | elm->next->prev = elm; |
| 118 | } |
| 119 | |
Kristian Høgsberg | 59fa346 | 2009-09-18 09:47:34 -0400 | [diff] [blame] | 120 | WL_EXPORT void |
Kristian Høgsberg | 1e4b86a | 2008-11-23 23:41:08 -0500 | [diff] [blame] | 121 | wl_list_remove(struct wl_list *elm) |
| 122 | { |
| 123 | elm->prev->next = elm->next; |
| 124 | elm->next->prev = elm->prev; |
| 125 | } |
| 126 | |
Kristian Høgsberg | 59fa346 | 2009-09-18 09:47:34 -0400 | [diff] [blame] | 127 | WL_EXPORT int |
Kristian Høgsberg | 1e4b86a | 2008-11-23 23:41:08 -0500 | [diff] [blame] | 128 | wl_list_length(struct wl_list *list) |
| 129 | { |
| 130 | struct wl_list *e; |
| 131 | int count; |
| 132 | |
Kristian Høgsberg | 3f16956 | 2008-12-14 15:52:34 -0500 | [diff] [blame] | 133 | count = 0; |
Kristian Høgsberg | 1e4b86a | 2008-11-23 23:41:08 -0500 | [diff] [blame] | 134 | e = list->next; |
| 135 | while (e != list) { |
| 136 | e = e->next; |
| 137 | count++; |
| 138 | } |
| 139 | |
| 140 | return count; |
| 141 | } |
Kristian Høgsberg | 4a29890 | 2008-11-28 18:35:25 -0500 | [diff] [blame] | 142 | |
Kristian Høgsberg | 59fa346 | 2009-09-18 09:47:34 -0400 | [diff] [blame] | 143 | WL_EXPORT int |
Kristian Høgsberg | 4a29890 | 2008-11-28 18:35:25 -0500 | [diff] [blame] | 144 | wl_list_empty(struct wl_list *list) |
| 145 | { |
| 146 | return list->next == list; |
| 147 | } |
Kristian Høgsberg | 3c38fa0 | 2009-02-23 22:30:29 -0500 | [diff] [blame] | 148 | |
Kristian Høgsberg | 59fa346 | 2009-09-18 09:47:34 -0400 | [diff] [blame] | 149 | WL_EXPORT void |
Kristian Høgsberg | 3c38fa0 | 2009-02-23 22:30:29 -0500 | [diff] [blame] | 150 | wl_array_init(struct wl_array *array) |
| 151 | { |
| 152 | memset(array, 0, sizeof *array); |
| 153 | } |
| 154 | |
Kristian Høgsberg | 59fa346 | 2009-09-18 09:47:34 -0400 | [diff] [blame] | 155 | WL_EXPORT void |
Kristian Høgsberg | 3c38fa0 | 2009-02-23 22:30:29 -0500 | [diff] [blame] | 156 | wl_array_release(struct wl_array *array) |
| 157 | { |
| 158 | free(array->data); |
| 159 | } |
| 160 | |
Kristian Høgsberg | 59fa346 | 2009-09-18 09:47:34 -0400 | [diff] [blame] | 161 | WL_EXPORT void * |
Kristian Høgsberg | 3c38fa0 | 2009-02-23 22:30:29 -0500 | [diff] [blame] | 162 | wl_array_add(struct wl_array *array, int size) |
| 163 | { |
| 164 | int alloc; |
| 165 | void *data, *p; |
| 166 | |
| 167 | if (array->alloc > 0) |
| 168 | alloc = array->alloc; |
| 169 | else |
| 170 | alloc = 16; |
| 171 | |
| 172 | while (alloc < array->size + size) |
| 173 | alloc *= 2; |
| 174 | |
| 175 | if (array->alloc < alloc) { |
| 176 | if (array->alloc > 0) |
| 177 | data = realloc(array->data, alloc); |
| 178 | else |
| 179 | data = malloc(alloc); |
| 180 | |
| 181 | if (data == NULL) |
| 182 | return 0; |
| 183 | array->data = data; |
| 184 | array->alloc = alloc; |
| 185 | } |
| 186 | |
| 187 | p = array->data + array->size; |
| 188 | array->size += size; |
| 189 | |
| 190 | return p; |
| 191 | } |