Tempfile document updated.
[ruby.git] / darray.h
blobd24e3c3eb5f5c323aaf5f4ef8a63f435a4cefbf1
1 #ifndef RUBY_DARRAY_H
2 #define RUBY_DARRAY_H
4 #include <stdint.h>
5 #include <stddef.h>
6 #include <stdlib.h>
8 #include "internal/bits.h"
10 // Type for a dynamic array. Use to declare a dynamic array.
11 // It is a pointer so it fits in st_table nicely. Designed
12 // to be fairly type-safe.
14 // NULL is a valid empty dynamic array.
16 // Example:
17 // rb_darray(char) char_array = NULL;
18 // rb_darray_append(&char_array, 'e');
19 // printf("pushed %c\n", *rb_darray_ref(char_array, 0));
20 // rb_darray_free(char_array);
22 #define rb_darray(T) struct { rb_darray_meta_t meta; T data[]; } *
24 // Copy an element out of the array. Warning: not bounds checked.
26 // T rb_darray_get(rb_darray(T) ary, size_t idx);
28 #define rb_darray_get(ary, idx) ((ary)->data[(idx)])
30 // Assign to an element. Warning: not bounds checked.
32 // void rb_darray_set(rb_darray(T) ary, size_t idx, T element);
34 #define rb_darray_set(ary, idx, element) ((ary)->data[(idx)] = (element))
36 // Get a pointer to an element. Warning: not bounds checked.
38 // T *rb_darray_ref(rb_darray(T) ary, size_t idx);
40 #define rb_darray_ref(ary, idx) (&((ary)->data[(idx)]))
42 /* Copy a new element into the array. ptr_to_ary is evaluated multiple times.
44 * void rb_darray_append(rb_darray(T) *ptr_to_ary, T element);
46 #define rb_darray_append(ptr_to_ary, element) \
47 rb_darray_append_impl(ptr_to_ary, element)
49 #define rb_darray_append_impl(ptr_to_ary, element) do { \
50 rb_darray_ensure_space((ptr_to_ary), \
51 sizeof(**(ptr_to_ary)), \
52 sizeof((*(ptr_to_ary))->data[0])); \
53 rb_darray_set(*(ptr_to_ary), \
54 (*(ptr_to_ary))->meta.size, \
55 (element)); \
56 (*(ptr_to_ary))->meta.size++; \
57 } while (0)
59 // Iterate over items of the array in a for loop
61 #define rb_darray_foreach(ary, idx_name, elem_ptr_var) \
62 for (size_t idx_name = 0; idx_name < rb_darray_size(ary) && ((elem_ptr_var) = rb_darray_ref(ary, idx_name)); ++idx_name)
64 // Iterate over valid indices in the array in a for loop
66 #define rb_darray_for(ary, idx_name) \
67 for (size_t idx_name = 0; idx_name < rb_darray_size(ary); ++idx_name)
69 /* Make a dynamic array of a certain size. All bytes backing the elements are set to zero.
70 * Return 1 on success and 0 on failure.
72 * Note that NULL is a valid empty dynamic array.
74 * void rb_darray_make(rb_darray(T) *ptr_to_ary, size_t size);
76 #define rb_darray_make(ptr_to_ary, size) \
77 rb_darray_make_impl((ptr_to_ary), size, sizeof(**(ptr_to_ary)), sizeof((*(ptr_to_ary))->data[0]))
79 /* Resize the darray to a new capacity. The new capacity must be greater than
80 * or equal to the size of the darray.
82 * void rb_darray_resize_capa(rb_darray(T) *ptr_to_ary, size_t capa);
84 #define rb_darray_resize_capa(ptr_to_ary, capa) \
85 rb_darray_resize_capa_impl((ptr_to_ary), capa, sizeof(**(ptr_to_ary)), sizeof((*(ptr_to_ary))->data[0]))
87 #define rb_darray_data_ptr(ary) ((ary)->data)
89 typedef struct rb_darray_meta {
90 size_t size;
91 size_t capa;
92 } rb_darray_meta_t;
94 /* Set the size of the array to zero without freeing the backing memory.
95 * Allows reusing the same array. */
96 static inline void
97 rb_darray_clear(void *ary)
99 rb_darray_meta_t *meta = ary;
100 if (meta) {
101 meta->size = 0;
105 // Get the size of the dynamic array.
107 static inline size_t
108 rb_darray_size(const void *ary)
110 const rb_darray_meta_t *meta = ary;
111 return meta ? meta->size : 0;
114 // Get the capacity of the dynamic array.
116 static inline size_t
117 rb_darray_capa(const void *ary)
119 const rb_darray_meta_t *meta = ary;
120 return meta ? meta->capa : 0;
123 /* Free the dynamic array. */
124 static inline void
125 rb_darray_free(void *ary)
127 xfree(ary);
130 /* Internal function. Resizes the capacity of a darray. The new capacity must
131 * be greater than or equal to the size of the darray. */
132 static inline void
133 rb_darray_resize_capa_impl(void *ptr_to_ary, size_t new_capa, size_t header_size, size_t element_size)
135 rb_darray_meta_t **ptr_to_ptr_to_meta = ptr_to_ary;
136 rb_darray_meta_t *meta = *ptr_to_ptr_to_meta;
138 rb_darray_meta_t *new_ary = xrealloc(meta, new_capa * element_size + header_size);
140 if (meta == NULL) {
141 /* First allocation. Initialize size. On subsequence allocations
142 * realloc takes care of carrying over the size. */
143 new_ary->size = 0;
146 RUBY_ASSERT(new_ary->size <= new_capa);
148 new_ary->capa = new_capa;
150 // We don't have access to the type of the dynamic array in function context.
151 // Write out result with memcpy to avoid strict aliasing issue.
152 memcpy(ptr_to_ary, &new_ary, sizeof(new_ary));
155 // Internal function
156 // Ensure there is space for one more element.
157 // Note: header_size can be bigger than sizeof(rb_darray_meta_t) when T is __int128_t, for example.
158 static inline void
159 rb_darray_ensure_space(void *ptr_to_ary, size_t header_size, size_t element_size)
161 rb_darray_meta_t **ptr_to_ptr_to_meta = ptr_to_ary;
162 rb_darray_meta_t *meta = *ptr_to_ptr_to_meta;
163 size_t current_capa = rb_darray_capa(meta);
164 if (rb_darray_size(meta) < current_capa) return;
166 // Double the capacity
167 size_t new_capa = current_capa == 0 ? 1 : current_capa * 2;
169 rb_darray_resize_capa_impl(ptr_to_ary, new_capa, header_size, element_size);
172 static inline void
173 rb_darray_make_impl(void *ptr_to_ary, size_t array_size, size_t header_size, size_t element_size)
175 rb_darray_meta_t **ptr_to_ptr_to_meta = ptr_to_ary;
176 if (array_size == 0) {
177 *ptr_to_ptr_to_meta = NULL;
178 return;
181 rb_darray_meta_t *meta = xcalloc(array_size * element_size + header_size, 1);
183 meta->size = array_size;
184 meta->capa = array_size;
186 // We don't have access to the type of the dynamic array in function context.
187 // Write out result with memcpy to avoid strict aliasing issue.
188 memcpy(ptr_to_ary, &meta, sizeof(meta));
191 #endif /* RUBY_DARRAY_H */