summaryrefslogtreecommitdiff
path: root/ext
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2014-10-04 23:33:04 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2014-10-04 23:33:04 +0000
commit20904659c3e8911e222a6d86cdddc114e75ca4d7 (patch)
treebeae9f767073c1860117c6311741b2076f179305 /ext
parentad48d998763c7d58ea7a90b00d280125659a74d1 (diff)
gdbm: typed data
* ext/gdbm/gdbm.c (dbm_type): turn into typed data. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@47795 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'ext')
-rw-r--r--ext/gdbm/extconf.rb11
-rw-r--r--ext/gdbm/gdbm.c30
2 files changed, 36 insertions, 5 deletions
diff --git a/ext/gdbm/extconf.rb b/ext/gdbm/extconf.rb
index 5a09492..389bfb6 100644
--- a/ext/gdbm/extconf.rb
+++ b/ext/gdbm/extconf.rb
@@ -3,5 +3,16 @@ require 'mkmf'
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
diff --git a/ext/gdbm/gdbm.c b/ext/gdbm/gdbm.c
index 851afea..a8f75b2 100644
--- a/ext/gdbm/gdbm.c
+++ b/ext/gdbm/gdbm.c
@@ -101,7 +101,7 @@ closed_dbm(void)
}
#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)
} 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)
{
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)
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, VALUE obj)
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;