From 6dd60cf114701f1ff3526381c0e742c588af2f91 Mon Sep 17 00:00:00 2001 From: 卜部昌平 Date: Mon, 26 Aug 2019 16:06:40 +0900 Subject: st_foreach now free from ANYARGS After 5e86b005c0f2ef30df2f9906c7e2f3abefe286a2, I now think ANYARGS is dangerous and should be extinct. This commit deletes ANYARGS from st_foreach. I strongly believe that this commit should have had come with b0af0592fdd9e9d4e4b863fde006d67ccefeac21, which added extra parameter to st_foreach callbacks. --- ext/-test-/st/foreach/foreach.c | 2 +- ext/-test-/st/numhash/numhash.c | 3 +-- ext/objspace/object_tracing.c | 4 ++-- gc.c | 2 +- include/ruby/st.h | 8 +++++--- marshal.c | 2 +- regparse.c | 8 ++++---- st.c | 23 ++++++++++++++++++----- 8 files changed, 33 insertions(+), 19 deletions(-) diff --git a/ext/-test-/st/foreach/foreach.c b/ext/-test-/st/foreach/foreach.c index 209b535503..27ac18046f 100644 --- a/ext/-test-/st/foreach/foreach.c +++ b/ext/-test-/st/foreach/foreach.c @@ -106,7 +106,7 @@ unp_fec(VALUE self, VALUE test) } static int -unp_fe_i(st_data_t key, st_data_t val, st_data_t args, int error) +unp_fe_i(st_data_t key, st_data_t val, st_data_t args) { struct checker *c = (struct checker *)args; diff --git a/ext/-test-/st/numhash/numhash.c b/ext/-test-/st/numhash/numhash.c index fc35f476cd..71eeed4910 100644 --- a/ext/-test-/st/numhash/numhash.c +++ b/ext/-test-/st/numhash/numhash.c @@ -57,7 +57,7 @@ numhash_aset(VALUE self, VALUE key, VALUE data) } static int -numhash_i(st_data_t key, st_data_t value, st_data_t arg) +numhash_i(st_data_t key, st_data_t value, st_data_t arg, int _) { VALUE ret; ret = rb_yield_values(3, (VALUE)key, (VALUE)value, (VALUE)arg); @@ -135,4 +135,3 @@ Init_numhash(void) rb_define_method(st, "size", numhash_size, 0); rb_define_method(st, "delete_safe", numhash_delete_safe, 1); } - diff --git a/ext/objspace/object_tracing.c b/ext/objspace/object_tracing.c index 7c354498ab..a057ac2a96 100644 --- a/ext/objspace/object_tracing.c +++ b/ext/objspace/object_tracing.c @@ -138,14 +138,14 @@ freeobj_i(VALUE tpval, void *data) } static int -free_keys_i(st_data_t key, st_data_t value, void *data) +free_keys_i(st_data_t key, st_data_t value, st_data_t data) { ruby_xfree((void *)key); return ST_CONTINUE; } static int -free_values_i(st_data_t key, st_data_t value, void *data) +free_values_i(st_data_t key, st_data_t value, st_data_t data) { ruby_xfree((void *)value); return ST_CONTINUE; diff --git a/gc.c b/gc.c index 42559eb377..32b7807d74 100644 --- a/gc.c +++ b/gc.c @@ -5620,7 +5620,7 @@ gc_check_after_marks_i(st_data_t k, st_data_t v, void *ptr) } static void -gc_marks_check(rb_objspace_t *objspace, int (*checker_func)(ANYARGS), const char *checker_name) +gc_marks_check(rb_objspace_t *objspace, st_foreach_callback_func *checker_func, const char *checker_name) { size_t saved_malloc_increase = objspace->malloc_params.increase; #if RGENGC_ESTIMATE_OLDMALLOC diff --git a/include/ruby/st.h b/include/ruby/st.h index a7eb0c6d7c..9b48d514a9 100644 --- a/include/ruby/st.h +++ b/include/ruby/st.h @@ -118,9 +118,11 @@ typedef int st_update_callback_func(st_data_t *key, st_data_t *value, st_data_t * results of hash() are same and compare() returns 0, otherwise the * behavior is undefined */ int st_update(st_table *table, st_data_t key, st_update_callback_func *func, st_data_t arg); -int st_foreach_with_replace(st_table *tab, int (*func)(ANYARGS), st_update_callback_func *replace, st_data_t arg); -int st_foreach(st_table *, int (*)(ANYARGS), st_data_t); -int st_foreach_check(st_table *, int (*)(ANYARGS), st_data_t, st_data_t); +typedef int st_foreach_callback_func(st_data_t, st_data_t, st_data_t); +typedef int st_foreach_check_callback_func(st_data_t, st_data_t, st_data_t, int); +int st_foreach_with_replace(st_table *tab, st_foreach_check_callback_func *func, st_update_callback_func *replace, st_data_t arg); +int st_foreach(st_table *, st_foreach_callback_func *, st_data_t); +int st_foreach_check(st_table *, st_foreach_check_callback_func *, st_data_t, st_data_t); st_index_t st_keys(st_table *table, st_data_t *keys, st_index_t size); st_index_t st_keys_check(st_table *table, st_data_t *keys, st_index_t size, st_data_t never); st_index_t st_values(st_table *table, st_data_t *values, st_index_t size); diff --git a/marshal.c b/marshal.c index b6de65023c..0d7cbc169c 100644 --- a/marshal.c +++ b/marshal.c @@ -112,7 +112,7 @@ static VALUE rb_marshal_dump_limited(VALUE obj, VALUE port, int limit); static VALUE rb_marshal_load_with_proc(VALUE port, VALUE proc); static int -mark_marshal_compat_i(st_data_t key, st_data_t value) +mark_marshal_compat_i(st_data_t key, st_data_t value, st_data_t _) { marshal_compat_t *p = (marshal_compat_t *)value; rb_gc_mark(p->newclass); diff --git a/regparse.c b/regparse.c index 574a07e05d..5f118900de 100644 --- a/regparse.c +++ b/regparse.c @@ -493,7 +493,7 @@ onig_print_names(FILE* fp, regex_t* reg) if (IS_NOT_NULL(t)) { fprintf(fp, "name table\n"); - onig_st_foreach(t, i_print_name_entry, (HashDataType )fp); + onig_st_foreach(t, (st_foreach_callback_func *)i_print_name_entry, (HashDataType )fp); fputs("\n", fp); } return 0; @@ -516,7 +516,7 @@ names_clear(regex_t* reg) NameTable* t = (NameTable* )reg->name_table; if (IS_NOT_NULL(t)) { - onig_st_foreach(t, i_free_name_entry, 0); + onig_st_foreach(t, (st_foreach_callback_func *)i_free_name_entry, 0); } return 0; } @@ -585,7 +585,7 @@ onig_foreach_name(regex_t* reg, narg.reg = reg; narg.arg = arg; narg.enc = reg->enc; /* should be pattern encoding. */ - onig_st_foreach(t, i_names, (HashDataType )&narg); + onig_st_foreach(t, (st_foreach_callback_func *)i_names, (HashDataType )&narg); } return narg.ret; } @@ -613,7 +613,7 @@ onig_renumber_name_table(regex_t* reg, GroupNumRemap* map) NameTable* t = (NameTable* )reg->name_table; if (IS_NOT_NULL(t)) { - onig_st_foreach(t, i_renumber_name, (HashDataType )map); + onig_st_foreach(t, (st_foreach_callback_func *)i_renumber_name, (HashDataType )map); } return 0; } diff --git a/st.c b/st.c index cecc2ac67f..c32a7ed208 100644 --- a/st.c +++ b/st.c @@ -1548,7 +1548,7 @@ st_update(st_table *tab, st_data_t key, different for ST_CHECK and when the current element is removed during traversing. */ static inline int -st_general_foreach(st_table *tab, int (*func)(ANYARGS), st_update_callback_func *replace, st_data_t arg, +st_general_foreach(st_table *tab, st_foreach_check_callback_func *func, st_update_callback_func *replace, st_data_t arg, int check_p) { st_index_t bin; @@ -1659,20 +1659,33 @@ st_general_foreach(st_table *tab, int (*func)(ANYARGS), st_update_callback_func } int -st_foreach_with_replace(st_table *tab, int (*func)(ANYARGS), st_update_callback_func *replace, st_data_t arg) +st_foreach_with_replace(st_table *tab, st_foreach_check_callback_func *func, st_update_callback_func *replace, st_data_t arg) { return st_general_foreach(tab, func, replace, arg, TRUE); } +struct functor { + st_foreach_callback_func *func; + st_data_t arg; +}; + +static int +apply_functor(st_data_t k, st_data_t v, st_data_t d, int _) +{ + const struct functor *f = (void *)d; + return f->func(k, v, f->arg); +} + int -st_foreach(st_table *tab, int (*func)(ANYARGS), st_data_t arg) +st_foreach(st_table *tab, st_foreach_callback_func *func, st_data_t arg) { - return st_general_foreach(tab, func, NULL, arg, FALSE); + const struct functor f = { func, arg }; + return st_general_foreach(tab, apply_functor, NULL, (st_data_t)&f, FALSE); } /* See comments for function st_delete_safe. */ int -st_foreach_check(st_table *tab, int (*func)(ANYARGS), st_data_t arg, +st_foreach_check(st_table *tab, st_foreach_check_callback_func *func, st_data_t arg, st_data_t never ATTRIBUTE_UNUSED) { return st_general_foreach(tab, func, NULL, arg, TRUE); -- cgit v1.2.3