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 | 864c468 | 2008-12-12 11:05:17 -0500 | [diff] [blame] | 24 | #include <string.h> |
Kristian Høgsberg | 97f1ebe | 2008-09-30 09:46:10 -0400 | [diff] [blame] | 25 | #include "wayland.h" |
| 26 | |
Kristian Høgsberg | 864c468 | 2008-12-12 11:05:17 -0500 | [diff] [blame] | 27 | struct wl_hash { |
| 28 | struct wl_object **objects; |
| 29 | uint32_t count, alloc; |
| 30 | }; |
| 31 | |
| 32 | struct wl_hash * |
| 33 | wl_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 | |
| 46 | void |
| 47 | wl_hash_destroy(struct wl_hash *hash) |
| 48 | { |
| 49 | free(hash); |
| 50 | } |
| 51 | |
Kristian Høgsberg | 97f1ebe | 2008-09-30 09:46:10 -0400 | [diff] [blame] | 52 | int 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 | |
| 76 | struct wl_object * |
| 77 | wl_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 | |
| 89 | void |
| 90 | wl_hash_delete(struct wl_hash *hash, struct wl_object *object) |
| 91 | { |
| 92 | /* writeme */ |
| 93 | } |
Kristian Høgsberg | 1e4b86a | 2008-11-23 23:41:08 -0500 | [diff] [blame] | 94 | |
| 95 | |
| 96 | void wl_list_init(struct wl_list *list) |
| 97 | { |
| 98 | list->prev = list; |
| 99 | list->next = list; |
| 100 | } |
| 101 | |
| 102 | void |
| 103 | wl_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 | |
| 111 | void |
| 112 | wl_list_remove(struct wl_list *elm) |
| 113 | { |
| 114 | elm->prev->next = elm->next; |
| 115 | elm->next->prev = elm->prev; |
| 116 | } |
| 117 | |
| 118 | int |
| 119 | wl_list_length(struct wl_list *list) |
| 120 | { |
| 121 | struct wl_list *e; |
| 122 | int count; |
| 123 | |
Kristian Høgsberg | 3f16956 | 2008-12-14 15:52:34 -0500 | [diff] [blame] | 124 | count = 0; |
Kristian Høgsberg | 1e4b86a | 2008-11-23 23:41:08 -0500 | [diff] [blame] | 125 | e = list->next; |
| 126 | while (e != list) { |
| 127 | e = e->next; |
| 128 | count++; |
| 129 | } |
| 130 | |
| 131 | return count; |
| 132 | } |
Kristian Høgsberg | 4a29890 | 2008-11-28 18:35:25 -0500 | [diff] [blame] | 133 | |
| 134 | int |
| 135 | wl_list_empty(struct wl_list *list) |
| 136 | { |
| 137 | return list->next == list; |
| 138 | } |