2021-02-13 01:12:18 +03:00
|
|
|
#ifndef RUBY_DARRAY_H
|
|
|
|
#define RUBY_DARRAY_H
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2023-07-24 21:21:50 +03:00
|
|
|
#include "internal/bits.h"
|
2023-07-24 17:42:04 +03:00
|
|
|
#include "internal/gc.h"
|
|
|
|
|
2021-02-13 01:12:18 +03:00
|
|
|
// Type for a dynamic array. Use to declare a dynamic array.
|
|
|
|
// It is a pointer so it fits in st_table nicely. Designed
|
|
|
|
// to be fairly type-safe.
|
|
|
|
//
|
|
|
|
// NULL is a valid empty dynamic array.
|
|
|
|
//
|
|
|
|
// Example:
|
|
|
|
// rb_darray(char) char_array = NULL;
|
2022-02-15 17:55:53 +03:00
|
|
|
// rb_darray_append(&char_array, 'e');
|
2021-02-13 01:12:18 +03:00
|
|
|
// printf("pushed %c\n", *rb_darray_ref(char_array, 0));
|
|
|
|
// rb_darray_free(char_array);
|
|
|
|
//
|
|
|
|
#define rb_darray(T) struct { rb_darray_meta_t meta; T data[]; } *
|
|
|
|
|
|
|
|
// Copy an element out of the array. Warning: not bounds checked.
|
|
|
|
//
|
2022-02-15 17:55:53 +03:00
|
|
|
// T rb_darray_get(rb_darray(T) ary, size_t idx);
|
2021-02-13 01:12:18 +03:00
|
|
|
//
|
|
|
|
#define rb_darray_get(ary, idx) ((ary)->data[(idx)])
|
|
|
|
|
|
|
|
// Assign to an element. Warning: not bounds checked.
|
|
|
|
//
|
2022-02-15 17:55:53 +03:00
|
|
|
// void rb_darray_set(rb_darray(T) ary, size_t idx, T element);
|
2021-02-13 01:12:18 +03:00
|
|
|
//
|
|
|
|
#define rb_darray_set(ary, idx, element) ((ary)->data[(idx)] = (element))
|
|
|
|
|
|
|
|
// Get a pointer to an element. Warning: not bounds checked.
|
|
|
|
//
|
2022-02-15 17:55:53 +03:00
|
|
|
// T *rb_darray_ref(rb_darray(T) ary, size_t idx);
|
2021-02-13 01:12:18 +03:00
|
|
|
//
|
|
|
|
#define rb_darray_ref(ary, idx) (&((ary)->data[(idx)]))
|
|
|
|
|
2023-07-24 17:42:04 +03:00
|
|
|
/* Copy a new element into the array. ptr_to_ary is evaluated multiple times.
|
|
|
|
*
|
|
|
|
* void rb_darray_append(rb_darray(T) *ptr_to_ary, T element);
|
|
|
|
*/
|
|
|
|
#define rb_darray_append(ptr_to_ary, element) \
|
|
|
|
rb_darray_append_impl(ptr_to_ary, element, rb_xrealloc_mul_add)
|
|
|
|
|
|
|
|
#define rb_darray_append_without_gc(ptr_to_ary, element) \
|
|
|
|
rb_darray_append_impl(ptr_to_ary, element, rb_darray_realloc_mul_add_without_gc)
|
|
|
|
|
|
|
|
#define rb_darray_append_impl(ptr_to_ary, element, realloc_func) do { \
|
|
|
|
rb_darray_ensure_space((ptr_to_ary), \
|
|
|
|
sizeof(**(ptr_to_ary)), \
|
|
|
|
sizeof((*(ptr_to_ary))->data[0]), \
|
|
|
|
realloc_func); \
|
2022-02-15 17:55:53 +03:00
|
|
|
rb_darray_set(*(ptr_to_ary), \
|
|
|
|
(*(ptr_to_ary))->meta.size, \
|
|
|
|
(element)); \
|
|
|
|
(*(ptr_to_ary))->meta.size++; \
|
|
|
|
} while (0)
|
|
|
|
|
2021-02-13 01:12:18 +03:00
|
|
|
// Iterate over items of the array in a for loop
|
|
|
|
//
|
|
|
|
#define rb_darray_foreach(ary, idx_name, elem_ptr_var) \
|
2022-02-15 17:55:53 +03:00
|
|
|
for (size_t idx_name = 0; idx_name < rb_darray_size(ary) && ((elem_ptr_var) = rb_darray_ref(ary, idx_name)); ++idx_name)
|
2021-02-13 01:12:18 +03:00
|
|
|
|
2023-07-24 17:42:04 +03:00
|
|
|
// Iterate over valid indices in the array in a for loop
|
2021-02-17 04:49:28 +03:00
|
|
|
//
|
2023-07-24 17:42:04 +03:00
|
|
|
#define rb_darray_for(ary, idx_name) \
|
|
|
|
for (size_t idx_name = 0; idx_name < rb_darray_size(ary); ++idx_name)
|
|
|
|
|
|
|
|
/* Make a dynamic array of a certain size. All bytes backing the elements are set to zero.
|
|
|
|
* Return 1 on success and 0 on failure.
|
|
|
|
*
|
|
|
|
* Note that NULL is a valid empty dynamic array.
|
|
|
|
*
|
|
|
|
* void rb_darray_make(rb_darray(T) *ptr_to_ary, size_t size);
|
|
|
|
*/
|
2022-02-15 17:55:53 +03:00
|
|
|
#define rb_darray_make(ptr_to_ary, size) \
|
|
|
|
rb_darray_make_impl((ptr_to_ary), size, sizeof(**(ptr_to_ary)), \
|
2023-07-24 17:42:04 +03:00
|
|
|
sizeof((*(ptr_to_ary))->data[0]), rb_xcalloc_mul_add)
|
2021-02-17 04:49:28 +03:00
|
|
|
|
2023-07-24 17:42:04 +03:00
|
|
|
#define rb_darray_make_without_gc(ptr_to_ary, size) \
|
|
|
|
rb_darray_make_impl((ptr_to_ary), size, sizeof(**(ptr_to_ary)), \
|
|
|
|
sizeof((*(ptr_to_ary))->data[0]), rb_darray_calloc_mul_add_without_gc)
|
2022-02-15 17:57:33 +03:00
|
|
|
|
2023-07-24 21:21:50 +03:00
|
|
|
/* Resize the darray to a new capacity. The new capacity must be greater than
|
|
|
|
* or equal to the size of the darray.
|
|
|
|
*
|
|
|
|
* void rb_darray_resize_capa(rb_darray(T) *ptr_to_ary, size_t capa);
|
|
|
|
*/
|
|
|
|
#define rb_darray_resize_capa_without_gc(ptr_to_ary, capa) \
|
|
|
|
rb_darray_resize_capa_impl((ptr_to_ary), rb_darray_next_power_of_two(capa), sizeof(**(ptr_to_ary)), \
|
|
|
|
sizeof((*(ptr_to_ary))->data[0]), rb_darray_realloc_mul_add_without_gc)
|
|
|
|
|
2023-07-24 17:42:04 +03:00
|
|
|
#define rb_darray_data_ptr(ary) ((ary)->data)
|
2021-09-09 22:35:47 +03:00
|
|
|
|
2021-02-13 01:12:18 +03:00
|
|
|
typedef struct rb_darray_meta {
|
2022-02-15 17:55:53 +03:00
|
|
|
size_t size;
|
|
|
|
size_t capa;
|
2021-02-13 01:12:18 +03:00
|
|
|
} rb_darray_meta_t;
|
|
|
|
|
2023-07-24 21:21:50 +03:00
|
|
|
/* Set the size of the array to zero without freeing the backing memory.
|
|
|
|
* Allows reusing the same array. */
|
2023-07-24 17:42:04 +03:00
|
|
|
static inline void
|
|
|
|
rb_darray_clear(void *ary)
|
|
|
|
{
|
|
|
|
rb_darray_meta_t *meta = ary;
|
|
|
|
if (meta) {
|
|
|
|
meta->size = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-13 01:12:18 +03:00
|
|
|
// Get the size of the dynamic array.
|
|
|
|
//
|
2022-02-15 17:55:53 +03:00
|
|
|
static inline size_t
|
2021-02-13 01:12:18 +03:00
|
|
|
rb_darray_size(const void *ary)
|
|
|
|
{
|
|
|
|
const rb_darray_meta_t *meta = ary;
|
|
|
|
return meta ? meta->size : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the capacity of the dynamic array.
|
|
|
|
//
|
2022-02-15 17:55:53 +03:00
|
|
|
static inline size_t
|
2021-02-13 01:12:18 +03:00
|
|
|
rb_darray_capa(const void *ary)
|
|
|
|
{
|
|
|
|
const rb_darray_meta_t *meta = ary;
|
|
|
|
return meta ? meta->capa : 0;
|
|
|
|
}
|
|
|
|
|
2023-07-24 17:42:04 +03:00
|
|
|
/* Free the dynamic array. */
|
2021-02-13 01:12:18 +03:00
|
|
|
static inline void
|
|
|
|
rb_darray_free(void *ary)
|
2022-02-15 17:55:53 +03:00
|
|
|
{
|
|
|
|
rb_darray_meta_t *meta = ary;
|
2023-07-24 17:42:04 +03:00
|
|
|
if (meta) ruby_sized_xfree(ary, meta->capa);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void
|
|
|
|
rb_darray_free_without_gc(void *ary)
|
|
|
|
{
|
|
|
|
free(ary);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Internal function. Like rb_xcalloc_mul_add but does not trigger GC and does
|
|
|
|
* not check for overflow in arithmetic. */
|
|
|
|
static inline void *
|
|
|
|
rb_darray_calloc_mul_add_without_gc(size_t x, size_t y, size_t z)
|
|
|
|
{
|
|
|
|
size_t size = (x * y) + z;
|
|
|
|
|
|
|
|
void *ptr = calloc(1, size);
|
|
|
|
if (ptr == NULL) rb_bug("rb_darray_calloc_mul_add_without_gc: failed");
|
|
|
|
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Internal function. Like rb_xrealloc_mul_add but does not trigger GC and does
|
|
|
|
* not check for overflow in arithmetic. */
|
|
|
|
static inline void *
|
|
|
|
rb_darray_realloc_mul_add_without_gc(const void *orig_ptr, size_t x, size_t y, size_t z)
|
|
|
|
{
|
|
|
|
size_t size = (x * y) + z;
|
|
|
|
|
|
|
|
void *ptr = realloc((void *)orig_ptr, size);
|
|
|
|
if (ptr == NULL) rb_bug("rb_darray_realloc_mul_add_without_gc: failed");
|
|
|
|
|
|
|
|
return ptr;
|
2022-02-15 17:55:53 +03:00
|
|
|
}
|
|
|
|
|
2023-07-24 21:21:50 +03:00
|
|
|
/* Internal function. Returns the next power of two that is greater than or
|
|
|
|
* equal to n. */
|
|
|
|
static inline size_t
|
|
|
|
rb_darray_next_power_of_two(size_t n)
|
|
|
|
{
|
|
|
|
return (size_t)(1 << (64 - nlz_int64(n)));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Internal function. Resizes the capacity of a darray. The new capacity must
|
|
|
|
* be greater than or equal to the size of the darray. */
|
|
|
|
static inline void
|
|
|
|
rb_darray_resize_capa_impl(void *ptr_to_ary, size_t new_capa, size_t header_size, size_t element_size,
|
|
|
|
void *(*realloc_mul_add_impl)(const void *, size_t, size_t, size_t))
|
|
|
|
{
|
|
|
|
rb_darray_meta_t **ptr_to_ptr_to_meta = ptr_to_ary;
|
|
|
|
rb_darray_meta_t *meta = *ptr_to_ptr_to_meta;
|
|
|
|
|
|
|
|
rb_darray_meta_t *new_ary = realloc_mul_add_impl(meta, new_capa, element_size, header_size);
|
|
|
|
|
|
|
|
if (meta == NULL) {
|
|
|
|
/* First allocation. Initialize size. On subsequence allocations
|
|
|
|
* realloc takes care of carrying over the size. */
|
|
|
|
new_ary->size = 0;
|
|
|
|
}
|
|
|
|
|
2024-02-08 17:42:51 +03:00
|
|
|
RUBY_ASSERT(new_ary->size <= new_capa);
|
2023-07-24 21:21:50 +03:00
|
|
|
|
|
|
|
new_ary->capa = new_capa;
|
|
|
|
|
|
|
|
// We don't have access to the type of the dynamic array in function context.
|
|
|
|
// Write out result with memcpy to avoid strict aliasing issue.
|
|
|
|
memcpy(ptr_to_ary, &new_ary, sizeof(new_ary));
|
|
|
|
}
|
|
|
|
|
2021-02-13 01:12:18 +03:00
|
|
|
// Internal function
|
2022-02-15 17:55:53 +03:00
|
|
|
// Ensure there is space for one more element.
|
2021-02-17 04:49:28 +03:00
|
|
|
// Note: header_size can be bigger than sizeof(rb_darray_meta_t) when T is __int128_t, for example.
|
2022-02-15 17:55:53 +03:00
|
|
|
static inline void
|
2023-07-24 17:42:04 +03:00
|
|
|
rb_darray_ensure_space(void *ptr_to_ary, size_t header_size, size_t element_size,
|
|
|
|
void *(*realloc_mul_add_impl)(const void *, size_t, size_t, size_t))
|
2021-02-13 01:12:18 +03:00
|
|
|
{
|
|
|
|
rb_darray_meta_t **ptr_to_ptr_to_meta = ptr_to_ary;
|
2021-02-17 04:48:14 +03:00
|
|
|
rb_darray_meta_t *meta = *ptr_to_ptr_to_meta;
|
2022-02-15 17:55:53 +03:00
|
|
|
size_t current_capa = rb_darray_capa(meta);
|
|
|
|
if (rb_darray_size(meta) < current_capa) return;
|
2021-02-13 01:12:18 +03:00
|
|
|
|
2022-02-15 17:55:53 +03:00
|
|
|
// Double the capacity
|
|
|
|
size_t new_capa = current_capa == 0 ? 1 : current_capa * 2;
|
2021-02-13 01:12:18 +03:00
|
|
|
|
2023-07-24 21:21:50 +03:00
|
|
|
rb_darray_resize_capa_impl(ptr_to_ary, new_capa, header_size, element_size, realloc_mul_add_impl);
|
2021-02-13 01:12:18 +03:00
|
|
|
}
|
|
|
|
|
2022-02-15 17:55:53 +03:00
|
|
|
static inline void
|
2023-07-24 17:42:04 +03:00
|
|
|
rb_darray_make_impl(void *ptr_to_ary, size_t array_size, size_t header_size, size_t element_size,
|
|
|
|
void *(*calloc_mul_add_impl)(size_t, size_t, size_t))
|
2021-02-17 04:49:28 +03:00
|
|
|
{
|
|
|
|
rb_darray_meta_t **ptr_to_ptr_to_meta = ptr_to_ary;
|
|
|
|
if (array_size == 0) {
|
|
|
|
*ptr_to_ptr_to_meta = NULL;
|
2022-02-15 17:55:53 +03:00
|
|
|
return;
|
2021-02-17 04:49:28 +03:00
|
|
|
}
|
|
|
|
|
2023-07-24 17:42:04 +03:00
|
|
|
rb_darray_meta_t *meta = calloc_mul_add_impl(array_size, element_size, header_size);
|
2021-02-17 04:49:28 +03:00
|
|
|
|
|
|
|
meta->size = array_size;
|
|
|
|
meta->capa = array_size;
|
|
|
|
|
2021-02-19 19:02:09 +03:00
|
|
|
// We don't have access to the type of the dynamic array in function context.
|
|
|
|
// Write out result with memcpy to avoid strict aliasing issue.
|
|
|
|
memcpy(ptr_to_ary, &meta, sizeof(meta));
|
2021-02-17 04:49:28 +03:00
|
|
|
}
|
|
|
|
|
2021-02-13 01:12:18 +03:00
|
|
|
#endif /* RUBY_DARRAY_H */
|