summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJean Boussier <jean.boussier@gmail.com>2026-02-07 10:30:35 +0100
committerJean Boussier <jean.boussier@gmail.com>2026-02-08 12:47:40 +0100
commitcbf6e775ee180cc4db49289f0db774d489fb9a4c (patch)
tree97a0b48575701a7701b6e1f271f72e266ea60681
parent3e02e99e425ee1c2e4a7cee625eaea6ed1faf1c4 (diff)
darray.h: use ruby_sized_xfree
-rw-r--r--box.c2
-rw-r--r--darray.h21
-rw-r--r--load.c2
-rw-r--r--symbol.c4
4 files changed, 21 insertions, 8 deletions
diff --git a/box.c b/box.c
index bfb6f51346..0c8c1ca193 100644
--- a/box.c
+++ b/box.c
@@ -217,7 +217,7 @@ static int
free_loaded_feature_index_i(st_data_t key, st_data_t value, st_data_t arg)
{
if (!FIXNUM_P(value)) {
- rb_darray_free((void *)value);
+ rb_darray_free_sized((void *)value, long);
}
return ST_CONTINUE;
}
diff --git a/darray.h b/darray.h
index 2b876c9f1e..31ab7d412a 100644
--- a/darray.h
+++ b/darray.h
@@ -161,6 +161,18 @@ rb_darray_free(void *ary)
xfree(ary);
}
+void ruby_sized_xfree(void *x, size_t size);
+
+static inline void
+rb_darray_free_sized0(void *ary, size_t element_size)
+{
+ const rb_darray_meta_t *meta = ary;
+ if (meta) {
+ ruby_sized_xfree(ary, sizeof(*meta) + (element_size * meta->capa));
+ }
+}
+#define rb_darray_free_sized(ary, T) rb_darray_free_sized0((ary), sizeof(T))
+
static inline void
rb_darray_free_without_gc(void *ary)
{
@@ -191,13 +203,16 @@ rb_darray_calloc_mul_add_without_gc(size_t x, size_t y, size_t z)
return ptr;
}
+void *ruby_sized_xrealloc(void *ptr, size_t new_size, size_t old_size);
+
/* Internal function. Like rb_xrealloc_mul_add. */
static inline void *
-rb_darray_realloc_mul_add(void *orig_ptr, size_t x, size_t y, size_t z)
+rb_darray_realloc_mul_add(void *orig_ptr, size_t capa, size_t element_size, size_t header_size)
{
- size_t size = rbimpl_size_add_or_raise(rbimpl_size_mul_or_raise(x, y), z);
+ size_t size = rbimpl_size_add_or_raise(rbimpl_size_mul_or_raise(capa, element_size), header_size);
+ size_t old_size = (rb_darray_capa(orig_ptr) * element_size) + header_size; // We know it won't overflow
- void *ptr = xrealloc(orig_ptr, size);
+ void *ptr = ruby_sized_xrealloc(orig_ptr, size, old_size);
RUBY_ASSERT(ptr != NULL);
return ptr;
diff --git a/load.c b/load.c
index 144f095b04..391a6c1337 100644
--- a/load.c
+++ b/load.c
@@ -343,7 +343,7 @@ loaded_features_index_clear_i(st_data_t key, st_data_t val, st_data_t arg)
{
VALUE obj = (VALUE)val;
if (!SPECIAL_CONST_P(obj)) {
- rb_darray_free((void *)obj);
+ rb_darray_free_sized((void *)obj, long);
}
return ST_DELETE;
}
diff --git a/symbol.c b/symbol.c
index f0316ddd60..85340be3a7 100644
--- a/symbol.c
+++ b/symbol.c
@@ -186,9 +186,7 @@ sym_id_entry_list_mark(void *ptr)
static void
sym_id_entry_list_free(void *ptr)
{
- rb_darray(struct sym_id_entry) ary = ptr;
-
- rb_darray_free(ary);
+ rb_darray_free_sized(ptr, struct sym_id_entry);
}
static size_t