diff options
author | Eric Wong <e@80x24.org> | 2017-03-14 20:01:18 +0000 |
---|---|---|
committer | Eric Wong <e@80x24.org> | 2017-03-15 20:55:58 +0000 |
commit | 93e936d9a1586d25c338c6974027d6aa70960e1b (patch) | |
tree | 6f26bada6f751ec8f68b989e31e820d8b06a95ae /ext/raindrops/raindrops.c | |
parent | 10cbedc7702240bdf0e04293f0b3f6e1b89f8f87 (diff) | |
download | raindrops-93e936d9a1586d25c338c6974027d6aa70960e1b.tar.gz |
This provides some extra type safety if combined with other C extensions, as well as allowing us to account for memory usage of the HTTP parser in ObjectSpace. This requires Ruby 1.9.3+ and has remained a stable API since then. This will become officially supported when Ruby 2.3.0 is released later this month. This API has only been documented in doc/extension.rdoc (formerly README.EXT) in the Ruby source tree since April 2015, r50318
Diffstat (limited to 'ext/raindrops/raindrops.c')
-rw-r--r-- | ext/raindrops/raindrops.c | 19 |
1 files changed, 16 insertions, 3 deletions
diff --git a/ext/raindrops/raindrops.c b/ext/raindrops/raindrops.c index 65c16e7..390b8b8 100644 --- a/ext/raindrops/raindrops.c +++ b/ext/raindrops/raindrops.c @@ -38,7 +38,7 @@ struct raindrops { }; /* called by GC */ -static void gcfree(void *ptr) +static void rd_free(void *ptr) { struct raindrops *r = ptr; @@ -51,11 +51,24 @@ static void gcfree(void *ptr) xfree(ptr); } +static size_t rd_memsize(const void *ptr) +{ + const struct raindrops *r = ptr; + + return r->drops == MAP_FAILED ? 0 : raindrop_size * r->capa; +} + +static const rb_data_type_t rd_type = { + "raindrops", + { NULL, rd_free, rd_memsize, /* reserved */ }, + /* parent, data, [ flags ] */ +}; + /* automatically called at creation (before initialize) */ static VALUE alloc(VALUE klass) { struct raindrops *r; - VALUE rv = Data_Make_Struct(klass, struct raindrops, NULL, gcfree, r); + VALUE rv = TypedData_Make_Struct(klass, struct raindrops, &rd_type, r); r->drops = MAP_FAILED; return rv; @@ -65,7 +78,7 @@ static struct raindrops *get(VALUE self) { struct raindrops *r; - Data_Get_Struct(self, struct raindrops, r); + TypedData_Get_Struct(self, struct raindrops, &rd_type, r); if (r->drops == MAP_FAILED) rb_raise(rb_eStandardError, "invalid or freed Raindrops"); |