refactor write. don't run unnecessary alloc

This commit is contained in:
Yuichi Nishiwaki 2014-07-27 12:36:02 +09:00
parent 300f87d569
commit 2caefd0c39
1 changed files with 21 additions and 28 deletions

View File

@ -56,29 +56,22 @@ struct writer_control {
#define WRITE_MODE 1 #define WRITE_MODE 1
#define DISPLAY_MODE 2 #define DISPLAY_MODE 2
static struct writer_control * static void
writer_control_new(pic_state *pic, xFILE *file, int mode) writer_control_init(struct writer_control *p, pic_state *pic, xFILE *file, int mode)
{ {
struct writer_control *p;
p = (struct writer_control *)pic_alloc(pic, sizeof(struct writer_control));
p->pic = pic; p->pic = pic;
p->file = file; p->file = file;
p->mode = mode; p->mode = mode;
p->cnt = 0; p->cnt = 0;
xh_init_ptr(&p->labels, sizeof(int)); xh_init_ptr(&p->labels, sizeof(int));
xh_init_ptr(&p->visited, sizeof(int)); xh_init_ptr(&p->visited, sizeof(int));
return p;
} }
static void static void
writer_control_destroy(struct writer_control *p) writer_control_destroy(struct writer_control *p)
{ {
pic_state *pic = p->pic;
xh_destroy(&p->labels); xh_destroy(&p->labels);
xh_destroy(&p->visited); xh_destroy(&p->visited);
pic_free(pic, p);
} }
static void static void
@ -321,57 +314,57 @@ write_core(struct writer_control *p, pic_value obj)
static void static void
write(pic_state *pic, pic_value obj, xFILE *file) write(pic_state *pic, pic_value obj, xFILE *file)
{ {
struct writer_control *p; struct writer_control p;
p = writer_control_new(pic, file, WRITE_MODE); writer_control_init(&p, pic, file, WRITE_MODE);
traverse_shared(p, obj); /* FIXME */ traverse_shared(&p, obj); /* FIXME */
write_core(p, obj); write_core(&p, obj);
writer_control_destroy(p); writer_control_destroy(&p);
} }
static void static void
write_simple(pic_state *pic, pic_value obj, xFILE *file) write_simple(pic_state *pic, pic_value obj, xFILE *file)
{ {
struct writer_control *p; struct writer_control p;
p = writer_control_new(pic, file, WRITE_MODE); writer_control_init(&p, pic, file, WRITE_MODE);
/* no traverse here! */ /* no traverse here! */
write_core(p, obj); write_core(&p, obj);
writer_control_destroy(p); writer_control_destroy(&p);
} }
static void static void
write_shared(pic_state *pic, pic_value obj, xFILE *file) write_shared(pic_state *pic, pic_value obj, xFILE *file)
{ {
struct writer_control *p; struct writer_control p;
p = writer_control_new(pic, file, WRITE_MODE); writer_control_init(&p, pic, file, WRITE_MODE);
traverse_shared(p, obj); traverse_shared(&p, obj);
write_core(p, obj); write_core(&p, obj);
writer_control_destroy(p); writer_control_destroy(&p);
} }
static void static void
display(pic_state *pic, pic_value obj, xFILE *file) display(pic_state *pic, pic_value obj, xFILE *file)
{ {
struct writer_control *p; struct writer_control p;
p = writer_control_new(pic, file, DISPLAY_MODE); writer_control_init(&p, pic, file, DISPLAY_MODE);
traverse_shared(p, obj); /* FIXME */ traverse_shared(&p, obj); /* FIXME */
write_core(p, obj); write_core(&p, obj);
writer_control_destroy(p); writer_control_destroy(&p);
} }
pic_value pic_value