From 10f81512d85b064579456187d98c5daf5a08d4d2 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Fri, 19 Jun 2015 03:14:55 +0900 Subject: [PATCH] don't malloc pic_reader --- extlib/benz/include/picrin.h | 5 ++-- extlib/benz/include/picrin/read.h | 8 +++--- extlib/benz/read.c | 47 ++++++++++++++----------------- extlib/benz/state.c | 4 +-- 4 files changed, 29 insertions(+), 35 deletions(-) diff --git a/extlib/benz/include/picrin.h b/extlib/benz/include/picrin.h index fcafe4c2..a95a6935 100644 --- a/extlib/benz/include/picrin.h +++ b/extlib/benz/include/picrin.h @@ -49,6 +49,7 @@ typedef struct pic_state pic_state; #include "picrin/irep.h" #include "picrin/file.h" +#include "picrin/read.h" typedef struct pic_jmpbuf { PIC_JMPBUF buf; @@ -137,7 +138,7 @@ struct pic_state { pic_value libs; struct pic_reg *attrs; - struct pic_reader *reader; + pic_reader reader; xFILE files[XOPEN_MAX]; pic_code iseq[2]; /* for pic_apply_trampoline */ @@ -277,11 +278,9 @@ pic_value pic_fdisplay(pic_state *, pic_value, xFILE *); #include "picrin/pair.h" #include "picrin/port.h" #include "picrin/proc.h" -#include "picrin/read.h" #include "picrin/record.h" #include "picrin/string.h" #include "picrin/symbol.h" -#include "picrin/read.h" #include "picrin/vector.h" #include "picrin/reg.h" diff --git a/extlib/benz/include/picrin/read.h b/extlib/benz/include/picrin/read.h index a3f01100..d9b0bb6e 100644 --- a/extlib/benz/include/picrin/read.h +++ b/extlib/benz/include/picrin/read.h @@ -11,7 +11,7 @@ extern "C" { typedef pic_value (*pic_reader_t)(pic_state *, struct pic_port *port, int c); -struct pic_reader { +typedef struct { enum pic_typecase { PIC_CASE_DEFAULT, PIC_CASE_FOLD @@ -19,10 +19,10 @@ struct pic_reader { xhash labels; pic_reader_t table[256]; pic_reader_t dispatch[256]; -}; +} pic_reader; -struct pic_reader *pic_reader_open(pic_state *); -void pic_reader_close(pic_state *, struct pic_reader *); +void pic_reader_init(pic_state *); +void pic_reader_destroy(pic_state *); #if defined(__cplusplus) } diff --git a/extlib/benz/read.c b/extlib/benz/read.c index 45325ebf..bcadecaa 100644 --- a/extlib/benz/read.c +++ b/extlib/benz/read.c @@ -79,7 +79,7 @@ strcaseeq(const char *s1, const char *s2) static int case_fold(pic_state *pic, int c) { - if (pic->reader->typecase == PIC_CASE_FOLD) { + if (pic->reader.typecase == PIC_CASE_FOLD) { c = tolower(c); } return c; @@ -131,13 +131,13 @@ read_directive(pic_state *pic, struct pic_port *port, int c) switch (peek(pic, port)) { case 'n': if (expect(pic, port, "no-fold-case")) { - pic->reader->typecase = PIC_CASE_DEFAULT; + pic->reader.typecase = PIC_CASE_DEFAULT; return pic_invalid_value(); } break; case 'f': if (expect(pic, port, "fold-case")) { - pic->reader->typecase = PIC_CASE_FOLD; + pic->reader.typecase = PIC_CASE_FOLD; return pic_invalid_value(); } break; @@ -649,7 +649,7 @@ read_label_set(pic_state *pic, struct pic_port *port, int i) val = pic_cons(pic, pic_undef_value(), pic_undef_value()); - xh_put_int(&pic->reader->labels, i, &val); + xh_put_int(&pic->reader.labels, i, &val); tmp = read(pic, port, c); pic_pair_ptr(val)->car = pic_car(pic, tmp); @@ -672,7 +672,7 @@ read_label_set(pic_state *pic, struct pic_port *port, int i) val = pic_obj_value(pic_make_vec(pic, 0)); - xh_put_int(&pic->reader->labels, i, &val); + xh_put_int(&pic->reader.labels, i, &val); tmp = pic_vec_ptr(read(pic, port, c)); PIC_SWAP(pic_value *, tmp->data, pic_vec_ptr(val)->data); @@ -687,7 +687,7 @@ read_label_set(pic_state *pic, struct pic_port *port, int i) { val = read(pic, port, c); - xh_put_int(&pic->reader->labels, i, &val); + xh_put_int(&pic->reader.labels, i, &val); return val; } @@ -699,7 +699,7 @@ read_label_ref(pic_state *pic, struct pic_port PIC_UNUSED(*port), int i) { xh_entry *e; - e = xh_get_int(&pic->reader->labels, i); + e = xh_get_int(&pic->reader.labels, i); if (! e) { read_error(pic, "label of given index not defined"); } @@ -740,11 +740,11 @@ read_dispatch(pic_state *pic, struct pic_port *port, int c) read_error(pic, "unexpected EOF"); } - if (pic->reader->dispatch[c] == NULL) { + if (pic->reader.dispatch[c] == NULL) { read_error(pic, "invalid character at the seeker head"); } - return pic->reader->dispatch[c](pic, port, c); + return pic->reader.dispatch[c](pic, port, c); } static pic_value @@ -756,11 +756,11 @@ read_nullable(pic_state *pic, struct pic_port *port, int c) read_error(pic, "unexpected EOF"); } - if (pic->reader->table[c] == NULL) { + if (pic->reader.table[c] == NULL) { read_error(pic, "invalid character at the seeker head"); } - return pic->reader->table[c](pic, port, c); + return pic->reader.table[c](pic, port, c); } static pic_value @@ -780,7 +780,7 @@ read(pic_state *pic, struct pic_port *port, int c) } static void -reader_table_init(struct pic_reader *reader) +reader_table_init(pic_reader *reader) { int c; @@ -826,34 +826,29 @@ reader_table_init(struct pic_reader *reader) } } -struct pic_reader * -pic_reader_open(pic_state *pic) +void +pic_reader_init(pic_state *pic) { - struct pic_reader *reader; int c; - reader = pic_malloc(pic, sizeof(struct pic_reader)); - reader->typecase = PIC_CASE_DEFAULT; - xh_init_int(&reader->labels, sizeof(pic_value)); + pic->reader.typecase = PIC_CASE_DEFAULT; + xh_init_int(&pic->reader.labels, sizeof(pic_value)); for (c = 0; c < 256; ++c) { - reader->table[c] = NULL; + pic->reader.table[c] = NULL; } for (c = 0; c < 256; ++c) { - reader->dispatch[c] = NULL; + pic->reader.dispatch[c] = NULL; } - reader_table_init(reader); - - return reader; + reader_table_init(&pic->reader); } void -pic_reader_close(pic_state *pic, struct pic_reader *reader) +pic_reader_destroy(pic_state *pic) { - xh_destroy(&reader->labels); - pic_free(pic, reader); + xh_destroy(&pic->reader.labels); } pic_value diff --git a/extlib/benz/state.c b/extlib/benz/state.c index 3f6f5ba9..6c9545fb 100644 --- a/extlib/benz/state.c +++ b/extlib/benz/state.c @@ -364,7 +364,7 @@ pic_open(int argc, char *argv[], char **envp, pic_allocf allocf) pic->cp->in = pic->cp->out = NULL; /* reader */ - pic->reader = pic_reader_open(pic); + pic_reader_init(pic); /* parameter table */ pic->ptable = pic_cons(pic, pic_obj_value(pic_make_dict(pic)), pic->ptable); @@ -440,7 +440,7 @@ pic_close(pic_state *pic) pic_heap_close(pic, pic->heap); /* free reader struct */ - pic_reader_close(pic, pic->reader); + pic_reader_destroy(pic); /* free runtime context */ allocf(pic->stbase, 0);