From 4d9ad91a35b16afde38fd4ae513a49d22e27b3ea Mon Sep 17 00:00:00 2001 From: Peter Zhu Date: Tue, 25 Jan 2022 16:11:24 -0500 Subject: [PATCH] Rename rb_id_table_foreach_with_replace Renames rb_id_table_foreach_with_replace to rb_id_table_foreach_values_with_replace and passes only the value to the callback. We can use this in GC compaction when we cannot access the global symbol array. --- gc.c | 14 +++++++------- id_table.c | 42 +++++++++++++++++++++--------------------- id_table.h | 4 ++-- 3 files changed, 30 insertions(+), 30 deletions(-) diff --git a/gc.c b/gc.c index d448c7515d..c7cfea4a24 100644 --- a/gc.c +++ b/gc.c @@ -9790,7 +9790,7 @@ gc_ref_update_imemo(rb_objspace_t *objspace, VALUE obj) } static enum rb_id_table_iterator_result -check_id_table_move(ID id, VALUE value, void *data) +check_id_table_move(VALUE value, void *data) { rb_objspace_t *objspace = (rb_objspace_t *)data; @@ -9835,7 +9835,7 @@ rb_gc_location(VALUE value) } static enum rb_id_table_iterator_result -update_id_table(ID *key, VALUE * value, void *data, int existing) +update_id_table(VALUE *value, void *data, int existing) { rb_objspace_t *objspace = (rb_objspace_t *)data; @@ -9850,12 +9850,12 @@ static void update_m_tbl(rb_objspace_t *objspace, struct rb_id_table *tbl) { if (tbl) { - rb_id_table_foreach_with_replace(tbl, check_id_table_move, update_id_table, objspace); + rb_id_table_foreach_values_with_replace(tbl, check_id_table_move, update_id_table, objspace); } } static enum rb_id_table_iterator_result -update_cc_tbl_i(ID id, VALUE ccs_ptr, void *data) +update_cc_tbl_i(VALUE ccs_ptr, void *data) { rb_objspace_t *objspace = (rb_objspace_t *)data; struct rb_class_cc_entries *ccs = (struct rb_class_cc_entries *)ccs_ptr; @@ -9883,12 +9883,12 @@ update_cc_tbl(rb_objspace_t *objspace, VALUE klass) { struct rb_id_table *tbl = RCLASS_CC_TBL(klass); if (tbl) { - rb_id_table_foreach_with_replace(tbl, update_cc_tbl_i, 0, objspace); + rb_id_table_foreach_values_with_replace(tbl, update_cc_tbl_i, 0, objspace); } } static enum rb_id_table_iterator_result -update_cvc_tbl_i(ID id, VALUE cvc_entry, void *data) +update_cvc_tbl_i(VALUE cvc_entry, void *data) { struct rb_cvar_class_tbl_entry *entry; @@ -9904,7 +9904,7 @@ update_cvc_tbl(rb_objspace_t *objspace, VALUE klass) { struct rb_id_table *tbl = RCLASS_CVC_TBL(klass); if (tbl) { - rb_id_table_foreach_with_replace(tbl, update_cvc_tbl_i, 0, objspace); + rb_id_table_foreach_values_with_replace(tbl, update_cvc_tbl_i, 0, objspace); } } diff --git a/id_table.c b/id_table.c index b2ba6fae89..6cf4af9c5f 100644 --- a/id_table.c +++ b/id_table.c @@ -267,27 +267,6 @@ rb_id_table_delete(struct rb_id_table *tbl, ID id) return hash_delete_index(tbl, index); } -void -rb_id_table_foreach_with_replace(struct rb_id_table *tbl, rb_id_table_foreach_func_t *func, rb_id_table_update_callback_func_t *replace, void *data) -{ - int i, capa = tbl->capa; - - for (i=0; iitems[i].val, data); - assert(ITEM_GET_KEY(tbl, i)); - - if (ret == ID_TABLE_REPLACE) { - VALUE val = tbl->items[i].val; - ret = (*replace)(NULL, &val, data, TRUE); - tbl->items[i].val = val; - } - else if (ret == ID_TABLE_STOP) - return; - } - } -} - void rb_id_table_foreach(struct rb_id_table *tbl, rb_id_table_foreach_func_t *func, void *data) { @@ -323,3 +302,24 @@ rb_id_table_foreach_values(struct rb_id_table *tbl, rb_id_table_foreach_values_f } } } + +void +rb_id_table_foreach_values_with_replace(struct rb_id_table *tbl, rb_id_table_foreach_values_func_t *func, rb_id_table_update_value_callback_func_t *replace, void *data) +{ + int i, capa = tbl->capa; + + for (i = 0; i < capa; i++) { + if (ITEM_KEY_ISSET(tbl, i)) { + enum rb_id_table_iterator_result ret = (*func)(tbl->items[i].val, data); + + if (ret == ID_TABLE_REPLACE) { + VALUE val = tbl->items[i].val; + ret = (*replace)(&val, data, TRUE); + tbl->items[i].val = val; + } + else if (ret == ID_TABLE_STOP) + return; + } + } +} + diff --git a/id_table.h b/id_table.h index f3dc681d17..9d9eb5648e 100644 --- a/id_table.h +++ b/id_table.h @@ -26,11 +26,11 @@ int rb_id_table_insert(struct rb_id_table *tbl, ID id, VALUE val); int rb_id_table_lookup(struct rb_id_table *tbl, ID id, VALUE *valp); int rb_id_table_delete(struct rb_id_table *tbl, ID id); -typedef enum rb_id_table_iterator_result rb_id_table_update_callback_func_t(ID *id, VALUE *val, void *data, int existing); +typedef enum rb_id_table_iterator_result rb_id_table_update_value_callback_func_t(VALUE *val, void *data, int existing); typedef enum rb_id_table_iterator_result rb_id_table_foreach_func_t(ID id, VALUE val, void *data); typedef enum rb_id_table_iterator_result rb_id_table_foreach_values_func_t(VALUE val, void *data); void rb_id_table_foreach(struct rb_id_table *tbl, rb_id_table_foreach_func_t *func, void *data); -void rb_id_table_foreach_with_replace(struct rb_id_table *tbl, rb_id_table_foreach_func_t *func, rb_id_table_update_callback_func_t *replace, void *data); void rb_id_table_foreach_values(struct rb_id_table *tbl, rb_id_table_foreach_values_func_t *func, void *data); +void rb_id_table_foreach_values_with_replace(struct rb_id_table *tbl, rb_id_table_foreach_values_func_t *func, rb_id_table_update_value_callback_func_t *replace, void *data); #endif /* RUBY_ID_TABLE_H */