8 // Type for a dynamic array. Use to declare a dynamic array.
9 // It is a pointer so it fits in st_table nicely. Designed
10 // to be fairly type-safe.
12 // NULL is a valid empty dynamic array.
15 // rb_darray(char) char_array = NULL;
16 // rb_darray_append(&char_array, 'e');
17 // printf("pushed %c\n", *rb_darray_ref(char_array, 0));
18 // rb_darray_free(char_array);
20 #define rb_darray(T) struct { rb_darray_meta_t meta; T data[]; } *
22 // Copy an element out of the array. Warning: not bounds checked.
24 // T rb_darray_get(rb_darray(T) ary, size_t idx);
26 #define rb_darray_get(ary, idx) ((ary)->data[(idx)])
28 // Assign to an element. Warning: not bounds checked.
30 // void rb_darray_set(rb_darray(T) ary, size_t idx, T element);
32 #define rb_darray_set(ary, idx, element) ((ary)->data[(idx)] = (element))
34 // Get a pointer to an element. Warning: not bounds checked.
36 // T *rb_darray_ref(rb_darray(T) ary, size_t idx);
38 #define rb_darray_ref(ary, idx) (&((ary)->data[(idx)]))
40 /* Copy a new element into the array. ptr_to_ary is evaluated multiple times.
42 * void rb_darray_append(rb_darray(T) *ptr_to_ary, T element);
44 #define rb_darray_append(ptr_to_ary, element) \
45 rb_darray_append_impl(ptr_to_ary, element, rb_darray_realloc_mul_add)
47 #define rb_darray_append_without_gc(ptr_to_ary, element) \
48 rb_darray_append_impl(ptr_to_ary, element, rb_darray_realloc_mul_add_without_gc)
50 #define rb_darray_append_impl(ptr_to_ary, element, realloc_func) do { \
51 rb_darray_ensure_space((ptr_to_ary), \
52 sizeof(**(ptr_to_ary)), \
53 sizeof((*(ptr_to_ary))->data[0]), \
55 rb_darray_set(*(ptr_to_ary), \
56 (*(ptr_to_ary))->meta.size, \
58 (*(ptr_to_ary))->meta.size++; \
61 #define rb_darray_insert_without_gc(ptr_to_ary, idx, element) do { \
62 rb_darray_ensure_space((ptr_to_ary), \
63 sizeof(**(ptr_to_ary)), \
64 sizeof((*(ptr_to_ary))->data[0]), \
65 rb_darray_realloc_mul_add_without_gc); \
67 rb_darray_ref(*(ptr_to_ary), idx + 1), \
68 rb_darray_ref(*(ptr_to_ary), idx), \
69 (*(ptr_to_ary))->data[0], \
70 rb_darray_size(*(ptr_to_ary)) - idx); \
71 rb_darray_set(*(ptr_to_ary), idx, element); \
72 (*(ptr_to_ary))->meta.size++; \
75 // Iterate over items of the array in a for loop
77 #define rb_darray_foreach(ary, idx_name, elem_ptr_var) \
78 for (size_t idx_name = 0; idx_name < rb_darray_size(ary) && ((elem_ptr_var) = rb_darray_ref(ary, idx_name)); ++idx_name)
80 // Iterate over valid indices in the array in a for loop
82 #define rb_darray_for(ary, idx_name) \
83 for (size_t idx_name = 0; idx_name < rb_darray_size(ary); ++idx_name)
85 /* Make a dynamic array of a certain size. All bytes backing the elements are set to zero.
86 * Return 1 on success and 0 on failure.
88 * Note that NULL is a valid empty dynamic array.
90 * void rb_darray_make(rb_darray(T) *ptr_to_ary, size_t size);
92 #define rb_darray_make(ptr_to_ary, size) \
93 rb_darray_make_impl((ptr_to_ary), size, sizeof(**(ptr_to_ary)), \
94 sizeof((*(ptr_to_ary))->data[0]), rb_darray_calloc_mul_add)
96 #define rb_darray_make_without_gc(ptr_to_ary, size) \
97 rb_darray_make_impl((ptr_to_ary), size, sizeof(**(ptr_to_ary)), \
98 sizeof((*(ptr_to_ary))->data[0]), rb_darray_calloc_mul_add_without_gc)
100 /* Resize the darray to a new capacity. The new capacity must be greater than
101 * or equal to the size of the darray.
103 * void rb_darray_resize_capa(rb_darray(T) *ptr_to_ary, size_t capa);
105 #define rb_darray_resize_capa_without_gc(ptr_to_ary, capa) \
106 rb_darray_resize_capa_impl((ptr_to_ary), capa, sizeof(**(ptr_to_ary)), \
107 sizeof((*(ptr_to_ary))->data[0]), rb_darray_realloc_mul_add_without_gc)
109 #define rb_darray_data_ptr(ary) ((ary)->data)
111 typedef struct rb_darray_meta
{
116 /* Set the size of the array to zero without freeing the backing memory.
117 * Allows reusing the same array. */
119 rb_darray_clear(void *ary
)
121 rb_darray_meta_t
*meta
= ary
;
127 // Get the size of the dynamic array.
130 rb_darray_size(const void *ary
)
132 const rb_darray_meta_t
*meta
= ary
;
133 return meta
? meta
->size
: 0;
138 rb_darray_pop(void *ary
, size_t count
)
140 rb_darray_meta_t
*meta
= ary
;
144 // Get the capacity of the dynamic array.
147 rb_darray_capa(const void *ary
)
149 const rb_darray_meta_t
*meta
= ary
;
150 return meta
? meta
->capa
: 0;
153 /* Free the dynamic array. */
155 rb_darray_free(void *ary
)
161 rb_darray_free_without_gc(void *ary
)
166 /* Internal function. Like rb_xcalloc_mul_add. */
168 rb_darray_calloc_mul_add(size_t x
, size_t y
, size_t z
)
170 size_t size
= rbimpl_size_add_or_raise(rbimpl_size_mul_or_raise(x
, y
), z
);
172 void *ptr
= xcalloc(1, size
);
173 RUBY_ASSERT(ptr
!= NULL
);
178 /* Internal function. Like rb_xcalloc_mul_add but does not trigger GC. */
180 rb_darray_calloc_mul_add_without_gc(size_t x
, size_t y
, size_t z
)
182 size_t size
= rbimpl_size_add_or_raise(rbimpl_size_mul_or_raise(x
, y
), z
);
184 void *ptr
= calloc(1, size
);
185 if (ptr
== NULL
) rb_bug("rb_darray_calloc_mul_add_without_gc: failed");
190 /* Internal function. Like rb_xrealloc_mul_add. */
192 rb_darray_realloc_mul_add(void *orig_ptr
, size_t x
, size_t y
, size_t z
)
194 size_t size
= rbimpl_size_add_or_raise(rbimpl_size_mul_or_raise(x
, y
), z
);
196 void *ptr
= xrealloc(orig_ptr
, size
);
197 RUBY_ASSERT(ptr
!= NULL
);
202 /* Internal function. Like rb_xrealloc_mul_add but does not trigger GC. */
204 rb_darray_realloc_mul_add_without_gc(void *orig_ptr
, size_t x
, size_t y
, size_t z
)
206 size_t size
= rbimpl_size_add_or_raise(rbimpl_size_mul_or_raise(x
, y
), z
);
208 void *ptr
= realloc(orig_ptr
, size
);
209 if (ptr
== NULL
) rb_bug("rb_darray_realloc_mul_add_without_gc: failed");
214 /* Internal function. Resizes the capacity of a darray. The new capacity must
215 * be greater than or equal to the size of the darray. */
217 rb_darray_resize_capa_impl(void *ptr_to_ary
, size_t new_capa
, size_t header_size
, size_t element_size
,
218 void *(*realloc_mul_add_impl
)(void *, size_t, size_t, size_t))
220 rb_darray_meta_t
**ptr_to_ptr_to_meta
= ptr_to_ary
;
221 rb_darray_meta_t
*meta
= *ptr_to_ptr_to_meta
;
223 rb_darray_meta_t
*new_ary
= realloc_mul_add_impl(meta
, new_capa
, element_size
, header_size
);
226 /* First allocation. Initialize size. On subsequence allocations
227 * realloc takes care of carrying over the size. */
231 RUBY_ASSERT(new_ary
->size
<= new_capa
);
233 new_ary
->capa
= new_capa
;
235 // We don't have access to the type of the dynamic array in function context.
236 // Write out result with memcpy to avoid strict aliasing issue.
237 memcpy(ptr_to_ary
, &new_ary
, sizeof(new_ary
));
241 // Ensure there is space for one more element.
242 // Note: header_size can be bigger than sizeof(rb_darray_meta_t) when T is __int128_t, for example.
244 rb_darray_ensure_space(void *ptr_to_ary
, size_t header_size
, size_t element_size
,
245 void *(*realloc_mul_add_impl
)(void *, size_t, size_t, size_t))
247 rb_darray_meta_t
**ptr_to_ptr_to_meta
= ptr_to_ary
;
248 rb_darray_meta_t
*meta
= *ptr_to_ptr_to_meta
;
249 size_t current_capa
= rb_darray_capa(meta
);
250 if (rb_darray_size(meta
) < current_capa
) return;
252 // Double the capacity
253 size_t new_capa
= current_capa
== 0 ? 1 : current_capa
* 2;
255 rb_darray_resize_capa_impl(ptr_to_ary
, new_capa
, header_size
, element_size
, realloc_mul_add_impl
);
259 rb_darray_make_impl(void *ptr_to_ary
, size_t array_size
, size_t header_size
, size_t element_size
,
260 void *(*calloc_mul_add_impl
)(size_t, size_t, size_t))
262 rb_darray_meta_t
**ptr_to_ptr_to_meta
= ptr_to_ary
;
263 if (array_size
== 0) {
264 *ptr_to_ptr_to_meta
= NULL
;
268 rb_darray_meta_t
*meta
= calloc_mul_add_impl(array_size
, element_size
, header_size
);
270 meta
->size
= array_size
;
271 meta
->capa
= array_size
;
273 // We don't have access to the type of the dynamic array in function context.
274 // Write out result with memcpy to avoid strict aliasing issue.
275 memcpy(ptr_to_ary
, &meta
, sizeof(meta
));
278 #endif /* RUBY_DARRAY_H */