objspace_dump.c: extract output option utilities
* ext/objspace/objspace_dump.c (dump_output, dump_result): extract output option utility functions from objspace_dump() and objspace_dump_all(). git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@43607 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
a50bbcfecb
commit
90feeb6ab9
@ -290,6 +290,46 @@ root_obj_i(const char *category, VALUE obj, void *data)
|
|||||||
dc->roots++;
|
dc->roots++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static VALUE
|
||||||
|
dump_output(struct dump_config *dc, VALUE opts, VALUE output, char *filename)
|
||||||
|
{
|
||||||
|
if (RTEST(opts))
|
||||||
|
output = rb_hash_aref(opts, sym_output);
|
||||||
|
|
||||||
|
if (output == sym_stdout) {
|
||||||
|
dc->stream = stdout;
|
||||||
|
dc->string = Qnil;
|
||||||
|
}
|
||||||
|
else if (output == sym_file) {
|
||||||
|
int fd = mkstemp(filename);
|
||||||
|
dc->string = rb_filesystem_str_new_cstr(filename);
|
||||||
|
if (fd == -1) rb_sys_fail_path(dc->string);
|
||||||
|
dc->stream = fdopen(fd, "w");
|
||||||
|
}
|
||||||
|
else if (output == sym_string) {
|
||||||
|
dc->string = rb_str_new_cstr("");
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
rb_raise(rb_eArgError, "wrong output option: %"PRIsVALUE, output);
|
||||||
|
}
|
||||||
|
return output;
|
||||||
|
}
|
||||||
|
|
||||||
|
static VALUE
|
||||||
|
dump_result(struct dump_config *dc, VALUE output)
|
||||||
|
{
|
||||||
|
if (output == sym_string) {
|
||||||
|
return dc->string;
|
||||||
|
}
|
||||||
|
else if (output == sym_file) {
|
||||||
|
fclose(dc->stream);
|
||||||
|
return dc->string;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return Qnil;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* call-seq:
|
* call-seq:
|
||||||
* ObjectSpace.dump(obj[, output: :string]) # => "{ ... }"
|
* ObjectSpace.dump(obj[, output: :string]) # => "{ ... }"
|
||||||
@ -307,38 +347,17 @@ root_obj_i(const char *category, VALUE obj, void *data)
|
|||||||
static VALUE
|
static VALUE
|
||||||
objspace_dump(int argc, VALUE *argv, VALUE os)
|
objspace_dump(int argc, VALUE *argv, VALUE os)
|
||||||
{
|
{
|
||||||
int fd;
|
|
||||||
char filename[] = "/tmp/rubyobjXXXXXX";
|
char filename[] = "/tmp/rubyobjXXXXXX";
|
||||||
VALUE obj = Qnil, opts = Qnil, output;
|
VALUE obj = Qnil, opts = Qnil, output;
|
||||||
struct dump_config dc = {0,};
|
struct dump_config dc = {0,};
|
||||||
|
|
||||||
rb_scan_args(argc, argv, "1:", &obj, &opts);
|
rb_scan_args(argc, argv, "1:", &obj, &opts);
|
||||||
|
|
||||||
if (RTEST(opts))
|
output = dump_output(&dc, opts, sym_string, filename);
|
||||||
output = rb_hash_aref(opts, sym_output);
|
|
||||||
|
|
||||||
if (output == sym_stdout)
|
|
||||||
dc.stream = stdout;
|
|
||||||
else if (output == sym_file) {
|
|
||||||
fd = mkstemp(filename);
|
|
||||||
if (fd == -1) rb_sys_fail_path(rb_str_new_cstr(filename));
|
|
||||||
dc.stream = fdopen(fd, "w");
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
output = sym_string;
|
|
||||||
dc.string = rb_str_new2("");
|
|
||||||
}
|
|
||||||
|
|
||||||
dump_object(obj, &dc);
|
dump_object(obj, &dc);
|
||||||
|
|
||||||
if (output == sym_string)
|
return dump_result(&dc, output);
|
||||||
return dc.string;
|
|
||||||
else if (output == sym_file) {
|
|
||||||
fclose(dc.stream);
|
|
||||||
return rb_str_new2(filename);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
return Qnil;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -358,26 +377,13 @@ objspace_dump(int argc, VALUE *argv, VALUE os)
|
|||||||
static VALUE
|
static VALUE
|
||||||
objspace_dump_all(int argc, VALUE *argv, VALUE os)
|
objspace_dump_all(int argc, VALUE *argv, VALUE os)
|
||||||
{
|
{
|
||||||
int fd;
|
|
||||||
char filename[] = "/tmp/rubyheapXXXXXX";
|
char filename[] = "/tmp/rubyheapXXXXXX";
|
||||||
VALUE opts = Qnil, output;
|
VALUE opts = Qnil, output;
|
||||||
struct dump_config dc = {0,};
|
struct dump_config dc = {0,};
|
||||||
|
|
||||||
rb_scan_args(argc, argv, "0:", &opts);
|
rb_scan_args(argc, argv, "0:", &opts);
|
||||||
|
|
||||||
if (RTEST(opts))
|
output = dump_output(&dc, opts, sym_file, filename);
|
||||||
output = rb_hash_aref(opts, sym_output);
|
|
||||||
|
|
||||||
if (output == sym_string)
|
|
||||||
dc.string = rb_str_new2("");
|
|
||||||
else if (output == sym_stdout)
|
|
||||||
dc.stream = stdout;
|
|
||||||
else {
|
|
||||||
output = sym_file;
|
|
||||||
fd = mkstemp(filename);
|
|
||||||
if (fd == -1) rb_sys_fail_path(rb_str_new_cstr(filename));
|
|
||||||
dc.stream = fdopen(fd, "w");
|
|
||||||
}
|
|
||||||
|
|
||||||
/* dump roots */
|
/* dump roots */
|
||||||
rb_objspace_reachable_objects_from_root(root_obj_i, &dc);
|
rb_objspace_reachable_objects_from_root(root_obj_i, &dc);
|
||||||
@ -386,14 +392,7 @@ objspace_dump_all(int argc, VALUE *argv, VALUE os)
|
|||||||
/* dump all objects */
|
/* dump all objects */
|
||||||
rb_objspace_each_objects(heap_i, &dc);
|
rb_objspace_each_objects(heap_i, &dc);
|
||||||
|
|
||||||
if (output == sym_string)
|
return dump_result(&dc, output);
|
||||||
return dc.string;
|
|
||||||
else if (output == sym_file) {
|
|
||||||
fclose(dc.stream);
|
|
||||||
return rb_str_new2(filename);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
return Qnil;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
|
Loading…
x
Reference in New Issue
Block a user