* 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
This commit is contained in:
nobu 2010-07-18 07:31:54 +00:00
Родитель ad654fe58e
Коммит 25b9eb5e57
25 изменённых файлов: 119 добавлений и 70 удалений

Просмотреть файл

@ -1,3 +1,10 @@
Sun Jul 18 16:31:50 2010 Nobuyoshi Nakada <nobu@ruby-lang.org>
* 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 <akr@fsij.org> Sun Jul 18 01:09:27 2010 Tanaka Akira <akr@fsij.org>
* ext/pathname/pathname.c (path_initialize): return a value. * ext/pathname/pathname.c (path_initialize): return a value.

4
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 = { static const rb_data_type_t cont_data_type = {
"continuation", "continuation",
cont_mark, cont_free, cont_memsize, {cont_mark, cont_free, cont_memsize,},
}; };
static void static void
@ -951,7 +951,7 @@ rb_cont_call(int argc, VALUE *argv, VALUE contval)
static const rb_data_type_t fiber_data_type = { static const rb_data_type_t fiber_data_type = {
"fiber", "fiber",
fiber_mark, fiber_free, fiber_memsize, {fiber_mark, fiber_free, fiber_memsize,},
}; };
static VALUE static VALUE

2
dir.c
Просмотреть файл

@ -344,7 +344,7 @@ dir_memsize(const void *ptr)
static const rb_data_type_t dir_data_type = { static const rb_data_type_t dir_data_type = {
"dir", "dir",
dir_mark, dir_free, dir_memsize {dir_mark, dir_free, dir_memsize,},
}; };
static VALUE dir_close(VALUE); static VALUE dir_close(VALUE);

Просмотреть файл

@ -59,7 +59,8 @@ enc_memsize(const void *p)
} }
static const rb_data_type_t encoding_data_type = { 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) #define is_data_encoding(obj) (RTYPEDDATA_P(obj) && RTYPEDDATA_TYPE(obj) == &encoding_data_type)

Просмотреть файл

@ -131,9 +131,11 @@ enumerator_memsize(const void *p)
static const rb_data_type_t enumerator_data_type = { static const rb_data_type_t enumerator_data_type = {
"enumerator", "enumerator",
enumerator_mark, {
enumerator_free, enumerator_mark,
enumerator_memsize, enumerator_free,
enumerator_memsize,
},
}; };
static struct enumerator * static struct enumerator *
@ -812,9 +814,11 @@ yielder_memsize(const void *p)
static const rb_data_type_t yielder_data_type = { static const rb_data_type_t yielder_data_type = {
"yielder", "yielder",
yielder_mark, {
yielder_free, yielder_mark,
yielder_memsize, yielder_free,
yielder_memsize,
},
}; };
static struct yielder * static struct yielder *
@ -915,9 +919,11 @@ generator_memsize(const void *p)
static const rb_data_type_t generator_data_type = { static const rb_data_type_t generator_data_type = {
"generator", "generator",
generator_mark, {
generator_free, generator_mark,
generator_memsize, generator_free,
generator_memsize,
},
}; };
static struct generator * static struct generator *

