Ruby: darray.h Source File

Ruby 3.2.5p208 (2024-07-26 revision 31d0f1a2e7dbfb60731d1f05b868e1d578cda493)

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// 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.
11//
12// NULL is a valid empty dynamic array.
13//
14// Example:
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);
19//
20#define rb_darray(T) struct { rb_darray_meta_t meta; T data[]; } *
21
22// Copy an element out of the array. Warning: not bounds checked.
23//
24// T rb_darray_get(rb_darray(T) ary, size_t idx);
25//
26#define rb_darray_get(ary, idx) ((ary)->data[(idx)])
27
28// Assign to an element. Warning: not bounds checked.
29//
30// void rb_darray_set(rb_darray(T) ary, size_t idx, T element);
31//
32#define rb_darray_set(ary, idx, element) ((ary)->data[(idx)] = (element))
33
34// Get a pointer to an element. Warning: not bounds checked.
35//
36// T *rb_darray_ref(rb_darray(T) ary, size_t idx);
37//
38#define rb_darray_ref(ary, idx) (&((ary)->data[(idx)]))
39
40// Copy a new element into the array. ptr_to_ary is evaluated multiple times.
41//
42// void rb_darray_append(rb_darray(T) *ptr_to_ary, T element);
43//
44#define rb_darray_append(ptr_to_ary, element) do { \
45 rb_darray_ensure_space((ptr_to_ary), sizeof(**(ptr_to_ary)), \
46 sizeof((*(ptr_to_ary))->data[0])); \
47 rb_darray_set(*(ptr_to_ary), \
48 (*(ptr_to_ary))->meta.size, \
49 (element)); \
50 (*(ptr_to_ary))->meta.size++; \
51} while (0)
52
53
54// Last element of the array
55//
56#define rb_darray_back(ary) ((ary)->data[(ary)->meta.size - 1])
57
58// Remove the last element of the array.
59//
60#define rb_darray_pop_back(ary) ((ary)->meta.size--)
61
62// Remove element at idx and replace it by the last element
63#define rb_darray_remove_unordered(ary, idx) do { \
64 rb_darray_set(ary, idx, rb_darray_back(ary)); \
65 rb_darray_pop_back(ary); \
66} while (0);
67
68// Iterate over items of the array in a for loop
69//
70#define rb_darray_foreach(ary, idx_name, elem_ptr_var) \
71 for (size_t idx_name = 0; idx_name < rb_darray_size(ary) && ((elem_ptr_var) = rb_darray_ref(ary, idx_name)); ++idx_name)
72
73// Iterate over valid indicies in the array in a for loop
74//
75#define rb_darray_for(ary, idx_name) \
76 for (size_t idx_name = 0; idx_name < rb_darray_size(ary); ++idx_name)
77
78// Make a dynamic array of a certain size. All bytes backing the elements are set to zero.
79//
80// Note that NULL is a valid empty dynamic array.
81//
82// void rb_darray_make(rb_darray(T) *ptr_to_ary, size_t size);
83//
84#define rb_darray_make(ptr_to_ary, size) \
85 rb_darray_make_impl((ptr_to_ary), size, sizeof(**(ptr_to_ary)), \
86 sizeof((*(ptr_to_ary))->data[0]))
87
88#define rb_darray_data_ptr(ary) ((ary)->data)
89
90// Set the size of the array to zero without freeing the backing memory.
91// Allows reusing the same array.
92//
93#define rb_darray_clear(ary) (ary->meta.size = 0)
94
95 typedef struct rb_darray_meta {
96 size_t size;
97 size_t capa;
98} rb_darray_meta_t;
99
100// Get the size of the dynamic array.
101//
102static inline size_t
103rb_darray_size(const void *ary)
104{
105 const rb_darray_meta_t *meta = ary;
106 return meta ? meta->size : 0;
107}
108
109// Get the capacity of the dynamic array.
110//
111static inline size_t
112rb_darray_capa(const void *ary)
113{
114 const rb_darray_meta_t *meta = ary;
115 return meta ? meta->capa : 0;
116}
117
118// Free the dynamic array.
119//
120static inline void
121rb_darray_free(void *ary)
122{
123 rb_darray_meta_t *meta = ary;
124 ruby_sized_xfree(ary, meta->capa);
125}
126
127// Internal function
128// Ensure there is space for one more element.
129// Note: header_size can be bigger than sizeof(rb_darray_meta_t) when T is __int128_t, for example.
130static inline void
131rb_darray_ensure_space(void *ptr_to_ary, size_t header_size, size_t element_size)
132{
133 rb_darray_meta_t **ptr_to_ptr_to_meta = ptr_to_ary;
134 rb_darray_meta_t *meta = *ptr_to_ptr_to_meta;
135 size_t current_capa = rb_darray_capa(meta);
136 if (rb_darray_size(meta) < current_capa) return;
137
138 // Double the capacity
139 size_t new_capa = current_capa == 0 ? 1 : current_capa * 2;
140
141 rb_darray_meta_t *doubled_ary = rb_xrealloc_mul_add(meta, new_capa, element_size, header_size);
142 // rb_xrealloc functions guarantee that NULL is not returned
143 assert(doubled_ary != NULL);
144
145 if (meta == NULL) {
146 // First allocation. Initialize size. On subsequence allocations
147 // realloc takes care of carrying over the size.
148 doubled_ary->size = 0;
149 }
150
151 doubled_ary->capa = new_capa;
152
153 // We don't have access to the type of the dynamic array in function context.
154 // Write out result with memcpy to avoid strict aliasing issue.
155 memcpy(ptr_to_ary, &doubled_ary, sizeof(doubled_ary));
156}
157
158static inline void
159rb_darray_make_impl(void *ptr_to_ary, size_t array_size, size_t header_size, size_t element_size)
160{
161 rb_darray_meta_t **ptr_to_ptr_to_meta = ptr_to_ary;
162 if (array_size == 0) {
163 *ptr_to_ptr_to_meta = NULL;
164 return;
165 }
166
167 rb_darray_meta_t *meta = rb_xcalloc_mul_add(array_size, element_size, header_size);
168 // rb_xcalloc functions guarantee that NULL is not returned
169 assert(meta != NULL);
170
171 meta->size = array_size;
172 meta->capa = array_size;
173
174 // We don't have access to the type of the dynamic array in function context.
175 // Write out result with memcpy to avoid strict aliasing issue.
176 memcpy(ptr_to_ary, &meta, sizeof(meta));
177}
178
179#endif /* RUBY_DARRAY_H */
rb_darray_meta
Definition darray.h:95