From ab7695fc5080fe0313ec4b62f3abb3156df08e3b Mon Sep 17 00:00:00 2001 From: nobu Date: Sat, 4 Oct 2014 23:33:06 +0000 Subject: [PATCH] sdbm: typed data * ext/sdbm/init.c (sdbm_type): turn into typed data. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@47796 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ext/sdbm/init.c | 30 +++++++++++++++++++++++++----- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/ext/sdbm/init.c b/ext/sdbm/init.c index eb9d3c917d..821918cff0 100644 --- a/ext/sdbm/init.c +++ b/ext/sdbm/init.c @@ -78,7 +78,7 @@ closed_sdbm(void) } #define GetDBM(obj, dbmp) do {\ - Data_Get_Struct((obj), struct dbmdata, (dbmp));\ + TypedData_Get_Struct((obj), struct dbmdata, &sdbm_type, (dbmp));\ if ((dbmp) == 0) closed_sdbm();\ if ((dbmp)->di_dbm == 0) closed_sdbm();\ } while (0) @@ -89,13 +89,33 @@ closed_sdbm(void) } while (0) static void -free_sdbm(struct dbmdata *dbmp) +free_sdbm(void *ptr) { + struct dbmdata *dbmp = ptr; if (dbmp->di_dbm) sdbm_close(dbmp->di_dbm); ruby_xfree(dbmp); } +static size_t +memsize_dbm(const void *ptr) +{ + size_t size = 0; + const struct dbmdata *dbmp = ptr; + if (dbmp) { + size += sizeof(*dbmp); + if (dbmp->di_dbm) size += sizeof(DBM); + } + return size; +} + +static const rb_data_type_t sdbm_type = { + "sdbm", + {0, free_sdbm, memsize_dbm,}, + NULL, NULL, + RUBY_TYPED_FREE_IMMEDIATELY, +}; + /* * call-seq: * sdbm.close -> nil @@ -127,7 +147,7 @@ fsdbm_closed(VALUE obj) { struct dbmdata *dbmp; - Data_Get_Struct(obj, struct dbmdata, dbmp); + TypedData_Get_Struct(obj, struct dbmdata, &sdbm_type, dbmp); if (dbmp == 0) return Qtrue; if (dbmp->di_dbm == 0) @@ -139,7 +159,7 @@ fsdbm_closed(VALUE obj) static VALUE fsdbm_alloc(VALUE klass) { - return Data_Wrap_Struct(klass, 0, free_sdbm, 0); + return TypedData_Wrap_Struct(klass, &sdbm_type, 0); } /* * call-seq: @@ -218,7 +238,7 @@ fsdbm_initialize(int argc, VALUE *argv, VALUE obj) static VALUE fsdbm_s_open(int argc, VALUE *argv, VALUE klass) { - VALUE obj = Data_Wrap_Struct(klass, 0, free_sdbm, 0); + VALUE obj = fsdbm_alloc(klass); if (NIL_P(fsdbm_initialize(argc, argv, obj))) { return Qnil;