From fe7c02c7444495679ff88f957c327b2d46c8e143 Mon Sep 17 00:00:00 2001 From: Peter Zhu Date: Mon, 2 May 2022 14:45:52 -0400 Subject: [PATCH] Remove _with_gc functions in darray darray was used in YJIT which required the functions to not trigger GC. YJIT has now moved to Rust and does not use darray anymore, so we can remove the functions that don't trigger GC and only keep the ones that trigger GC. --- darray.h | 67 +++++++++----------------------------------------------- load.c | 6 ++--- 2 files changed, 13 insertions(+), 60 deletions(-) diff --git a/darray.h b/darray.h index bb8199a98c..c9a53f1e01 100644 --- a/darray.h +++ b/darray.h @@ -41,20 +41,9 @@ // // void rb_darray_append(rb_darray(T) *ptr_to_ary, T element); // -// TODO: replace this with rb_darray_append_with_gc when YJIT moves to Rust. -// #define rb_darray_append(ptr_to_ary, element) do { \ rb_darray_ensure_space((ptr_to_ary), sizeof(**(ptr_to_ary)), \ - sizeof((*(ptr_to_ary))->data[0]), realloc); \ - rb_darray_set(*(ptr_to_ary), \ - (*(ptr_to_ary))->meta.size, \ - (element)); \ - (*(ptr_to_ary))->meta.size++; \ -} while (0) - -#define rb_darray_append_with_gc(ptr_to_ary, element) do { \ - rb_darray_ensure_space((ptr_to_ary), sizeof(**(ptr_to_ary)), \ - sizeof((*(ptr_to_ary))->data[0]), ruby_xrealloc); \ + sizeof((*(ptr_to_ary))->data[0])); \ rb_darray_set(*(ptr_to_ary), \ (*(ptr_to_ary))->meta.size, \ (element)); \ @@ -87,22 +76,14 @@ 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); // -// TODO: replace this with rb_darray_make_with_gc with YJIT moves to Rust. -// #define rb_darray_make(ptr_to_ary, size) \ rb_darray_make_impl((ptr_to_ary), size, sizeof(**(ptr_to_ary)), \ - sizeof((*(ptr_to_ary))->data[0]), calloc) - - -#define rb_darray_make_with_gc(ptr_to_ary, size) \ - rb_darray_make_impl((ptr_to_ary), size, sizeof(**(ptr_to_ary)), \ - sizeof((*(ptr_to_ary))->data[0]), ruby_xcalloc) + sizeof((*(ptr_to_ary))->data[0])) #define rb_darray_data_ptr(ary) ((ary)->data) @@ -136,34 +117,18 @@ rb_darray_capa(const void *ary) // Free the dynamic array. // -// TODO: replace this with rb_darray_free_with_gc when YJIT moves to Rust. -// static inline void rb_darray_free(void *ary) -{ - free(ary); -} - -static inline void -rb_darray_free_with_gc(void *ary) { rb_darray_meta_t *meta = ary; ruby_sized_xfree(ary, meta->capa); } -// Internal function. Calculate buffer size on malloc heap. -static inline size_t -rb_darray_buffer_size(size_t capacity, size_t header_size, size_t element_size) -{ - if (capacity == 0) return 0; - return header_size + capacity * element_size; -} - // Internal function // Ensure there is space for one more element. // Note: header_size can be bigger than sizeof(rb_darray_meta_t) when T is __int128_t, for example. static inline void -rb_darray_ensure_space(void *ptr_to_ary, size_t header_size, size_t element_size, void *(*realloc_impl)(void *, size_t)) +rb_darray_ensure_space(void *ptr_to_ary, size_t header_size, size_t element_size) { rb_darray_meta_t **ptr_to_ptr_to_meta = ptr_to_ary; rb_darray_meta_t *meta = *ptr_to_ptr_to_meta; @@ -173,18 +138,9 @@ rb_darray_ensure_space(void *ptr_to_ary, size_t header_size, size_t element_size // Double the capacity size_t new_capa = current_capa == 0 ? 1 : current_capa * 2; - // Calculate new buffer size - size_t current_buffer_size = rb_darray_buffer_size(current_capa, header_size, element_size); - size_t new_buffer_size = rb_darray_buffer_size(new_capa, header_size, element_size); - if (new_buffer_size <= current_buffer_size) { - rb_bug("rb_darray_ensure_space: overflow"); - } - - // TODO: replace with rb_xrealloc_mul_add(meta, new_capa, element_size, header_size); - rb_darray_meta_t *doubled_ary = realloc_impl(meta, new_buffer_size); - if (!doubled_ary) { - rb_bug("rb_darray_ensure_space: failed"); - } + rb_darray_meta_t *doubled_ary = rb_xrealloc_mul_add(meta, new_capa, element_size, header_size); + // rb_xrealloc functions guarantee that NULL is not returned + assert(doubled_ary != NULL); if (meta == NULL) { // First allocation. Initialize size. On subsequence allocations @@ -200,7 +156,7 @@ rb_darray_ensure_space(void *ptr_to_ary, size_t header_size, size_t element_size } static inline void -rb_darray_make_impl(void *ptr_to_ary, size_t array_size, size_t header_size, size_t element_size, void *(*calloc_impl)(size_t, size_t)) +rb_darray_make_impl(void *ptr_to_ary, size_t array_size, size_t header_size, size_t element_size) { rb_darray_meta_t **ptr_to_ptr_to_meta = ptr_to_ary; if (array_size == 0) { @@ -208,12 +164,9 @@ rb_darray_make_impl(void *ptr_to_ary, size_t array_size, size_t header_size, siz return; } - // TODO: replace with rb_xcalloc_mul_add(array_size, element_size, header_size) - size_t buffer_size = rb_darray_buffer_size(array_size, header_size, element_size); - rb_darray_meta_t *meta = calloc_impl(buffer_size, 1); - if (!meta) { - rb_bug("rb_darray_make_impl: failed"); - } + rb_darray_meta_t *meta = rb_xcalloc_mul_add(array_size, element_size, header_size); + // rb_xcalloc functions guarantee that NULL is not returned + assert(meta != NULL); meta->size = array_size; meta->capa = array_size; diff --git a/load.c b/load.c index 496188d850..f2f55fde2b 100644 --- a/load.c +++ b/load.c @@ -226,7 +226,7 @@ features_index_add_single_callback(st_data_t *key, st_data_t *value, st_data_t r VALUE this_feature_path = RARRAY_AREF(loaded_features, FIX2LONG(this_feature_index)); feature_indexes_t feature_indexes; - rb_darray_make_with_gc(&feature_indexes, 2); + rb_darray_make(&feature_indexes, 2); int top = (rb && !is_rbext_path(this_feature_path)) ? 1 : 0; rb_darray_set(feature_indexes, top^0, FIX2LONG(this_feature_index)); rb_darray_set(feature_indexes, top^1, FIX2LONG(offset)); @@ -254,7 +254,7 @@ features_index_add_single_callback(st_data_t *key, st_data_t *value, st_data_t r } } - rb_darray_append_with_gc(&feature_indexes, FIX2LONG(offset)); + rb_darray_append(&feature_indexes, FIX2LONG(offset)); /* darray may realloc which will change the pointer */ *value = (st_data_t)feature_indexes; @@ -344,7 +344,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_with_gc((void *)obj); + rb_darray_free((void *)obj); } return ST_DELETE; }