769c38205460374216397cc6f0fb599ed58eb19f
[mesa.git] / src / util / u_dynarray.h
1 /**************************************************************************
2 *
3 * Copyright 2010 Luca Barbieri
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial
15 * portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20 * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
21 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 *
25 **************************************************************************/
26
27 #ifndef U_DYNARRAY_H
28 #define U_DYNARRAY_H
29
30 #include <stdlib.h>
31 #include <string.h>
32 #include "ralloc.h"
33
34 #ifdef __cplusplus
35 extern "C" {
36 #endif
37
38 /* A zero-initialized version of this is guaranteed to represent an
39 * empty array.
40 *
41 * Also, size <= capacity and data != 0 if and only if capacity != 0
42 * capacity will always be the allocation size of data
43 */
44 struct util_dynarray
45 {
46 void *mem_ctx;
47 void *data;
48 unsigned size;
49 unsigned capacity;
50 };
51
52 static inline void
53 util_dynarray_init(struct util_dynarray *buf, void *mem_ctx)
54 {
55 memset(buf, 0, sizeof(*buf));
56 buf->mem_ctx = mem_ctx;
57 }
58
59 static inline void
60 util_dynarray_fini(struct util_dynarray *buf)
61 {
62 if (buf->data) {
63 if (buf->mem_ctx) {
64 ralloc_free(buf->data);
65 } else {
66 free(buf->data);
67 }
68 util_dynarray_init(buf, buf->mem_ctx);
69 }
70 }
71
72 static inline void
73 util_dynarray_clear(struct util_dynarray *buf)
74 {
75 buf->size = 0;
76 }
77
78 #define DYN_ARRAY_INITIAL_SIZE 64
79
80 MUST_CHECK static inline void *
81 util_dynarray_ensure_cap(struct util_dynarray *buf, unsigned newcap)
82 {
83 if (newcap > buf->capacity) {
84 unsigned capacity = MAX3(DYN_ARRAY_INITIAL_SIZE, buf->capacity * 2, newcap);
85 void *data;
86
87 if (buf->mem_ctx) {
88 data = reralloc_size(buf->mem_ctx, buf->data, capacity);
89 } else {
90 data = realloc(buf->data, capacity);
91 }
92 if (!data)
93 return 0;
94
95 buf->data = data;
96 buf->capacity = capacity;
97 }
98
99 return (void *)((char *)buf->data + buf->size);
100 }
101
102 static inline void *
103 util_dynarray_grow_cap(struct util_dynarray *buf, int diff)
104 {
105 return util_dynarray_ensure_cap(buf, buf->size + diff);
106 }
107
108 /* use util_dynarray_trim to reduce the allocated storage */
109 static inline void *
110 util_dynarray_resize(struct util_dynarray *buf, unsigned newsize)
111 {
112 void *p = util_dynarray_ensure_cap(buf, newsize);
113 buf->size = newsize;
114
115 return p;
116 }
117
118 static inline void
119 util_dynarray_clone(struct util_dynarray *buf, void *mem_ctx,
120 struct util_dynarray *from_buf)
121 {
122 util_dynarray_init(buf, mem_ctx);
123 util_dynarray_resize(buf, from_buf->size);
124 memcpy(buf->data, from_buf->data, from_buf->size);
125 }
126
127 static inline void *
128 util_dynarray_grow(struct util_dynarray *buf, int diff)
129 {
130 return util_dynarray_resize(buf, buf->size + diff);
131 }
132
133 static inline void
134 util_dynarray_trim(struct util_dynarray *buf)
135 {
136 if (buf->size != buf->capacity) {
137 if (buf->size) {
138 if (buf->mem_ctx) {
139 buf->data = reralloc_size(buf->mem_ctx, buf->data, buf->size);
140 } else {
141 buf->data = realloc(buf->data, buf->size);
142 }
143 buf->capacity = buf->size;
144 } else {
145 if (buf->mem_ctx) {
146 ralloc_free(buf->data);
147 } else {
148 free(buf->data);
149 }
150 buf->data = NULL;
151 buf->capacity = 0;
152 }
153 }
154 }
155
156 #define util_dynarray_append(buf, type, v) do {type __v = (v); memcpy(util_dynarray_grow((buf), sizeof(type)), &__v, sizeof(type));} while(0)
157 #define util_dynarray_top_ptr(buf, type) (type*)((char*)(buf)->data + (buf)->size - sizeof(type))
158 #define util_dynarray_top(buf, type) *util_dynarray_top_ptr(buf, type)
159 #define util_dynarray_pop_ptr(buf, type) (type*)((char*)(buf)->data + ((buf)->size -= sizeof(type)))
160 #define util_dynarray_pop(buf, type) *util_dynarray_pop_ptr(buf, type)
161 #define util_dynarray_contains(buf, type) ((buf)->size >= sizeof(type))
162 #define util_dynarray_element(buf, type, idx) ((type*)(buf)->data + (idx))
163 #define util_dynarray_begin(buf) ((buf)->data)
164 #define util_dynarray_end(buf) ((void*)util_dynarray_element((buf), char, (buf)->size))
165 #define util_dynarray_num_elements(buf, type) ((buf)->size / sizeof(type))
166
167 #define util_dynarray_foreach(buf, type, elem) \
168 for (type *elem = (type *)(buf)->data; \
169 elem < (type *)((char *)(buf)->data + (buf)->size); elem++)
170
171 #define util_dynarray_foreach_reverse(buf, type, elem) \
172 if ((buf)->size > 0) \
173 for (type *elem = util_dynarray_top_ptr(buf, type); \
174 elem; \
175 elem = elem > (type *)(buf)->data ? elem - 1 : NULL)
176
177 #define util_dynarray_delete_unordered(buf, type, v) \
178 do { \
179 unsigned num_elements = (buf)->size / sizeof(type); \
180 unsigned i; \
181 for (i = 0; i < num_elements; i++) { \
182 type __v = *util_dynarray_element((buf), type, (i)); \
183 if (v == __v) { \
184 memcpy(util_dynarray_element((buf), type, (i)), \
185 util_dynarray_pop_ptr((buf), type), sizeof(type)); \
186 break; \
187 } \
188 } \
189 } while (0)
190
191 #ifdef __cplusplus
192 }
193 #endif
194
195 #endif /* U_DYNARRAY_H */
196