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