blob: a287cce022bbd2bb228d1a03c1246fa8008805ff [file] [log] [blame]
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05001/*
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øgsberg97f1ebe2008-09-30 09:46:10 -040023#include <stdlib.h>
Kristian Høgsbergfabd4392008-12-22 18:06:49 -050024#include <stdint.h>
Kristian Høgsberg864c4682008-12-12 11:05:17 -050025#include <string.h>
Kristian Høgsbergfabd4392008-12-22 18:06:49 -050026#include "wayland-util.h"
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -040027
Kristian Høgsberg59fa3462009-09-18 09:47:34 -040028WL_EXPORT void
29wl_list_init(struct wl_list *list)
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -050030{
31 list->prev = list;
32 list->next = list;
33}
34
Kristian Høgsberg59fa3462009-09-18 09:47:34 -040035WL_EXPORT void
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -050036wl_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øgsberg59fa3462009-09-18 09:47:34 -040044WL_EXPORT void
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -050045wl_list_remove(struct wl_list *elm)
46{
47 elm->prev->next = elm->next;
48 elm->next->prev = elm->prev;
49}
50
Kristian Høgsberg59fa3462009-09-18 09:47:34 -040051WL_EXPORT int
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -050052wl_list_length(struct wl_list *list)
53{
54 struct wl_list *e;
55 int count;
56
Kristian Høgsberg3f169562008-12-14 15:52:34 -050057 count = 0;
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -050058 e = list->next;
59 while (e != list) {
60 e = e->next;
61 count++;
62 }
63
64 return count;
65}
Kristian Høgsberg4a298902008-11-28 18:35:25 -050066
Kristian Høgsberg59fa3462009-09-18 09:47:34 -040067WL_EXPORT int
Kristian Høgsberg4a298902008-11-28 18:35:25 -050068wl_list_empty(struct wl_list *list)
69{
70 return list->next == list;
71}
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -050072
Kristian Høgsberg59fa3462009-09-18 09:47:34 -040073WL_EXPORT void
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -050074wl_array_init(struct wl_array *array)
75{
76 memset(array, 0, sizeof *array);
77}
78
Kristian Høgsberg59fa3462009-09-18 09:47:34 -040079WL_EXPORT void
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -050080wl_array_release(struct wl_array *array)
81{
82 free(array->data);
83}
84
Kristian Høgsberg59fa3462009-09-18 09:47:34 -040085WL_EXPORT void *
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -050086wl_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}