diff options
| author | Jean Boussier <jean.boussier@gmail.com> | 2025-06-16 11:19:12 +0200 |
|---|---|---|
| committer | Jean Boussier <jean.boussier@gmail.com> | 2025-06-17 15:28:05 +0200 |
| commit | cd9f447be247478d2eb3da985295735cce20cb23 (patch) | |
| tree | 1f87f079323e42a71f07b862b6d32daa6260e5b7 /imemo.c | |
| parent | 164486a954e3cf3e716393c5f9a9e2c4dd776993 (diff) | |
Refactor generic fields to use `T_IMEMO/fields` objects.
Followup: https://github.com/ruby/ruby/pull/13589
This simplify a lot of things, as we no longer need to manually
manage the memory, we can use the Read-Copy-Update pattern and
avoid numerous race conditions.
Co-Authored-By: Étienne Barrié <etienne.barrie@gmail.com>
Notes
Notes:
Merged: https://github.com/ruby/ruby/pull/13626
Diffstat (limited to 'imemo.c')
| -rw-r--r-- | imemo.c | 30 |
1 files changed, 30 insertions, 0 deletions
@@ -147,6 +147,23 @@ rb_imemo_fields_new_complex(VALUE klass, size_t capa) return imemo_fields_new_complex(klass, capa); } +static int +imemo_fields_trigger_wb_i(st_data_t key, st_data_t value, st_data_t arg) +{ + VALUE field_obj = (VALUE)arg; + RB_OBJ_WRITTEN(field_obj, Qundef, (VALUE)value); + return ST_CONTINUE; +} + +VALUE +rb_imemo_fields_new_complex_tbl(VALUE klass, st_table *tbl) +{ + VALUE fields = imemo_fields_new(klass, sizeof(struct rb_fields)); + IMEMO_OBJ_FIELDS(fields)->as.complex.table = tbl; + st_foreach(tbl, imemo_fields_trigger_wb_i, (st_data_t)fields); + return fields; +} + VALUE rb_imemo_fields_clone(VALUE fields_obj) { @@ -168,6 +185,19 @@ rb_imemo_fields_clone(VALUE fields_obj) return clone; } +void +rb_imemo_fields_clear(VALUE fields_obj) +{ + // When replacing an imemo/fields by another one, we must clear + // its shape so that gc.c:obj_free_object_id won't be called. + if (rb_shape_obj_too_complex_p(fields_obj)) { + RBASIC_SET_SHAPE_ID(fields_obj, ROOT_TOO_COMPLEX_SHAPE_ID); + } + else { + RBASIC_SET_SHAPE_ID(fields_obj, ROOT_SHAPE_ID); + } +} + /* ========================================================================= * memsize * ========================================================================= */ |