22
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 int
rb_typeddata_is_kind_of(VALUE obj, const rb_data_type_t *data_type) rb_typeddata_is_kind_of(VALUE obj, const rb_data_type_t *data_type)
{ {
if (SPECIAL_CONST_P(obj) || BUILTIN_TYPE(obj) != T_DATA || 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 0;
} }
return 1; return 1;
@ -373,7 +383,7 @@ rb_check_typeddata(VALUE obj, const rb_data_type_t *data_type)
etype = rb_obj_classname(obj); etype = rb_obj_classname(obj);
rb_raise(rb_eTypeError, mesg, etype, data_type->wrap_struct_name); 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; etype = RTYPEDDATA_TYPE(obj)->wrap_struct_name;
rb_raise(rb_eTypeError, mesg, etype, data_type->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 = { static const rb_data_type_t name_err_mesg_data_type = {
"name_err_mesg", "name_err_mesg",
name_err_mesg_mark, {
name_err_mesg_free, name_err_mesg_mark,
name_err_mesg_memsize, name_err_mesg_free,
name_err_mesg_memsize,
},
}; };
/* :nodoc: */ /* :nodoc: */

Просмотреть файл

@ -105,7 +105,7 @@ BigDecimal_memsize(const void *ptr)
static const rb_data_type_t BigDecimal_data_type = { static const rb_data_type_t BigDecimal_data_type = {
"BigDecimal", "BigDecimal",
0, BigDecimal_delete, BigDecimal_memsize, {0, BigDecimal_delete, BigDecimal_memsize,},
}; };
static VALUE static VALUE

Просмотреть файл

@ -70,7 +70,7 @@ dlcfunc_memsize(const void *ptr)
const rb_data_type_t dlcfunc_data_type = { const rb_data_type_t dlcfunc_data_type = {
"dl/cfunc", "dl/cfunc",
0, dlcfunc_free, dlcfunc_memsize, {0, dlcfunc_free, dlcfunc_memsize,},
}; };
VALUE VALUE

Просмотреть файл

@ -43,7 +43,7 @@ dlptr_memsize(const void *ptr)
static const rb_data_type_t dlptr_data_type = { static const rb_data_type_t dlptr_data_type = {
"dl/ptr", "dl/ptr",
0, dlptr_free, dlptr_memsize, {0, dlptr_free, dlptr_memsize,},
}; };
void void

Просмотреть файл

@ -48,7 +48,7 @@ dlhandle_memsize(const void *ptr)
static const rb_data_type_t dlhandle_data_type = { static const rb_data_type_t dlhandle_data_type = {
"dl/handle", "dl/handle",
0, dlhandle_free, dlhandle_memsize, {0, dlhandle_free, dlhandle_memsize,},
}; };
/* /*

Просмотреть файл

@ -534,7 +534,7 @@ addrinfo_memsize(const void *ptr)
static const rb_data_type_t addrinfo_type = { static const rb_data_type_t addrinfo_type = {
"socket/addrinfo", "socket/addrinfo",
addrinfo_mark, addrinfo_free, addrinfo_memsize, {addrinfo_mark, addrinfo_free, addrinfo_memsize,},
}; };
static VALUE static VALUE

Просмотреть файл

@ -73,9 +73,11 @@ strio_memsize(const void *p)
static const rb_data_type_t strio_data_type = { static const rb_data_type_t strio_data_type = {
"strio", "strio",
strio_mark, {
strio_free, strio_mark,
strio_memsize, strio_free,
strio_memsize,
},
}; };
#define check_strio(self) ((struct StringIO*)rb_check_typeddata(self, &strio_data_type)) #define check_strio(self) ((struct StringIO*)rb_check_typeddata(self, &strio_data_type))

2
file.c
Просмотреть файл

@ -247,7 +247,7 @@ stat_memsize(const void *p)
static const rb_data_type_t stat_data_type = { static const rb_data_type_t stat_data_type = {
"stat", "stat",
NULL, RUBY_TYPED_DEFAULT_FREE, stat_memsize, {NULL, RUBY_TYPED_DEFAULT_FREE, stat_memsize,},
}; };
static VALUE static VALUE

11
gc.c
Просмотреть файл

@ -1177,7 +1177,7 @@ size_t
rb_objspace_data_type_memsize(VALUE obj) rb_objspace_data_type_memsize(VALUE obj)
{ {
if (RTYPEDDATA_P(obj)) { if (RTYPEDDATA_P(obj)) {
return RTYPEDDATA_TYPE(obj)->dsize(RTYPEDDATA_DATA(obj)); return RTYPEDDATA_TYPE(obj)->function.dsize(RTYPEDDATA_DATA(obj));
} }
else { else {
return 0; return 0;
@ -1749,7 +1749,8 @@ gc_mark_children(rb_objspace_t *objspace, VALUE ptr, int lev)
case T_DATA: case T_DATA:
if (RTYPEDDATA_P(obj)) { 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 { else {
if (obj->as.data.dmark) (*obj->as.data.dmark)(DATA_PTR(obj)); 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: case T_DATA:
if (DATA_PTR(obj)) { if (DATA_PTR(obj)) {
if (RTYPEDDATA_P(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) { if ((long)RANY(obj)->as.data.dfree == -1) {
xfree(DATA_PTR(obj)); xfree(DATA_PTR(obj));
@ -2780,7 +2781,7 @@ run_final(rb_objspace_t *objspace, VALUE obj)
RBASIC(obj)->klass = 0; RBASIC(obj)->klass = 0;
if (RTYPEDDATA_P(obj)) { if (RTYPEDDATA_P(obj)) {
free_func = RTYPEDDATA_TYPE(obj)->dfree; free_func = RTYPEDDATA_TYPE(obj)->function.dfree;
} }
else { else {
free_func = RDATA(obj)->dfree; 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) { RANY(p)->as.basic.klass != rb_cThread && RANY(p)->as.basic.klass != rb_cMutex) {
p->as.free.flags = 0; p->as.free.flags = 0;
if (RTYPEDDATA_P(p)) { 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) { if ((long)RANY(p)->as.data.dfree == -1) {
xfree(DATA_PTR(p)); xfree(DATA_PTR(p));

Просмотреть файл

@ -742,16 +742,21 @@ struct RData {
void *data; 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; const char *wrap_struct_name;
void (*dmark)(void*); struct {
void (*dfree)(void*); void (*dmark)(void*);
size_t (*dsize)(const void *); void (*dfree)(void*);
void *reserved[3]; /* For future extension. size_t (*dsize)(const void *);
This array *must* be filled with ZERO. */ 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 void *data; /* This area can be used for any purpose
by a programmer who define the type. */ by a programmer who define the type. */
} rb_data_type_t; };
struct RTypedData { struct RTypedData {
struct RBasic basic; 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_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 *); 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 *); int rb_typeddata_is_kind_of(VALUE, const rb_data_type_t *);
void *rb_check_typeddata(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) #define Check_TypedStruct(v,t) rb_check_typeddata((VALUE)(v),t)

