Ruby 4.1.0dev (2026-04-24 revision 0a1cb0a822b6665b972539da41209569b5d9de6e)
darray.h (0a1cb0a822b6665b972539da41209569b5d9de6e)
1#ifndef RUBY_DARRAY_H
2#define RUBY_DARRAY_H
3
4#include <stdint.h>
5#include <stddef.h>
6#include <stdlib.h>
7#include "ruby/ruby.h"
8
9// Type for a dynamic array. Use to declare a dynamic array.
10// It is a pointer so it fits in st_table nicely. Designed
11// to be fairly type-safe.
12//
13// NULL is a valid empty dynamic array.
14//
15// Example:
16// rb_darray(char) char_array = NULL;
17// rb_darray_append(&char_array, 'e');
18// printf("pushed %c\n", *rb_darray_ref(char_array, 0));
19// rb_darray_free(char_array);
20//
21#define rb_darray(T) struct { rb_darray_meta_t meta; T data[]; } *
22
23// Copy an element out of the array. Warning: not bounds checked.
24//
25// T rb_darray_get(rb_darray(T) ary, size_t idx);
26//
27#define rb_darray_get(ary, idx) ((ary)->data[(idx)])
28
29// Assign to an element. Warning: not bounds checked.
30//
31// void rb_darray_set(rb_darray(T) ary, size_t idx, T element);
32//
33#define rb_darray_set(ary, idx, element) ((ary)->data[(idx)] = (element))
34
35// Get a pointer to an element. Warning: not bounds checked.
36//
37// T *rb_darray_ref(rb_darray(T) ary, size_t idx);
38//
39#define rb_darray_ref(ary, idx) (&((ary)->data[(idx)]))
40
41/* Copy a new element into the array. ptr_to_ary is evaluated multiple times.
42 *
43 * void rb_darray_append(rb_darray(T) *ptr_to_ary, T element);
44 */
45#define rb_darray_append(ptr_to_ary, element) \
46 rb_darray_append_impl(ptr_to_ary, element, rb_darray_realloc_mul_add)
47
48#define rb_darray_append_without_gc(ptr_to_ary, element) \
49 rb_darray_append_impl(ptr_to_ary, element, rb_darray_realloc_mul_add_without_gc)
50
51#define rb_darray_append_impl(ptr_to_ary, element, realloc_func) do { \
52 rb_darray_ensure_space((ptr_to_ary), \
53 sizeof(**(ptr_to_ary)), \
54 sizeof((*(ptr_to_ary))->data[0]), \
55 realloc_func); \
56 rb_darray_set(*(ptr_to_ary), \
57 (*(ptr_to_ary))->meta.size, \
58 (element)); \
59 (*(ptr_to_ary))->meta.size++; \
60} while (0)
61
62#define rb_darray_insert_without_gc(ptr_to_ary, idx, element) do { \
63 rb_darray_ensure_space((ptr_to_ary), \
64 sizeof(**(ptr_to_ary)), \
65 sizeof((*(ptr_to_ary))->data[0]), \
66 rb_darray_realloc_mul_add_without_gc); \
67 MEMMOVE( \
68 rb_darray_ref(*(ptr_to_ary), idx + 1), \
69 rb_darray_ref(*(ptr_to_ary), idx), \
70 (*(ptr_to_ary))->data[0], \
71 rb_darray_size(*(ptr_to_ary)) - idx); \
72 rb_darray_set(*(ptr_to_ary), idx, element); \
73 (*(ptr_to_ary))->meta.size++; \
74} while (0)
75
76// Iterate over items of the array in a for loop
77//
78#define rb_darray_foreach(ary, idx_name, elem_ptr_var) \
79 for (size_t idx_name = 0; idx_name < rb_darray_size(ary) && ((elem_ptr_var) = rb_darray_ref(ary, idx_name)); ++idx_name)
80
81// Iterate over valid indices in the array in a for loop
82//
83#define rb_darray_for(ary, idx_name) \
84 for (size_t idx_name = 0; idx_name < rb_darray_size(ary); ++idx_name)
85
86/* Make a dynamic array of a certain size. All bytes backing the elements are set to zero.
87 * Return 1 on success and 0 on failure.
88 *
89 * Note that NULL is a valid empty dynamic array.
90 *
91 * void rb_darray_make(rb_darray(T) *ptr_to_ary, size_t size);
92 */
93#define rb_darray_make(ptr_to_ary, size) \
94 rb_darray_make_impl((ptr_to_ary), size, sizeof(**(ptr_to_ary)), \
95 sizeof((*(ptr_to_ary))->data[0]), rb_darray_calloc_mul_add)
96
97#define rb_darray_make_without_gc(ptr_to_ary, size) \
98 rb_darray_make_impl((ptr_to_ary), size, sizeof(**(ptr_to_ary)), \
99 sizeof((*(ptr_to_ary))->data[0]), rb_darray_calloc_mul_add_without_gc)
100
101/* Resize the darray to a new capacity. The new capacity must be greater than
102 * or equal to the size of the darray.
103 *
104 * void rb_darray_resize_capa(rb_darray(T) *ptr_to_ary, size_t capa);
105 */
106#define rb_darray_resize_capa_without_gc(ptr_to_ary, capa) \
107 rb_darray_resize_capa_impl((ptr_to_ary), capa, sizeof(**(ptr_to_ary)), \
108 sizeof((*(ptr_to_ary))->data[0]), rb_darray_realloc_mul_add_without_gc)
109
110#define rb_darray_data_ptr(ary) ((ary)->data)
111
112typedef struct rb_darray_meta {
113 size_t size;
114 size_t capa;
116
117/* Set the size of the array to zero without freeing the backing memory.
118 * Allows reusing the same array. */
119static inline void
120rb_darray_clear(void *ary)
121{
122 rb_darray_meta_t *meta = ary;
123 if (meta) {
124 meta->size = 0;
125 }
126}
127
128// Get the size of the dynamic array.
129//
130static inline size_t
131rb_darray_size(const void *ary)
132{
133 const rb_darray_meta_t *meta = ary;
134 return meta ? meta->size : 0;
135}
136
137/* Estimate of the amount of memory used by this darray.
138 * Useful for TypedData objects. */
139#define rb_darray_memsize(ary) (sizeof(*(ary)) + (rb_darray_size(ary) * sizeof((ary)->data[0])))
140
141static inline void
142rb_darray_pop(void *ary, size_t count)
143{
144 rb_darray_meta_t *meta = ary;
145 meta->size -= count;
146}
147
148// Get the capacity of the dynamic array.
149//
150static inline size_t
151rb_darray_capa(const void *ary)
152{
153 const rb_darray_meta_t *meta = ary;
154 return meta ? meta->capa : 0;
155}
156
157/* Free the dynamic array. */
158static inline void
159rb_darray_free(void *ary)
160{
161 xfree(ary);
162}
163
164static inline void
165rb_darray_free_sized0(void *ary, size_t element_size)
166{
167 const rb_darray_meta_t *meta = ary;
168 if (meta) {
169 ruby_xfree_sized(ary, sizeof(*meta) + (element_size * meta->capa));
170 }
171}
172#define rb_darray_free_sized(ary, T) rb_darray_free_sized0((ary), sizeof(T))
173
174static inline void
175rb_darray_free_without_gc(void *ary)
176{
177 free(ary);
178}
179
180/* Internal function. Like rb_xcalloc_mul_add. */
181static inline void *
182rb_darray_calloc_mul_add(size_t x, size_t y, size_t z)
183{
184 size_t size = rbimpl_size_add_or_raise(rbimpl_size_mul_or_raise(x, y), z);
185
186 void *ptr = xcalloc(1, size);
187 RUBY_ASSERT(ptr != NULL);
188
189 return ptr;
190}
191
192/* Internal function. Like rb_xcalloc_mul_add but does not trigger GC. */
193static inline void *
194rb_darray_calloc_mul_add_without_gc(size_t x, size_t y, size_t z)
195{
196 size_t size = rbimpl_size_add_or_raise(rbimpl_size_mul_or_raise(x, y), z);
197
198 void *ptr = calloc(1, size);
199 if (ptr == NULL) rb_bug("rb_darray_calloc_mul_add_without_gc: failed");
200
201 return ptr;
202}
203
204void *ruby_xrealloc_sized(void *ptr, size_t new_size, size_t old_size);
205
206/* Internal function. Like rb_xrealloc_mul_add. */
207static inline void *
208rb_darray_realloc_mul_add(void *orig_ptr, size_t capa, size_t element_size, size_t header_size)
209{
210 size_t size = rbimpl_size_add_or_raise(rbimpl_size_mul_or_raise(capa, element_size), header_size);
211 size_t old_size = (rb_darray_capa(orig_ptr) * element_size) + header_size; // We know it won't overflow
212
213 void *ptr = ruby_xrealloc_sized(orig_ptr, size, old_size);
214 RUBY_ASSERT(ptr != NULL);
215
216 return ptr;
217}
218
219/* Internal function. Like rb_xrealloc_mul_add but does not trigger GC. */
220static inline void *
221rb_darray_realloc_mul_add_without_gc(void *orig_ptr, size_t x, size_t y, size_t z)
222{
223 size_t size = rbimpl_size_add_or_raise(rbimpl_size_mul_or_raise(x, y), z);
224
225 void *ptr = realloc(orig_ptr, size);
226 if (ptr == NULL) rb_bug("rb_darray_realloc_mul_add_without_gc: failed");
227
228 return ptr;
229}
230
231/* Internal function. Resizes the capacity of a darray. The new capacity must
232 * be greater than or equal to the size of the darray. */
233static inline void
234rb_darray_resize_capa_impl(void *ptr_to_ary, size_t new_capa, size_t header_size, size_t element_size,
235 void *(*realloc_mul_add_impl)(void *, size_t, size_t, size_t))
236{
237 rb_darray_meta_t **ptr_to_ptr_to_meta = ptr_to_ary;
238 rb_darray_meta_t *meta = *ptr_to_ptr_to_meta;
239
240 rb_darray_meta_t *new_ary = realloc_mul_add_impl(meta, new_capa, element_size, header_size);
241
242 if (meta == NULL) {
243 /* First allocation. Initialize size. On subsequence allocations
244 * realloc takes care of carrying over the size. */
245 new_ary->size = 0;
246 }
247
248 RUBY_ASSERT(new_ary->size <= new_capa);
249
250 new_ary->capa = new_capa;
251
252 // We don't have access to the type of the dynamic array in function context.
253 // Write out result with memcpy to avoid strict aliasing issue.
254 memcpy(ptr_to_ary, &new_ary, sizeof(new_ary));
255}
256
257// Internal function
258// Ensure there is space for one more element.
259// Note: header_size can be bigger than sizeof(rb_darray_meta_t) when T is __int128_t, for example.
260static inline void
261rb_darray_ensure_space(void *ptr_to_ary, size_t header_size, size_t element_size,
262 void *(*realloc_mul_add_impl)(void *, size_t, size_t, size_t))
263{
264 rb_darray_meta_t **ptr_to_ptr_to_meta = ptr_to_ary;
265 rb_darray_meta_t *meta = *ptr_to_ptr_to_meta;
266 size_t current_capa = rb_darray_capa(meta);
267 if (rb_darray_size(meta) < current_capa) return;
268
269 // Double the capacity
270 size_t new_capa = current_capa == 0 ? 1 : current_capa * 2;
271
272 rb_darray_resize_capa_impl(ptr_to_ary, new_capa, header_size, element_size, realloc_mul_add_impl);
273}
274
275static inline void
276rb_darray_make_impl(void *ptr_to_ary, size_t array_size, size_t header_size, size_t element_size,
277 void *(*calloc_mul_add_impl)(size_t, size_t, size_t))
278{
279 rb_darray_meta_t **ptr_to_ptr_to_meta = ptr_to_ary;
280 if (array_size == 0) {
281 *ptr_to_ptr_to_meta = NULL;
282 return;
283 }
284
285 rb_darray_meta_t *meta = calloc_mul_add_impl(array_size, element_size, header_size);
286
287 meta->size = array_size;
288 meta->capa = array_size;
289
290 // We don't have access to the type of the dynamic array in function context.
291 // Write out result with memcpy to avoid strict aliasing issue.
292 memcpy(ptr_to_ary, &meta, sizeof(meta));
293}
294
295#endif /* RUBY_DARRAY_H */
#define RUBY_ASSERT(...)
Asserts that the given expression is truthy if and only if RUBY_DEBUG is truthy.
Definition assert.h:219
#define xfree
Old name of ruby_xfree.
Definition xmalloc.h:58
#define xcalloc
Old name of ruby_xcalloc.
Definition xmalloc.h:55
int capa
Designed capacity of the buffer.
Definition io.h:11