зеркало из https://github.com/github/ruby.git
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
This commit is contained in:
Родитель
ad48d99876
Коммит
20904659c3
|
@ -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
|
||||
|
|
|
@ -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;
|
||||
|
|
Загрузка…
Ссылка в новой задаче