diff --git a/ChangeLog b/ChangeLog index 13e76d663f..d18e0c0d9b 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,7 @@ +Thu Dec 15 17:56:58 2011 Nobuyoshi Nakada + + * io.c (argf_type): make typed data. + Thu Dec 15 17:40:28 2011 Nobuyoshi Nakada * error.c (rb_check_type): fix typo. diff --git a/io.c b/io.c index 3e184f2995..fc4fba7aa2 100644 --- a/io.c +++ b/io.c @@ -7133,6 +7133,21 @@ argf_free(void *ptr) xfree(p); } +static size_t +argf_memsize(const void *ptr) +{ + const struct argf *p = ptr; + size_t size = sizeof(*p); + if (!ptr) return 0; + if (p->inplace) size += strlen(p->inplace) + 1; + return size; +} + +static const rb_data_type_t argf_type = { + "ARGF", + {argf_mark, argf_free, argf_memsize}, +}; + static inline void argf_init(struct argf *p, VALUE v) { @@ -7146,7 +7161,7 @@ static VALUE argf_alloc(VALUE klass) { struct argf *p; - VALUE argf = Data_Make_Struct(klass, struct argf, argf_mark, argf_free, p); + VALUE argf = TypedData_Make_Struct(klass, struct argf, &argf_type, p); argf_init(p, Qnil); return argf; diff --git a/test/objspace/test_objspace.rb b/test/objspace/test_objspace.rb index 3c22dae551..345b00f737 100644 --- a/test/objspace/test_objspace.rb +++ b/test/objspace/test_objspace.rb @@ -1,5 +1,6 @@ require "test/unit" require "objspace" +require_relative "../ruby/envutil" class TestObjSpace < Test::Unit::TestCase def test_memsize_of @@ -23,6 +24,17 @@ class TestObjSpace < Test::Unit::TestCase ObjectSpace.memsize_of(//.match(""))) end + def test_argf_memsize + size = ObjectSpace.memsize_of(ARGF) + assert_kind_of(Integer, size) + assert_operator(size, :>, 0) + argf = ARGF.dup + argf.inplace_mode = nil + size = ObjectSpace.memsize_of(argf) + argf.inplace_mode = "inplace_mode_suffix" + assert_equal(size + 20, ObjectSpace.memsize_of(argf)) + end + def test_memsize_of_all assert_kind_of(Integer, a = ObjectSpace.memsize_of_all) assert_kind_of(Integer, b = ObjectSpace.memsize_of_all(String))