8
iseq.c
Просмотреть файл

@ -156,9 +156,11 @@ iseq_memsize(const void *ptr)
static const rb_data_type_t iseq_data_type = { static const rb_data_type_t iseq_data_type = {
"iseq", "iseq",
iseq_mark, {
iseq_free, iseq_mark,
iseq_memsize, iseq_free,
iseq_memsize,
},
}; };
static VALUE static VALUE

Просмотреть файл

@ -186,7 +186,7 @@ memsize_dump_arg(const void *ptr)
static const rb_data_type_t dump_arg_data = { static const rb_data_type_t dump_arg_data = {
"dump_arg", "dump_arg",
mark_dump_arg, free_dump_arg, memsize_dump_arg {mark_dump_arg, free_dump_arg, memsize_dump_arg,},
}; };
static const char * static const char *
@ -999,7 +999,7 @@ memsize_load_arg(const void *ptr)
static const rb_data_type_t load_arg_data = { static const rb_data_type_t load_arg_data = {
"load_arg", "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) #define r_entry(v, arg) r_entry0(v, (arg)->data->num_entries, arg)

Просмотреть файл

@ -10027,9 +10027,11 @@ parser_memsize(const void *ptr)
static const rb_data_type_t parser_data_type = { static const rb_data_type_t parser_data_type = {
"parser", "parser",
parser_mark, {
parser_free, parser_mark,
parser_memsize, parser_free,
parser_memsize,
},
}; };
VALUE rb_parser_get_yydebug(VALUE); VALUE rb_parser_get_yydebug(VALUE);

24
proc.c
Просмотреть файл

@ -71,9 +71,11 @@ proc_memsize(const void *ptr)
static const rb_data_type_t proc_data_type = { static const rb_data_type_t proc_data_type = {
"proc", "proc",
proc_mark, {
proc_free, proc_mark,
proc_memsize, proc_free,
proc_memsize,
},
}; };
VALUE VALUE
@ -268,9 +270,11 @@ binding_memsize(const void *ptr)
static const rb_data_type_t binding_data_type = { static const rb_data_type_t binding_data_type = {
"binding", "binding",
binding_mark, {
binding_free, binding_mark,
binding_memsize, binding_free,
binding_memsize,
},
}; };
static VALUE static VALUE
@ -882,9 +886,11 @@ bm_memsize(const void *ptr)
static const rb_data_type_t method_data_type = { static const rb_data_type_t method_data_type = {
"method", "method",
bm_mark, {
bm_free, bm_mark,
bm_memsize, bm_free,
bm_memsize,
},
}; };
static inline int static inline int

Просмотреть файл

@ -339,9 +339,11 @@ random_memsize(const void *ptr)
static const rb_data_type_t random_data_type = { static const rb_data_type_t random_data_type = {
"random", "random",
random_mark, {
random_free, random_mark,
random_memsize, random_free,
random_memsize,
},
}; };
static rb_random_t * static rb_random_t *

Просмотреть файл

@ -2824,7 +2824,7 @@ thgroup_memsize(const void *ptr)
static const rb_data_type_t thgroup_data_type = { static const rb_data_type_t thgroup_data_type = {
"thgroup", "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 = { static const rb_data_type_t mutex_data_type = {
"mutex", "mutex",
mutex_mark, mutex_free, mutex_memsize, {mutex_mark, mutex_free, mutex_memsize,},
}; };
static VALUE static VALUE
@ -3433,7 +3433,7 @@ barrier_mark(void *ptr)
static const rb_data_type_t barrier_data_type = { static const rb_data_type_t barrier_data_type = {
"barrier", "barrier",
barrier_mark, 0, 0, {barrier_mark, 0, 0,},
}; };
static VALUE static VALUE

2
time.c
Просмотреть файл

@ -1851,7 +1851,7 @@ time_memsize(const void *tobj)
static const rb_data_type_t time_data_type = { static const rb_data_type_t time_data_type = {
"time", "time",
time_mark, time_free, time_memsize, {time_mark, time_free, time_memsize,},
}; };
static VALUE static VALUE

Просмотреть файл

@ -2829,7 +2829,7 @@ econv_memsize(const void *ptr)
static const rb_data_type_t econv_data_type = { static const rb_data_type_t econv_data_type = {
"econv", "econv",
NULL, econv_free, econv_memsize, {NULL, econv_free, econv_memsize,},
}; };
static VALUE static VALUE

Просмотреть файл

@ -1418,7 +1418,7 @@ autoload_memsize(const void *ptr)
static const rb_data_type_t autoload_data_type = { static const rb_data_type_t autoload_data_type = {
"autoload", "autoload",
autoload_mark, autoload_free, autoload_memsize, {autoload_mark, autoload_free, autoload_memsize,},
}; };
#define check_autoload_table(av) \ #define check_autoload_table(av) \

12
vm.c
Просмотреть файл

@ -244,7 +244,7 @@ env_memsize(const void *ptr)
static const rb_data_type_t env_data_type = { static const rb_data_type_t env_data_type = {
"VM/env", "VM/env",
env_mark, env_free, env_memsize, {env_mark, env_free, env_memsize,},
}; };
static VALUE static VALUE
@ -1569,7 +1569,7 @@ vm_memsize(const void *ptr)
static const rb_data_type_t vm_data_type = { static const rb_data_type_t vm_data_type = {
"VM", "VM",
rb_vm_mark, vm_free, vm_memsize, {rb_vm_mark, vm_free, vm_memsize,},
}; };
static void static void
@ -1759,9 +1759,11 @@ thread_memsize(const void *ptr)
static const rb_data_type_t thread_data_type = { static const rb_data_type_t thread_data_type = {
"VM/thread", "VM/thread",
rb_thread_mark, {
thread_free, rb_thread_mark,
thread_memsize, thread_free,
thread_memsize,
},
}; };
static VALUE static VALUE