Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 43fe223

Browse files
committed
Add options to dump T_NONE objects and add page numbers
1 parent f9ea965 commit 43fe223

File tree

1 file changed

+36
-2
lines changed

1 file changed

+36
-2
lines changed

ext/objspace/objspace_dump.c

+36-2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "objspace.h"
2222

2323
static VALUE sym_output, sym_stdout, sym_string, sym_file;
24+
static VALUE sym_include_pages, sym_include_none;
2425

2526
struct dump_config {
2627
VALUE type;
@@ -31,6 +32,9 @@ struct dump_config {
3132
VALUE cur_obj;
3233
VALUE cur_obj_klass;
3334
size_t cur_obj_references;
35+
int include_pages;
36+
int include_none;
37+
int pages_seen;
3438
};
3539

3640
PRINTF_ARGS(static void dump_append(struct dump_config *, const char *, ...), 2, 3);
@@ -190,6 +194,18 @@ dump_append_string_content(struct dump_config *dc, VALUE obj)
190194
}
191195
}
192196

197+
static void
198+
dump_empty(VALUE obj, struct dump_config *dc)
199+
{
200+
dump_append(dc, "{\"address\":\"%p\", ", (void *)obj);
201+
dump_append(dc, "\"type\":\"NONE\"");
202+
203+
if (dc->include_pages)
204+
dump_append(dc, ", \"page_number\":%d", dc->pages_seen);
205+
dump_append(dc, "}\n");
206+
return;
207+
}
208+
193209
static void
194210
dump_object(VALUE obj, struct dump_config *dc)
195211
{
@@ -215,6 +231,8 @@ dump_object(VALUE obj, struct dump_config *dc)
215231

216232
if (dc->cur_obj_klass)
217233
dump_append(dc, ", \"class\":\"%p\"", (void *)dc->cur_obj_klass);
234+
if (dc->include_pages)
235+
dump_append(dc, ", \"page_number\":%d", dc->pages_seen);
218236
if (rb_obj_frozen_p(obj))
219237
dump_append(dc, ", \"frozen\":true");
220238

@@ -321,8 +339,11 @@ heap_i(void *vstart, void *vend, size_t stride, void *data)
321339
VALUE v = (VALUE)vstart;
322340
for (; v != (VALUE)vend; v += stride) {
323341
if (RBASIC(v)->flags)
324-
dump_object(v, data);
342+
dump_object(v, dc);
343+
else if (dc->include_none && T_NONE == BUILTIN_TYPE(v))
344+
dump_empty(v, dc);
325345
}
346+
dc->pages_seen++;
326347
return 0;
327348
}
328349

@@ -347,9 +368,20 @@ dump_output(struct dump_config *dc, VALUE opts, VALUE output, const char *filena
347368
{
348369
VALUE tmp;
349370

350-
if (RTEST(opts))
371+
dc->pages_seen = 0;
372+
dc->include_pages = 0;
373+
dc->include_none = 0;
374+
375+
if (RTEST(opts)) {
351376
output = rb_hash_aref(opts, sym_output);
352377

378+
if (Qtrue == rb_hash_lookup2(opts, sym_include_pages, Qfalse))
379+
dc->include_pages = 1;
380+
381+
if (Qtrue == rb_hash_lookup2(opts, sym_include_none, Qfalse))
382+
dc->include_none = 1;
383+
}
384+
353385
if (output == sym_stdout) {
354386
dc->stream = stdout;
355387
dc->string = Qnil;
@@ -474,6 +506,8 @@ Init_objspace_dump(VALUE rb_mObjSpace)
474506
sym_stdout = ID2SYM(rb_intern("stdout"));
475507
sym_string = ID2SYM(rb_intern("string"));
476508
sym_file = ID2SYM(rb_intern("file"));
509+
sym_include_pages = ID2SYM(rb_intern("include_pages"));
510+
sym_include_none = ID2SYM(rb_intern("include_none"));
477511

478512
/* force create static IDs */
479513
rb_obj_gc_flags(rb_mObjSpace, 0, 0);

0 commit comments

Comments
 (0)