From 25b9eb5e5746665ca098f7117ba207692b2bd78d Mon Sep 17 00:00:00 2001 From: nobu Date: Sun, 18 Jul 2010 07:31:54 +0000 Subject: [PATCH] * include/ruby/ruby.h (rb_data_type_t): restructured. [ruby-dev:41862] add parent member. * error.c (rb_typeddata_inherited_p): new function. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@28676 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 7 +++++++ cont.c | 4 ++-- dir.c | 2 +- encoding.c | 3 ++- enumerator.c | 24 +++++++++++++++--------- error.c | 22 +++++++++++++++++----- ext/bigdecimal/bigdecimal.c | 2 +- ext/dl/cfunc.c | 2 +- ext/dl/cptr.c | 2 +- ext/dl/handle.c | 2 +- ext/socket/raddrinfo.c | 2 +- ext/stringio/stringio.c | 8 +++++--- file.c | 2 +- gc.c | 11 ++++++----- include/ruby/ruby.h | 20 +++++++++++++------- iseq.c | 8 +++++--- marshal.c | 4 ++-- parse.y | 8 +++++--- proc.c | 24 +++++++++++++++--------- random.c | 8 +++++--- thread.c | 6 +++--- time.c | 2 +- transcode.c | 2 +- variable.c | 2 +- vm.c | 12 +++++++----- 25 files changed, 119 insertions(+), 70 deletions(-) diff --git a/ChangeLog b/ChangeLog index edcb38b75a..24c134a9bc 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,10 @@ +Sun Jul 18 16:31:50 2010 Nobuyoshi Nakada + + * include/ruby/ruby.h (rb_data_type_t): restructured. [ruby-dev:41862] + add parent member. + + * error.c (rb_typeddata_inherited_p): new function. + Sun Jul 18 01:09:27 2010 Tanaka Akira * ext/pathname/pathname.c (path_initialize): return a value. diff --git a/cont.c b/cont.c index dec3b01908..6065d34eff 100644 --- a/cont.c +++ b/cont.c @@ -374,7 +374,7 @@ cont_save_machine_stack(rb_thread_t *th, rb_context_t *cont) static const rb_data_type_t cont_data_type = { "continuation", - cont_mark, cont_free, cont_memsize, + {cont_mark, cont_free, cont_memsize,}, }; static void @@ -951,7 +951,7 @@ rb_cont_call(int argc, VALUE *argv, VALUE contval) static const rb_data_type_t fiber_data_type = { "fiber", - fiber_mark, fiber_free, fiber_memsize, + {fiber_mark, fiber_free, fiber_memsize,}, }; static VALUE diff --git a/dir.c b/dir.c index cab0cdcc07..8ebd1de116 100644 --- a/dir.c +++ b/dir.c @@ -344,7 +344,7 @@ dir_memsize(const void *ptr) static const rb_data_type_t dir_data_type = { "dir", - dir_mark, dir_free, dir_memsize + {dir_mark, dir_free, dir_memsize,}, }; static VALUE dir_close(VALUE); diff --git a/encoding.c b/encoding.c index 8f09142b9e..79b90be10c 100644 --- a/encoding.c +++ b/encoding.c @@ -59,7 +59,8 @@ enc_memsize(const void *p) } static const rb_data_type_t encoding_data_type = { - "encoding", 0, 0, enc_memsize, + "encoding", + {0, 0, enc_memsize,}, }; #define is_data_encoding(obj) (RTYPEDDATA_P(obj) && RTYPEDDATA_TYPE(obj) == &encoding_data_type) diff --git a/enumerator.c b/enumerator.c index bdf3bb80d2..47a5c7ecb8 100644 --- a/enumerator.c +++ b/enumerator.c @@ -131,9 +131,11 @@ enumerator_memsize(const void *p) static const rb_data_type_t enumerator_data_type = { "enumerator", - enumerator_mark, - enumerator_free, - enumerator_memsize, + { + enumerator_mark, + enumerator_free, + enumerator_memsize, + }, }; static struct enumerator * @@ -812,9 +814,11 @@ yielder_memsize(const void *p) static const rb_data_type_t yielder_data_type = { "yielder", - yielder_mark, - yielder_free, - yielder_memsize, + { + yielder_mark, + yielder_free, + yielder_memsize, + }, }; static struct yielder * @@ -915,9 +919,11 @@ generator_memsize(const void *p) static const rb_data_type_t generator_data_type = { "generator", - generator_mark, - generator_free, - generator_memsize, + { + generator_mark, + generator_free, + generator_memsize, + }, }; static struct generator * diff --git a/error.c b/error.c index 0e1eab0a77..479caff213 100644 --- a/error.c +++ b/error.c @@ -350,11 +350,21 @@ rb_check_type(VALUE x, int t) } } +int +rb_typeddata_inherited_p(const rb_data_type_t *child, const rb_data_type_t *parent) +{ + while (child) { + if (child == parent) return 1; + child = child->parent; + } + return 0; +} + int rb_typeddata_is_kind_of(VALUE obj, const rb_data_type_t *data_type) { if (SPECIAL_CONST_P(obj) || BUILTIN_TYPE(obj) != T_DATA || - !RTYPEDDATA_P(obj) || RTYPEDDATA_TYPE(obj) != data_type) { + !RTYPEDDATA_P(obj) || !rb_typeddata_inherited_p(RTYPEDDATA_TYPE(obj), data_type)) { return 0; } return 1; @@ -373,7 +383,7 @@ rb_check_typeddata(VALUE obj, const rb_data_type_t *data_type) etype = rb_obj_classname(obj); rb_raise(rb_eTypeError, mesg, etype, data_type->wrap_struct_name); } - else if (RTYPEDDATA_TYPE(obj) != data_type) { + else if (!rb_typeddata_inherited_p(RTYPEDDATA_TYPE(obj), data_type)) { etype = RTYPEDDATA_TYPE(obj)->wrap_struct_name; rb_raise(rb_eTypeError, mesg, etype, data_type->wrap_struct_name); } @@ -815,9 +825,11 @@ name_err_mesg_memsize(const void *p) static const rb_data_type_t name_err_mesg_data_type = { "name_err_mesg", - name_err_mesg_mark, - name_err_mesg_free, - name_err_mesg_memsize, + { + name_err_mesg_mark, + name_err_mesg_free, + name_err_mesg_memsize, + }, }; /* :nodoc: */ diff --git a/ext/bigdecimal/bigdecimal.c b/ext/bigdecimal/bigdecimal.c index 340ea8bb80..146145ce41 100644 --- a/ext/bigdecimal/bigdecimal.c +++ b/ext/bigdecimal/bigdecimal.c @@ -105,7 +105,7 @@ BigDecimal_memsize(const void *ptr) static const rb_data_type_t BigDecimal_data_type = { "BigDecimal", - 0, BigDecimal_delete, BigDecimal_memsize, + {0, BigDecimal_delete, BigDecimal_memsize,}, }; static VALUE diff --git a/ext/dl/cfunc.c b/ext/dl/cfunc.c index 536a7231a8..7eb58603aa 100644 --- a/ext/dl/cfunc.c +++ b/ext/dl/cfunc.c @@ -70,7 +70,7 @@ dlcfunc_memsize(const void *ptr) const rb_data_type_t dlcfunc_data_type = { "dl/cfunc", - 0, dlcfunc_free, dlcfunc_memsize, + {0, dlcfunc_free, dlcfunc_memsize,}, }; VALUE diff --git a/ext/dl/cptr.c b/ext/dl/cptr.c index bafbc48be6..68c18ec32c 100644 --- a/ext/dl/cptr.c +++ b/ext/dl/cptr.c @@ -43,7 +43,7 @@ dlptr_memsize(const void *ptr) static const rb_data_type_t dlptr_data_type = { "dl/ptr", - 0, dlptr_free, dlptr_memsize, + {0, dlptr_free, dlptr_memsize,}, }; void diff --git a/ext/dl/handle.c b/ext/dl/handle.c index a450e6bac5..ed042e12c7 100644 --- a/ext/dl/handle.c +++ b/ext/dl/handle.c @@ -48,7 +48,7 @@ dlhandle_memsize(const void *ptr) static const rb_data_type_t dlhandle_data_type = { "dl/handle", - 0, dlhandle_free, dlhandle_memsize, + {0, dlhandle_free, dlhandle_memsize,}, }; /* diff --git a/ext/socket/raddrinfo.c b/ext/socket/raddrinfo.c index 4bc20c913e..2e5f95ad1d 100644 --- a/ext/socket/raddrinfo.c +++ b/ext/socket/raddrinfo.c @@ -534,7 +534,7 @@ addrinfo_memsize(const void *ptr) static const rb_data_type_t addrinfo_type = { "socket/addrinfo", - addrinfo_mark, addrinfo_free, addrinfo_memsize, + {addrinfo_mark, addrinfo_free, addrinfo_memsize,}, }; static VALUE diff --git a/ext/stringio/stringio.c b/ext/stringio/stringio.c index c93ba10466..4628a357dd 100644 --- a/ext/stringio/stringio.c +++ b/ext/stringio/stringio.c @@ -73,9 +73,11 @@ strio_memsize(const void *p) static const rb_data_type_t strio_data_type = { "strio", - strio_mark, - strio_free, - strio_memsize, + { + strio_mark, + strio_free, + strio_memsize, + }, }; #define check_strio(self) ((struct StringIO*)rb_check_typeddata(self, &strio_data_type)) diff --git a/file.c b/file.c index 94c2468179..37c9c47588 100644 --- a/file.c +++ b/file.c @@ -247,7 +247,7 @@ stat_memsize(const void *p) static const rb_data_type_t stat_data_type = { "stat", - NULL, RUBY_TYPED_DEFAULT_FREE, stat_memsize, + {NULL, RUBY_TYPED_DEFAULT_FREE, stat_memsize,}, }; static VALUE diff --git a/gc.c b/gc.c index 9a96e31da0..0c139400cf 100644 --- a/gc.c +++ b/gc.c @@ -1177,7 +1177,7 @@ size_t rb_objspace_data_type_memsize(VALUE obj) { if (RTYPEDDATA_P(obj)) { - return RTYPEDDATA_TYPE(obj)->dsize(RTYPEDDATA_DATA(obj)); + return RTYPEDDATA_TYPE(obj)->function.dsize(RTYPEDDATA_DATA(obj)); } else { return 0; @@ -1749,7 +1749,8 @@ gc_mark_children(rb_objspace_t *objspace, VALUE ptr, int lev) case T_DATA: if (RTYPEDDATA_P(obj)) { - if (obj->as.typeddata.type->dmark) (*obj->as.typeddata.type->dmark)(DATA_PTR(obj)); + RUBY_DATA_FUNC mark_func = obj->as.typeddata.type->function.dmark; + if (mark_func) (*mark_func)(DATA_PTR(obj)); } else { if (obj->as.data.dmark) (*obj->as.data.dmark)(DATA_PTR(obj)); @@ -2186,7 +2187,7 @@ obj_free(rb_objspace_t *objspace, VALUE obj) case T_DATA: if (DATA_PTR(obj)) { if (RTYPEDDATA_P(obj)) { - RDATA(obj)->dfree = RANY(obj)->as.typeddata.type->dfree; + RDATA(obj)->dfree = RANY(obj)->as.typeddata.type->function.dfree; } if ((long)RANY(obj)->as.data.dfree == -1) { xfree(DATA_PTR(obj)); @@ -2780,7 +2781,7 @@ run_final(rb_objspace_t *objspace, VALUE obj) RBASIC(obj)->klass = 0; if (RTYPEDDATA_P(obj)) { - free_func = RTYPEDDATA_TYPE(obj)->dfree; + free_func = RTYPEDDATA_TYPE(obj)->function.dfree; } else { free_func = RDATA(obj)->dfree; @@ -2902,7 +2903,7 @@ rb_objspace_call_finalizer(rb_objspace_t *objspace) RANY(p)->as.basic.klass != rb_cThread && RANY(p)->as.basic.klass != rb_cMutex) { p->as.free.flags = 0; if (RTYPEDDATA_P(p)) { - RDATA(p)->dfree = RANY(p)->as.typeddata.type->dfree; + RDATA(p)->dfree = RANY(p)->as.typeddata.type->function.dfree; } if ((long)RANY(p)->as.data.dfree == -1) { xfree(DATA_PTR(p)); diff --git a/include/ruby/ruby.h b/include/ruby/ruby.h index 03984b4d2e..4d8b06feb9 100644 --- a/include/ruby/ruby.h +++ b/include/ruby/ruby.h @@ -742,16 +742,21 @@ struct RData { void *data; }; -typedef struct rb_data_type_struct { +typedef struct rb_data_type_struct rb_data_type_t; + +struct rb_data_type_struct { const char *wrap_struct_name; - void (*dmark)(void*); - void (*dfree)(void*); - size_t (*dsize)(const void *); - void *reserved[3]; /* For future extension. - This array *must* be filled with ZERO. */ + struct { + void (*dmark)(void*); + void (*dfree)(void*); + size_t (*dsize)(const void *); + void *reserved[2]; /* For future extension. + This array *must* be filled with ZERO. */ + } function; + const rb_data_type_t *parent; void *data; /* This area can be used for any purpose by a programmer who define the type. */ -} rb_data_type_t; +}; struct RTypedData { struct RBasic basic; @@ -773,6 +778,7 @@ typedef void (*RUBY_DATA_FUNC)(void*); VALUE rb_data_object_alloc(VALUE,void*,RUBY_DATA_FUNC,RUBY_DATA_FUNC); VALUE rb_data_typed_object_alloc(VALUE klass, void *datap, const rb_data_type_t *); +int rb_typeddata_inherited_p(const rb_data_type_t *child, const rb_data_type_t *parent); int rb_typeddata_is_kind_of(VALUE, const rb_data_type_t *); void *rb_check_typeddata(VALUE, const rb_data_type_t *); #define Check_TypedStruct(v,t) rb_check_typeddata((VALUE)(v),t) diff --git a/iseq.c b/iseq.c index ae45884578..fc9b101cab 100644 --- a/iseq.c +++ b/iseq.c @@ -156,9 +156,11 @@ iseq_memsize(const void *ptr) static const rb_data_type_t iseq_data_type = { "iseq", - iseq_mark, - iseq_free, - iseq_memsize, + { + iseq_mark, + iseq_free, + iseq_memsize, + }, }; static VALUE diff --git a/marshal.c b/marshal.c index bf76e6d524..bcbc2def83 100644 --- a/marshal.c +++ b/marshal.c @@ -186,7 +186,7 @@ memsize_dump_arg(const void *ptr) static const rb_data_type_t dump_arg_data = { "dump_arg", - mark_dump_arg, free_dump_arg, memsize_dump_arg + {mark_dump_arg, free_dump_arg, memsize_dump_arg,}, }; static const char * @@ -999,7 +999,7 @@ memsize_load_arg(const void *ptr) static const rb_data_type_t load_arg_data = { "load_arg", - mark_load_arg, free_load_arg, memsize_load_arg + {mark_load_arg, free_load_arg, memsize_load_arg,}, }; #define r_entry(v, arg) r_entry0(v, (arg)->data->num_entries, arg) diff --git a/parse.y b/parse.y index b9f59d4a9c..6b4bf956f0 100644 --- a/parse.y +++ b/parse.y @@ -10027,9 +10027,11 @@ parser_memsize(const void *ptr) static const rb_data_type_t parser_data_type = { "parser", - parser_mark, - parser_free, - parser_memsize, + { + parser_mark, + parser_free, + parser_memsize, + }, }; VALUE rb_parser_get_yydebug(VALUE); diff --git a/proc.c b/proc.c index 7df2ec890b..0489dd967f 100644 --- a/proc.c +++ b/proc.c @@ -71,9 +71,11 @@ proc_memsize(const void *ptr) static const rb_data_type_t proc_data_type = { "proc", - proc_mark, - proc_free, - proc_memsize, + { + proc_mark, + proc_free, + proc_memsize, + }, }; VALUE @@ -268,9 +270,11 @@ binding_memsize(const void *ptr) static const rb_data_type_t binding_data_type = { "binding", - binding_mark, - binding_free, - binding_memsize, + { + binding_mark, + binding_free, + binding_memsize, + }, }; static VALUE @@ -882,9 +886,11 @@ bm_memsize(const void *ptr) static const rb_data_type_t method_data_type = { "method", - bm_mark, - bm_free, - bm_memsize, + { + bm_mark, + bm_free, + bm_memsize, + }, }; static inline int diff --git a/random.c b/random.c index 333c593ff5..72ef7c5780 100644 --- a/random.c +++ b/random.c @@ -339,9 +339,11 @@ random_memsize(const void *ptr) static const rb_data_type_t random_data_type = { "random", - random_mark, - random_free, - random_memsize, + { + random_mark, + random_free, + random_memsize, + }, }; static rb_random_t * diff --git a/thread.c b/thread.c index de60b4d226..49d4d28df0 100644 --- a/thread.c +++ b/thread.c @@ -2824,7 +2824,7 @@ thgroup_memsize(const void *ptr) static const rb_data_type_t thgroup_data_type = { "thgroup", - NULL, RUBY_TYPED_DEFAULT_FREE, thgroup_memsize, + {NULL, RUBY_TYPED_DEFAULT_FREE, thgroup_memsize,}, }; /* @@ -3062,7 +3062,7 @@ mutex_memsize(const void *ptr) static const rb_data_type_t mutex_data_type = { "mutex", - mutex_mark, mutex_free, mutex_memsize, + {mutex_mark, mutex_free, mutex_memsize,}, }; static VALUE @@ -3433,7 +3433,7 @@ barrier_mark(void *ptr) static const rb_data_type_t barrier_data_type = { "barrier", - barrier_mark, 0, 0, + {barrier_mark, 0, 0,}, }; static VALUE diff --git a/time.c b/time.c index ad567133d3..c95039e7f5 100644 --- a/time.c +++ b/time.c @@ -1851,7 +1851,7 @@ time_memsize(const void *tobj) static const rb_data_type_t time_data_type = { "time", - time_mark, time_free, time_memsize, + {time_mark, time_free, time_memsize,}, }; static VALUE diff --git a/transcode.c b/transcode.c index dadb57b90d..c718182a14 100644 --- a/transcode.c +++ b/transcode.c @@ -2829,7 +2829,7 @@ econv_memsize(const void *ptr) static const rb_data_type_t econv_data_type = { "econv", - NULL, econv_free, econv_memsize, + {NULL, econv_free, econv_memsize,}, }; static VALUE diff --git a/variable.c b/variable.c index 382644b48a..33e663293e 100644 --- a/variable.c +++ b/variable.c @@ -1418,7 +1418,7 @@ autoload_memsize(const void *ptr) static const rb_data_type_t autoload_data_type = { "autoload", - autoload_mark, autoload_free, autoload_memsize, + {autoload_mark, autoload_free, autoload_memsize,}, }; #define check_autoload_table(av) \ diff --git a/vm.c b/vm.c index 8e93963a39..f8509d766b 100644 --- a/vm.c +++ b/vm.c @@ -244,7 +244,7 @@ env_memsize(const void *ptr) static const rb_data_type_t env_data_type = { "VM/env", - env_mark, env_free, env_memsize, + {env_mark, env_free, env_memsize,}, }; static VALUE @@ -1569,7 +1569,7 @@ vm_memsize(const void *ptr) static const rb_data_type_t vm_data_type = { "VM", - rb_vm_mark, vm_free, vm_memsize, + {rb_vm_mark, vm_free, vm_memsize,}, }; static void @@ -1759,9 +1759,11 @@ thread_memsize(const void *ptr) static const rb_data_type_t thread_data_type = { "VM/thread", - rb_thread_mark, - thread_free, - thread_memsize, + { + rb_thread_mark, + thread_free, + thread_memsize, + }, }; static VALUE