summaryrefslogtreecommitdiff
path: root/struct.c
diff options
context:
space:
mode:
authorakr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2008-06-27 12:56:45 +0000
committerakr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2008-06-27 12:56:45 +0000
commitdfbf04166afce5162f7f66ec72c90b2953b5f0b3 (patch)
tree866696b46a51a8d150a12d528f214d9fa4371e6a /struct.c
parent4bcf877beb7bc1b61f5e6c542619c7991fb5e399 (diff)
* gc.c: __size__ removed. use the length of __members__ instead.
(num_members): new function. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@17615 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'struct.c')
-rw-r--r--struct.c26
1 files changed, 15 insertions, 11 deletions
diff --git a/struct.c b/struct.c
index eecc47254e..027256869a 100644
--- a/struct.c
+++ b/struct.c
@@ -193,7 +193,6 @@ make_struct(VALUE name, VALUE members, VALUE klass)
}
nstr = rb_define_class_under(klass, rb_id2name(id), klass);
}
- rb_iv_set(nstr, "__size__", LONG2NUM(RARRAY_LEN(members)));
rb_iv_set(nstr, "__members__", members);
rb_define_alloc_func(nstr, struct_alloc);
@@ -249,7 +248,6 @@ rb_struct_define_without_accessor(const char *class_name, VALUE super, rb_alloc_
rb_class_inherited(super, klass);
}
- rb_iv_set(klass, "__size__", LONG2NUM(RARRAY_LEN(members)));
rb_iv_set(klass, "__members__", members);
if (alloc)
@@ -340,6 +338,17 @@ rb_struct_s_def(int argc, VALUE *argv, VALUE klass)
return st;
}
+static size_t
+num_members(VALUE klass)
+{
+ VALUE members;
+ members = rb_struct_iv_get(klass, "__members__");
+ if (TYPE(members) != T_ARRAY) {
+ rb_raise(rb_eTypeError, "broken members");
+ }
+ return RARRAY_LEN(members);
+}
+
/*
*/
@@ -347,12 +356,10 @@ VALUE
rb_struct_initialize(VALUE self, VALUE values)
{
VALUE klass = rb_obj_class(self);
- VALUE size;
long n;
rb_struct_modify(self);
- size = rb_struct_iv_get(klass, "__size__");
- n = FIX2LONG(size);
+ n = num_members(klass);
if (n < RARRAY_LEN(values)) {
rb_raise(rb_eArgError, "struct size differs");
}
@@ -367,13 +374,11 @@ rb_struct_initialize(VALUE self, VALUE values)
static VALUE
struct_alloc(VALUE klass)
{
- VALUE size;
long n;
NEWOBJ(st, struct RStruct);
OBJSETUP(st, klass, T_STRUCT);
- size = rb_struct_iv_get(klass, "__size__");
- n = FIX2LONG(size);
+ n = num_members(klass);
if (0 < n && n <= RSTRUCT_EMBED_LEN_MAX) {
RBASIC(st)->flags &= ~RSTRUCT_EMBED_LEN_MASK;
@@ -398,12 +403,11 @@ rb_struct_alloc(VALUE klass, VALUE values)
VALUE
rb_struct_new(VALUE klass, ...)
{
- VALUE sz, *mem;
+ VALUE *mem;
long size, i;
va_list args;
- sz = rb_struct_iv_get(klass, "__size__");
- size = FIX2LONG(sz);
+ size = num_members(klass);
mem = ALLOCA_N(VALUE, size);
va_start(args, klass);
for (i=0; i<size; i++) {