1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

Change feature_index from fake Array to darray

Using a fake (malloc) RArray is not friendly for the garbage
collector. Fake RArray does not have a heap page, so it causes Variable
Width Allocation to crash when we try to implement it on Arrays.

This commit changes feature_index from a RArray to a darray.
This commit is contained in:
Peter Zhu 2022-02-15 09:57:33 -05:00
parent 71afa8164d
commit 969ad5802d
Notes: git 2022-02-16 23:50:49 +09:00
2 changed files with 89 additions and 55 deletions

View file

@ -104,6 +104,8 @@
rb_darray_make_impl((ptr_to_ary), size, sizeof(**(ptr_to_ary)), \ 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]), ruby_xcalloc)
#define rb_darray_data_ptr(ary) ((ary)->data)
// Set the size of the array to zero without freeing the backing memory. // Set the size of the array to zero without freeing the backing memory.
// Allows reusing the same array. // Allows reusing the same array.
// //

118
load.c
View file

@ -201,62 +201,96 @@ is_rbext_path(VALUE feature_path)
return IS_RBEXT(RSTRING_PTR(feature_path) + len - rbext_len); return IS_RBEXT(RSTRING_PTR(feature_path) + len - rbext_len);
} }
static void typedef rb_darray(long) feature_indexes_t;
features_index_add_single(rb_vm_t *vm, const char* str, size_t len, VALUE offset, bool rb)
struct features_index_add_single_args {
rb_vm_t *vm;
VALUE offset;
bool rb;
};
static int
features_index_add_single_callback(st_data_t *key, st_data_t *value, st_data_t raw_args, int existing)
{ {
struct st_table *features_index; struct features_index_add_single_args *args = (struct features_index_add_single_args *)raw_args;
VALUE this_feature_index = Qnil; rb_vm_t *vm = args->vm;
st_data_t short_feature_key; VALUE offset = args->offset;
st_data_t data; bool rb = args->rb;
Check_Type(offset, T_FIXNUM); if (existing) {
short_feature_key = feature_key(str, len); VALUE this_feature_index = *value;
features_index = get_loaded_features_index_raw(vm); if (FIXNUM_P(this_feature_index)) {
if (!st_lookup(features_index, short_feature_key, &data) ||
NIL_P(this_feature_index = (VALUE)data)) {
st_insert(features_index, short_feature_key, (st_data_t)offset);
}
else if (FIXNUM_P(this_feature_index)) {
VALUE loaded_features = get_loaded_features(vm); VALUE loaded_features = get_loaded_features(vm);
VALUE this_feature_path = RARRAY_AREF(loaded_features, FIX2LONG(this_feature_index)); VALUE this_feature_path = RARRAY_AREF(loaded_features, FIX2LONG(this_feature_index));
VALUE feature_indexes[2];
feature_indexes_t feature_indexes;
rb_darray_make_with_gc(&feature_indexes, 2);
int top = (rb && !is_rbext_path(this_feature_path)) ? 1 : 0; int top = (rb && !is_rbext_path(this_feature_path)) ? 1 : 0;
feature_indexes[top^0] = this_feature_index; rb_darray_set(feature_indexes, top^0, FIX2LONG(this_feature_index));
feature_indexes[top^1] = offset; rb_darray_set(feature_indexes, top^1, FIX2LONG(offset));
this_feature_index = (VALUE)xcalloc(1, sizeof(struct RArray));
RBASIC(this_feature_index)->flags = T_ARRAY; /* fake VALUE, do not mark/sweep */ assert(rb_darray_size(feature_indexes) == 2);
rb_ary_cat(this_feature_index, feature_indexes, numberof(feature_indexes)); // assert feature_indexes does not look like a special const
st_insert(features_index, short_feature_key, (st_data_t)this_feature_index); assert(!SPECIAL_CONST_P((VALUE)feature_indexes));
*value = (st_data_t)feature_indexes;
} }
else { else {
feature_indexes_t feature_indexes = (feature_indexes_t)this_feature_index;
long pos = -1; long pos = -1;
Check_Type(this_feature_index, T_ARRAY);
if (rb) { if (rb) {
VALUE loaded_features = get_loaded_features(vm); VALUE loaded_features = get_loaded_features(vm);
for (long i = 0; i < RARRAY_LEN(this_feature_index); ++i) { for (size_t i = 0; i < rb_darray_size(feature_indexes); ++i) {
VALUE idx = RARRAY_AREF(this_feature_index, i); long idx = rb_darray_get(feature_indexes, i);
VALUE this_feature_path = RARRAY_AREF(loaded_features, FIX2LONG(idx)); VALUE this_feature_path = RARRAY_AREF(loaded_features, idx);
Check_Type(this_feature_path, T_STRING); Check_Type(this_feature_path, T_STRING);
if (!is_rbext_path(this_feature_path)) { if (!is_rbext_path(this_feature_path)) {
/* as this_feature_index is a fake VALUE, `push` (which
* doesn't wb_unprotect like as rb_ary_splice) first,
* then rotate partially. */
pos = i; pos = i;
break; break;
} }
} }
} }
rb_ary_push(this_feature_index, offset);
rb_darray_append_with_gc(&feature_indexes, FIX2LONG(offset));
/* darray may realloc which will change the pointer */
*value = (st_data_t)feature_indexes;
if (pos >= 0) { if (pos >= 0) {
VALUE *ptr = (VALUE *)RARRAY_CONST_PTR_TRANSIENT(this_feature_index); long *ptr = rb_darray_data_ptr(feature_indexes);
long len = RARRAY_LEN(this_feature_index); long len = rb_darray_size(feature_indexes);
MEMMOVE(ptr + pos, ptr + pos + 1, VALUE, len - pos - 1); MEMMOVE(ptr + pos, ptr + pos + 1, long, len - pos - 1);
ptr[pos] = offset; ptr[pos] = FIX2LONG(offset);
} }
} }
} }
else {
*value = offset;
}
return ST_CONTINUE;
}
static void
features_index_add_single(rb_vm_t *vm, const char* str, size_t len, VALUE offset, bool rb)
{
struct st_table *features_index;
st_data_t short_feature_key;
Check_Type(offset, T_FIXNUM);
short_feature_key = feature_key(str, len);
features_index = get_loaded_features_index_raw(vm);
struct features_index_add_single_args args = {
.vm = vm,
.offset = offset,
.rb = rb,
};
st_update(features_index, short_feature_key, features_index_add_single_callback, (st_data_t)&args);
}
/* Add to the loaded-features index all the required entries for /* Add to the loaded-features index all the required entries for
`feature`, located at `offset` in $LOADED_FEATURES. We add an `feature`, located at `offset` in $LOADED_FEATURES. We add an
@ -309,8 +343,7 @@ loaded_features_index_clear_i(st_data_t key, st_data_t val, st_data_t arg)
{ {
VALUE obj = (VALUE)val; VALUE obj = (VALUE)val;
if (!SPECIAL_CONST_P(obj)) { if (!SPECIAL_CONST_P(obj)) {
rb_ary_free(obj); rb_darray_free_with_gc((void *)obj);
ruby_sized_xfree((void *)obj, sizeof(struct RArray));
} }
return ST_DELETE; return ST_DELETE;
} }
@ -480,18 +513,17 @@ rb_feature_p(rb_vm_t *vm, const char *feature, const char *ext, int rb, int expa
as any distractors, so we may ignore all other entries in `features`. as any distractors, so we may ignore all other entries in `features`.
*/ */
if (st_lookup(features_index, key, &data) && !NIL_P(this_feature_index = (VALUE)data)) { if (st_lookup(features_index, key, &data) && !NIL_P(this_feature_index = (VALUE)data)) {
for (i = 0; ; i++) { for (size_t i = 0; ; i++) {
VALUE entry;
long index; long index;
if (RB_TYPE_P(this_feature_index, T_ARRAY)) { if (FIXNUM_P(this_feature_index)) {
if (i >= RARRAY_LEN(this_feature_index)) break; if (i > 0) break;
entry = RARRAY_AREF(this_feature_index, i); index = FIX2LONG(this_feature_index);
} }
else { else {
if (i > 0) break; feature_indexes_t feature_indexes = (feature_indexes_t)this_feature_index;
entry = this_feature_index; if (i >= rb_darray_size(feature_indexes)) break;
index = rb_darray_get(feature_indexes, i);
} }
index = FIX2LONG(entry);
v = RARRAY_AREF(features, index); v = RARRAY_AREF(features, index);
f = StringValuePtr(v); f = StringValuePtr(v);