20#define rb_darray(T) struct { rb_darray_meta_t meta; T data[]; } *
26#define rb_darray_get(ary, idx) ((ary)->data[(idx)])
32#define rb_darray_set(ary, idx, element) ((ary)->data[(idx)] = (element))
38#define rb_darray_ref(ary, idx) (&((ary)->data[(idx)]))
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++; \
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)
82#define rb_darray_for(ary, idx_name) \
83 for (size_t idx_name = 0; idx_name < rb_darray_size(ary); ++idx_name)
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)
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)
119rb_darray_clear(
void *ary)
130rb_darray_size(
const void *ary)
133 return meta ? meta->size : 0;
138rb_darray_pop(
void *ary,
size_t count)
147rb_darray_capa(
const void *ary)
150 return meta ? meta->capa : 0;
155rb_darray_free(
void *ary)
161rb_darray_free_without_gc(
void *ary)
168rb_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);
180rb_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");
192rb_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);
204rb_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");
217rb_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))
223 rb_darray_meta_t *new_ary = realloc_mul_add_impl(meta, new_capa, element_size, header_size);
233 new_ary->capa = new_capa;
237 memcpy(ptr_to_ary, &new_ary,
sizeof(new_ary));
244rb_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))
249 size_t current_capa = rb_darray_capa(meta);
250 if (rb_darray_size(meta) < current_capa)
return;
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);
259rb_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))
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;
275 memcpy(ptr_to_ary, &meta,
sizeof(meta));
#define RUBY_ASSERT(...)
Asserts that the given expression is truthy if and only if RUBY_DEBUG is truthy.
#define xfree
Old name of ruby_xfree.
#define xrealloc
Old name of ruby_xrealloc.
#define xcalloc
Old name of ruby_xcalloc.