Skip to content

Commit

Permalink
* include/ruby/ruby.h (rb_data_type_t): restructured. [ruby-dev:41862]
Browse files Browse the repository at this point in the history
  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
  • Loading branch information
nobu committed Jul 18, 2010
1 parent ad654fe commit 25b9eb5
Show file tree
Hide file tree
Showing 25 changed files with 119 additions and 70 deletions.
7 changes: 7 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -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>

* ext/pathname/pathname.c (path_initialize): return a value.
Expand Down
4 changes: 2 additions & 2 deletions cont.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion dir.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
3 changes: 2 additions & 1 deletion encoding.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
24 changes: 15 additions & 9 deletions enumerator.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 *
Expand Down Expand Up @@ -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 *
Expand Down Expand Up @@ -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 *
Expand Down
22 changes: 17 additions & 5 deletions error.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}
Expand Down Expand Up @@ -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: */
Expand Down
2 changes: 1 addition & 1 deletion ext/bigdecimal/bigdecimal.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion ext/dl/cfunc.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion ext/dl/cptr.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion ext/dl/handle.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,},
};

/*
Expand Down
2 changes: 1 addition & 1 deletion ext/socket/raddrinfo.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 5 additions & 3 deletions ext/stringio/stringio.c
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
2 changes: 1 addition & 1 deletion file.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 6 additions & 5 deletions gc.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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));
Expand Down
20 changes: 13 additions & 7 deletions include/ruby/ruby.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)
Expand Down
8 changes: 5 additions & 3 deletions iseq.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions marshal.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 *
Expand Down Expand Up @@ -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)
Expand Down
8 changes: 5 additions & 3 deletions parse.y
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
24 changes: 15 additions & 9 deletions proc.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
8 changes: 5 additions & 3 deletions random.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 *
Expand Down
Loading

0 comments on commit 25b9eb5

Please sign in to comment.