mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
* dir.c (dir_data_type): typed.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@24804 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
af4a989a98
commit
44110c88e7
2 changed files with 27 additions and 13 deletions
|
@ -1,3 +1,7 @@
|
|||
Wed Sep 9 11:28:19 2009 Nobuyoshi Nakada <nobu@ruby-lang.org>
|
||||
|
||||
* dir.c (dir_data_type): typed.
|
||||
|
||||
Wed Sep 9 11:11:33 2009 Nobuyoshi Nakada <nobu@ruby-lang.org>
|
||||
|
||||
* include/ruby/ruby.h (rb_data_type_struct): constified dsize.
|
||||
|
|
36
dir.c
36
dir.c
|
@ -310,27 +310,40 @@ struct dir_data {
|
|||
};
|
||||
|
||||
static void
|
||||
mark_dir(struct dir_data *dir)
|
||||
dir_mark(void *ptr)
|
||||
{
|
||||
struct dir_data *dir = ptr;
|
||||
rb_gc_mark(dir->path);
|
||||
}
|
||||
|
||||
static void
|
||||
free_dir(struct dir_data *dir)
|
||||
dir_free(void *ptr)
|
||||
{
|
||||
struct dir_data *dir = ptr;
|
||||
if (dir) {
|
||||
if (dir->dir) closedir(dir->dir);
|
||||
}
|
||||
xfree(dir);
|
||||
}
|
||||
|
||||
static size_t
|
||||
dir_memsize(const void *ptr)
|
||||
{
|
||||
return ptr ? sizeof(struct dir_data) : 0;
|
||||
}
|
||||
|
||||
static const rb_data_type_t dir_data_type = {
|
||||
"dir",
|
||||
dir_mark, dir_free, dir_memsize
|
||||
};
|
||||
|
||||
static VALUE dir_close(VALUE);
|
||||
|
||||
static VALUE
|
||||
dir_s_alloc(VALUE klass)
|
||||
{
|
||||
struct dir_data *dirp;
|
||||
VALUE obj = Data_Make_Struct(klass, struct dir_data, mark_dir, free_dir, dirp);
|
||||
VALUE obj = TypedData_Make_Struct(klass, struct dir_data, &dir_data_type, dirp);
|
||||
|
||||
dirp->dir = NULL;
|
||||
dirp->path = Qnil;
|
||||
|
@ -374,7 +387,7 @@ dir_initialize(int argc, VALUE *argv, VALUE dir)
|
|||
|
||||
FilePathValue(dirname);
|
||||
|
||||
Data_Get_Struct(dir, struct dir_data, dp);
|
||||
TypedData_Get_Struct(dir, struct dir_data, &dir_data_type, dp);
|
||||
if (dp->dir) closedir(dp->dir);
|
||||
dp->dir = NULL;
|
||||
dp->path = Qnil;
|
||||
|
@ -409,7 +422,7 @@ static VALUE
|
|||
dir_s_open(int argc, VALUE *argv, VALUE klass)
|
||||
{
|
||||
struct dir_data *dp;
|
||||
VALUE dir = Data_Make_Struct(klass, struct dir_data, mark_dir, free_dir, dp);
|
||||
VALUE dir = TypedData_Make_Struct(klass, struct dir_data, &dir_data_type, dp);
|
||||
|
||||
dir_initialize(argc, argv, dir);
|
||||
if (rb_block_given_p()) {
|
||||
|
@ -435,7 +448,7 @@ dir_check(VALUE dir)
|
|||
|
||||
#define GetDIR(obj, dirp) do {\
|
||||
dir_check(dir);\
|
||||
Data_Get_Struct(obj, struct dir_data, dirp);\
|
||||
TypedData_Get_Struct(obj, struct dir_data, &dir_data_type, dirp); \
|
||||
if (dirp->dir == NULL) dir_closed();\
|
||||
} while (0)
|
||||
|
||||
|
@ -451,7 +464,7 @@ dir_inspect(VALUE dir)
|
|||
{
|
||||
struct dir_data *dirp;
|
||||
|
||||
Data_Get_Struct(dir, struct dir_data, dirp);
|
||||
TypedData_Get_Struct(dir, struct dir_data, &dir_data_type, dirp);
|
||||
if (!NIL_P(dirp->path)) {
|
||||
const char *c = rb_obj_classname(dir);
|
||||
return rb_sprintf("#<%s:%s>", c, RSTRING_PTR(dirp->path));
|
||||
|
@ -473,7 +486,7 @@ dir_path(VALUE dir)
|
|||
{
|
||||
struct dir_data *dirp;
|
||||
|
||||
Data_Get_Struct(dir, struct dir_data, dirp);
|
||||
TypedData_Get_Struct(dir, struct dir_data, &dir_data_type, dirp);
|
||||
if (NIL_P(dirp->path)) return Qnil;
|
||||
return rb_str_dup(dirp->path);
|
||||
}
|
||||
|
@ -1758,12 +1771,9 @@ static VALUE
|
|||
dir_open_dir(int argc, VALUE *argv)
|
||||
{
|
||||
VALUE dir = rb_funcall2(rb_cDir, rb_intern("open"), argc, argv);
|
||||
struct dir_data *dirp;
|
||||
|
||||
if (TYPE(dir) != T_DATA ||
|
||||
RDATA(dir)->dfree != (RUBY_DATA_FUNC)free_dir) {
|
||||
rb_raise(rb_eTypeError, "wrong argument type %s (expected Dir)",
|
||||
rb_obj_classname(dir));
|
||||
}
|
||||
TypedData_Get_Struct(dir, struct dir_data, &dir_data_type, dirp);
|
||||
return dir;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue