Ruby: darray.h Source File

Ruby 3.3.4p94 (2024-07-09 revision be1089c8ec5ba40e09b1553e36b3174bf4014d9d)

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