Change scanner.c license to MIT
[profile/ivi/wayland.git] / wayland / 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 WL_EXPORT void
29 wl_list_init(struct wl_list *list)
30 {
31         list->prev = list;
32         list->next = list;
33 }
34
35 WL_EXPORT void
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
44 WL_EXPORT void
45 wl_list_remove(struct wl_list *elm)
46 {
47         elm->prev->next = elm->next;
48         elm->next->prev = elm->prev;
49 }
50
51 WL_EXPORT int
52 wl_list_length(struct wl_list *list)
53 {
54         struct wl_list *e;
55         int count;
56
57         count = 0;
58         e = list->next;
59         while (e != list) {
60                 e = e->next;
61                 count++;
62         }
63
64         return count;
65 }
66
67 WL_EXPORT int
68 wl_list_empty(struct wl_list *list)
69 {
70         return list->next == list;
71 }
72
73 WL_EXPORT void
74 wl_array_init(struct wl_array *array)
75 {
76         memset(array, 0, sizeof *array);
77 }
78
79 WL_EXPORT void
80 wl_array_release(struct wl_array *array)
81 {
82         free(array->data);
83 }
84
85 WL_EXPORT void *
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 }
116
117 WL_EXPORT void
118 wl_array_copy(struct wl_array *array, struct wl_array *source)
119 {
120         array->size = 0;
121         wl_array_add(array, source->size);
122         memcpy(array->data, source->data, source->size);
123 }