summaryrefslogtreecommitdiff
path: root/ext
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2014-10-04 23:33:01 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2014-10-04 23:33:01 +0000
commitad48d998763c7d58ea7a90b00d280125659a74d1 (patch)
treedcb99f52515e3dae1ab82c41b21fc40a1f3c6f69 /ext
parentba0655ebdd31e5e6b9ae8ede328733067a68dcbc (diff)
dbm: typed data
* ext/dbm/dbm.c (dbm_type): turn into typed data. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@47794 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'ext')
-rw-r--r--ext/dbm/dbm.c30
-rw-r--r--ext/dbm/extconf.rb17
2 files changed, 42 insertions, 5 deletions
diff --git a/ext/dbm/dbm.c b/ext/dbm/dbm.c
index 527d651..6f64d13 100644
--- a/ext/dbm/dbm.c
+++ b/ext/dbm/dbm.c
@@ -46,7 +46,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)
@@ -57,14 +57,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) dbm_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 = {
+ "dbm",
+ {0, free_dbm, memsize_dbm,},
+ NULL, NULL,
+ RUBY_TYPED_FREE_IMMEDIATELY,
+};
+
/*
* call-seq:
* dbm.close
@@ -94,7 +114,7 @@ fdbm_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)
@@ -106,7 +126,7 @@ fdbm_closed(VALUE obj)
static VALUE
fdbm_alloc(VALUE klass)
{
- return Data_Wrap_Struct(klass, 0, free_dbm, 0);
+ return TypedData_Wrap_Struct(klass, &dbm_type, 0);
}
/*
@@ -228,7 +248,7 @@ fdbm_initialize(int argc, VALUE *argv, VALUE obj)
static VALUE
fdbm_s_open(int argc, VALUE *argv, VALUE klass)
{
- VALUE obj = Data_Wrap_Struct(klass, 0, free_dbm, 0);
+ VALUE obj = fdbm_alloc(klass);
if (NIL_P(fdbm_initialize(argc, argv, obj))) {
return Qnil;
diff --git a/ext/dbm/extconf.rb b/ext/dbm/extconf.rb
index 68070c9..2c8caa3 100644
--- a/ext/dbm/extconf.rb
+++ b/ext/dbm/extconf.rb
@@ -267,6 +267,23 @@ if dblib.any? {|db| headers.fetch(db, ["ndbm.h"]).any? {|hdr| headers.db_check(d
have_func("dbm_pagfno((DBM *)0)", headers.found, headers.defs)
have_func("dbm_dirfno((DBM *)0)", headers.found, headers.defs)
convertible_int("datum.dsize", headers.found, headers.defs)
+ checking_for("sizeof(DBM) is available") {
+ if try_compile(<<SRC)
+#ifdef HAVE_CDEFS_H
+# include <cdefs.h>
+#endif
+#ifdef HAVE_SYS_CDEFS_H
+# include <sys/cdefs.h>
+#endif
+#include DBM_HDR
+
+const int sizeof_DBM = (int)sizeof(DBM);
+SRC
+ $defs << '-DDBM_SIZEOF_DBM=sizeof(DBM)'
+ else
+ $defs << '-DDBM_SIZEOF_DBM=0'
+ end
+ }
create_makefile("dbm")
end
# :startdoc: