ruby-changes:35714
From: nobu <ko1@a...>
Date: Sun, 5 Oct 2014 08:33:17 +0900 (JST)
Subject: [ruby-changes:35714] nobu:r47795 (trunk): gdbm: typed data
nobu 2014-10-05 08:33:04 +0900 (Sun, 05 Oct 2014) New Revision: 47795 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=47795 Log: gdbm: typed data * ext/gdbm/gdbm.c (dbm_type): turn into typed data. Modified files: trunk/ext/gdbm/extconf.rb trunk/ext/gdbm/gdbm.c Index: ext/gdbm/extconf.rb =================================================================== --- ext/gdbm/extconf.rb (revision 47794) +++ ext/gdbm/extconf.rb (revision 47795) @@ -3,5 +3,16 @@ require 'mkmf' https://github.com/ruby/ruby/blob/trunk/ext/gdbm/extconf.rb#L3 dir_config("gdbm") if have_library("gdbm", "gdbm_open") and have_header("gdbm.h") + checking_for("sizeof(DBM) is available") { + if try_compile(<<SRC) +#include <gdbm.h> + +const int sizeof_DBM = (int)sizeof(DBM); +SRC + $defs << '-DDBM_SIZEOF_DBM=sizeof(DBM)' + else + $defs << '-DDBM_SIZEOF_DBM=0' + end + } create_makefile("gdbm") end Index: ext/gdbm/gdbm.c =================================================================== --- ext/gdbm/gdbm.c (revision 47794) +++ ext/gdbm/gdbm.c (revision 47795) @@ -101,7 +101,7 @@ closed_dbm(void) https://github.com/ruby/ruby/blob/trunk/ext/gdbm/gdbm.c#L101 } #define GetDBM(obj, dbmp) do {\ - Data_Get_Struct((obj), struct dbmdata, (dbmp));\ + TypedData_Get_Struct((obj), struct dbmdata, &dbm_type, (dbmp));\ if ((dbmp) == 0) closed_dbm();\ if ((dbmp)->di_dbm == 0) closed_dbm();\ } while (0) @@ -112,14 +112,34 @@ closed_dbm(void) https://github.com/ruby/ruby/blob/trunk/ext/gdbm/gdbm.c#L112 } while (0) static void -free_dbm(struct dbmdata *dbmp) +free_dbm(void *ptr) { + struct dbmdata *dbmp = ptr; if (dbmp) { if (dbmp->di_dbm) gdbm_close(dbmp->di_dbm); 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 += DBM_SIZEOF_DBM; + } + return size; +} + +static const rb_data_type_t dbm_type = { + "gdbm", + {0, free_dbm, memsize_dbm,}, + NULL, NULL, + RUBY_TYPED_FREE_IMMEDIATELY, +}; + /* * call-seq: * gdbm.close -> nil @@ -149,7 +169,7 @@ fgdbm_closed(VALUE obj) https://github.com/ruby/ruby/blob/trunk/ext/gdbm/gdbm.c#L169 { struct dbmdata *dbmp; - Data_Get_Struct(obj, struct dbmdata, dbmp); + TypedData_Get_Struct(obj, struct dbmdata, &dbm_type, dbmp); if (dbmp == 0) return Qtrue; if (dbmp->di_dbm == 0) @@ -161,7 +181,7 @@ fgdbm_closed(VALUE obj) https://github.com/ruby/ruby/blob/trunk/ext/gdbm/gdbm.c#L181 static VALUE fgdbm_s_alloc(VALUE klass) { - return Data_Wrap_Struct(klass, 0, free_dbm, 0); + return TypedData_Wrap_Struct(klass, &dbm_type, 0); } /* @@ -279,7 +299,7 @@ fgdbm_initialize(int argc, VALUE *argv, https://github.com/ruby/ruby/blob/trunk/ext/gdbm/gdbm.c#L299 static VALUE fgdbm_s_open(int argc, VALUE *argv, VALUE klass) { - VALUE obj = Data_Wrap_Struct(klass, 0, free_dbm, 0); + VALUE obj = fgdbm_s_alloc(klass); if (NIL_P(fgdbm_initialize(argc, argv, obj))) { return Qnil; -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/