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 | 59fa346 | 2009-09-18 09:47:34 -0400 | [diff] [blame] | 28 | WL_EXPORT void |
| 29 | wl_list_init(struct wl_list *list) |
Kristian Høgsberg | 1e4b86a | 2008-11-23 23:41:08 -0500 | [diff] [blame] | 30 | { |
| 31 | list->prev = list; |
| 32 | list->next = list; |
| 33 | } |
| 34 | |
Kristian Høgsberg | 59fa346 | 2009-09-18 09:47:34 -0400 | [diff] [blame] | 35 | WL_EXPORT void |
Kristian Høgsberg | 1e4b86a | 2008-11-23 23:41:08 -0500 | [diff] [blame] | 36 | wl_list_insert(struct wl_list *list, struct wl_list *elm) |
| 37 | { |
| 38 | elm->prev = list; |
| 39 | elm->next = list->next; |
| 40 | list->next = elm; |
| 41 | elm->next->prev = elm; |
| 42 | } |
| 43 | |
Kristian Høgsberg | 59fa346 | 2009-09-18 09:47:34 -0400 | [diff] [blame] | 44 | WL_EXPORT void |
Kristian Høgsberg | 1e4b86a | 2008-11-23 23:41:08 -0500 | [diff] [blame] | 45 | wl_list_remove(struct wl_list *elm) |
| 46 | { |
| 47 | elm->prev->next = elm->next; |
| 48 | elm->next->prev = elm->prev; |
| 49 | } |
| 50 | |
Kristian Høgsberg | 59fa346 | 2009-09-18 09:47:34 -0400 | [diff] [blame] | 51 | WL_EXPORT int |
Kristian Høgsberg | 1e4b86a | 2008-11-23 23:41:08 -0500 | [diff] [blame] | 52 | wl_list_length(struct wl_list *list) |
| 53 | { |
| 54 | struct wl_list *e; |
| 55 | int count; |
| 56 | |
Kristian Høgsberg | 3f16956 | 2008-12-14 15:52:34 -0500 | [diff] [blame] | 57 | count = 0; |
Kristian Høgsberg | 1e4b86a | 2008-11-23 23:41:08 -0500 | [diff] [blame] | 58 | e = list->next; |
| 59 | while (e != list) { |
| 60 | e = e->next; |
| 61 | count++; |
| 62 | } |
| 63 | |
| 64 | return count; |
| 65 | } |
Kristian Høgsberg | 4a29890 | 2008-11-28 18:35:25 -0500 | [diff] [blame] | 66 | |
Kristian Høgsberg | 59fa346 | 2009-09-18 09:47:34 -0400 | [diff] [blame] | 67 | WL_EXPORT int |
Kristian Høgsberg | 4a29890 | 2008-11-28 18:35:25 -0500 | [diff] [blame] | 68 | wl_list_empty(struct wl_list *list) |
| 69 | { |
| 70 | return list->next == list; |
| 71 | } |
Kristian Høgsberg | 3c38fa0 | 2009-02-23 22:30:29 -0500 | [diff] [blame] | 72 | |
Kristian Høgsberg | 59fa346 | 2009-09-18 09:47:34 -0400 | [diff] [blame] | 73 | WL_EXPORT void |
Kristian Høgsberg | 3c38fa0 | 2009-02-23 22:30:29 -0500 | [diff] [blame] | 74 | wl_array_init(struct wl_array *array) |
| 75 | { |
| 76 | memset(array, 0, sizeof *array); |
| 77 | } |
| 78 | |
Kristian Høgsberg | 59fa346 | 2009-09-18 09:47:34 -0400 | [diff] [blame] | 79 | WL_EXPORT void |
Kristian Høgsberg | 3c38fa0 | 2009-02-23 22:30:29 -0500 | [diff] [blame] | 80 | wl_array_release(struct wl_array *array) |
| 81 | { |
| 82 | free(array->data); |
| 83 | } |
| 84 | |
Kristian Høgsberg | 59fa346 | 2009-09-18 09:47:34 -0400 | [diff] [blame] | 85 | WL_EXPORT void * |
Kristian Høgsberg | 3c38fa0 | 2009-02-23 22:30:29 -0500 | [diff] [blame] | 86 | wl_array_add(struct wl_array *array, int size) |
| 87 | { |
| 88 | int alloc; |
| 89 | void *data, *p; |
| 90 | |
| 91 | if (array->alloc > 0) |
| 92 | alloc = array->alloc; |
| 93 | else |
| 94 | alloc = 16; |
| 95 | |
| 96 | while (alloc < array->size + size) |
| 97 | alloc *= 2; |
| 98 | |
| 99 | if (array->alloc < alloc) { |
| 100 | if (array->alloc > 0) |
| 101 | data = realloc(array->data, alloc); |
| 102 | else |
| 103 | data = malloc(alloc); |
| 104 | |
| 105 | if (data == NULL) |
| 106 | return 0; |
| 107 | array->data = data; |
| 108 | array->alloc = alloc; |
| 109 | } |
| 110 | |
| 111 | p = array->data + array->size; |
| 112 | array->size += size; |
| 113 | |
| 114 | return p; |
| 115 | } |