diff options
| -rw-r--r-- | include/mrbconf.h | 3 | ||||
| -rw-r--r-- | include/mruby/irep.h | 2 | ||||
| -rw-r--r-- | src/codegen.c | 2 | ||||
| -rw-r--r-- | src/dump.c | 72 | ||||
| -rw-r--r-- | src/load.c | 70 |
5 files changed, 31 insertions, 118 deletions
diff --git a/include/mrbconf.h b/include/mrbconf.h index bd8f6ea80..d8dca7d61 100644 --- a/include/mrbconf.h +++ b/include/mrbconf.h @@ -83,13 +83,11 @@ # define PRIoMRB_INT PRIo64 # define PRIxMRB_INT PRIx64 # define PRIXMRB_INT PRIX64 -# define str_to_mrb_int(buf) strtoll(buf, NULL, 10) # endif #elif defined(MRB_INT16) typedef int16_t mrb_int; # define MRB_INT_MIN INT16_MIN # define MRB_INT_MAX INT16_MAX -# define str_to_mrb_int(buf) strtol(buf, NULL, 10) #else typedef int32_t mrb_int; # define MRB_INT_MIN INT32_MIN @@ -99,7 +97,6 @@ # define PRIoMRB_INT PRIo32 # define PRIxMRB_INT PRIx32 # define PRIXMRB_INT PRIX32 -# define str_to_mrb_int(buf) strtol(buf, NULL, 10) #endif typedef short mrb_sym; diff --git a/include/mruby/irep.h b/include/mruby/irep.h index 982ab4ac2..0305b9e0b 100644 --- a/include/mruby/irep.h +++ b/include/mruby/irep.h @@ -23,7 +23,7 @@ typedef struct mrb_irep { /* debug info */ const char *filename; - short *lines; + uint16_t *lines; size_t ilen, plen, slen; } mrb_irep; diff --git a/src/codegen.c b/src/codegen.c index cff3b3ce4..e5b1802a1 100644 --- a/src/codegen.c +++ b/src/codegen.c @@ -2401,7 +2401,7 @@ scope_finish(codegen_scope *s) irep->iseq = (mrb_code *)codegen_realloc(s, s->iseq, sizeof(mrb_code)*s->pc); irep->ilen = s->pc; if (s->lines) { - irep->lines = (short *)codegen_realloc(s, s->lines, sizeof(short)*s->pc); + irep->lines = (uint16_t *)codegen_realloc(s, s->lines, sizeof(uint16_t)*s->pc); } else { irep->lines = 0; diff --git a/src/dump.c b/src/dump.c index f4c48d951..3000f2bcf 100644 --- a/src/dump.c +++ b/src/dump.c @@ -107,19 +107,12 @@ get_pool_block_size(mrb_state *mrb, mrb_irep *irep) static int write_pool_block(mrb_state *mrb, mrb_irep *irep, uint8_t *buf) { - int result; size_t pool_no; uint8_t *cur = buf; - size_t buf_size, len; + size_t len; mrb_value str; - char *char_buf = NULL; - - buf_size = MRB_DUMP_DEFAULT_STR_LEN; - char_buf = (char *)mrb_malloc(mrb, buf_size); - if (char_buf == NULL) { - result = MRB_DUMP_GENERAL_FAILURE; - goto error_exit; - } + const char *char_ptr; + char char_buf[30]; cur += uint32_to_bin(irep->plen, cur); /* number of pool */ @@ -127,51 +120,37 @@ write_pool_block(mrb_state *mrb, mrb_irep *irep, uint8_t *buf) int ai = mrb_gc_arena_save(mrb); cur += uint8_to_bin(mrb_type(irep->pool[pool_no]), cur); /* data type */ - memset(char_buf, 0, buf_size); switch (mrb_type(irep->pool[pool_no])) { case MRB_TT_FIXNUM: str = mrb_fix2str(mrb, irep->pool[pool_no], 10); - memcpy(char_buf, RSTRING_PTR(str), RSTRING_LEN(str)); + char_ptr = RSTRING_PTR(str); len = RSTRING_LEN(str); break; case MRB_TT_FLOAT: len = mrb_float_to_str(char_buf, mrb_float(irep->pool[pool_no])); + char_ptr = &char_buf[0]; break; case MRB_TT_STRING: str = irep->pool[pool_no]; + char_ptr = RSTRING_PTR(str); len = RSTRING_LEN(str); - if (len > buf_size - 1) { - buf_size = len + 1; - char_buf = (char *)mrb_realloc(mrb, char_buf, buf_size); - if (char_buf == NULL) { - mrb_gc_arena_restore(mrb, ai); - result = MRB_DUMP_GENERAL_FAILURE; - goto error_exit; - } - memset(char_buf, 0, buf_size); - } - memcpy(char_buf, RSTRING_PTR(str), RSTRING_LEN(str)); break; default: - len = 0; continue; } cur += uint16_to_bin(len, cur); /* data length */ - memcpy(cur, char_buf, len); + memcpy(cur, char_ptr, len); cur += len; mrb_gc_arena_restore(mrb, ai); } - result = (int)(cur - buf); -error_exit: - mrb_free(mrb, char_buf); - return result; + return (int)(cur - buf); } @@ -197,21 +176,10 @@ get_syms_block_size(mrb_state *mrb, mrb_irep *irep) static int write_syms_block(mrb_state *mrb, mrb_irep *irep, uint8_t *buf) { - int result; size_t sym_no; - size_t buf_size; uint8_t *cur = buf; - uint16_t nlen; - char *char_buf = NULL; const char *name; - buf_size = MRB_DUMP_DEFAULT_STR_LEN; - char_buf = (char *)mrb_malloc(mrb, buf_size); - if (char_buf == NULL) { - result = MRB_DUMP_GENERAL_FAILURE; - goto error_exit; - } - cur += uint32_to_bin(irep->slen, cur); /* number of symbol */ for (sym_no = 0; sym_no < irep->slen; sym_no++) { @@ -219,32 +187,20 @@ write_syms_block(mrb_state *mrb, mrb_irep *irep, uint8_t *buf) size_t len; name = mrb_sym2name_len(mrb, irep->syms[sym_no], &len); - if (len > UINT16_MAX) goto error_exit; - nlen = (uint16_t)len; - if (nlen > buf_size - 1) { - buf_size = nlen + 1; - char_buf = (char *)mrb_realloc(mrb, char_buf, buf_size); - if (char_buf == NULL) { - result = MRB_DUMP_GENERAL_FAILURE; - goto error_exit; - } + if (len > UINT16_MAX) { + return MRB_DUMP_GENERAL_FAILURE; } - memset(char_buf, 0, buf_size); - memcpy(char_buf, name, len); - cur += uint16_to_bin(nlen, cur); /* length of symbol name */ - memcpy(cur, char_buf, nlen); /* symbol name */ - cur += nlen; + cur += uint16_to_bin((uint16_t)len, cur); /* length of symbol name */ + memcpy(cur, name, len); /* symbol name */ + cur += (uint16_t)len; } else { cur += uint16_to_bin(MRB_DUMP_NULL_SYM_LEN, cur); /* length of symbol name */ } } - result = (int)(cur - buf); -error_exit: - mrb_free(mrb, char_buf); - return result; + return (int)(cur - buf); } diff --git a/src/load.c b/src/load.c index 4a9947e75..a2f685742 100644 --- a/src/load.c +++ b/src/load.c @@ -17,22 +17,16 @@ #include "mruby/proc.h" #include "mruby/string.h" -#ifdef ENABLE_STDIO -typedef struct _RiteFILE -{ - FILE* fp; - unsigned char buf[256]; - int cnt; - int readlen; -} RiteFILE; -#endif - #ifndef _WIN32 # if SIZE_MAX < UINT32_MAX # error "It can't be run this code on this environment (SIZE_MAX < UINT32_MAX)" # endif #endif +#if CHAR_BIT != 8 +# error This code assumes CHAR_BIT == 8 +#endif + static size_t offset_crc_body() { @@ -45,21 +39,12 @@ read_rite_irep_record(mrb_state *mrb, const uint8_t *bin, uint32_t *len) { int ret; size_t i; - char *char_buf; const uint8_t *src = bin; - uint16_t tt, pool_data_len, snl, buf_size = MRB_DUMP_DEFAULT_STR_LEN; - mrb_int fix_num; - mrb_float f; + uint16_t tt, pool_data_len, snl; size_t plen; int ai = mrb_gc_arena_save(mrb); mrb_irep *irep = mrb_add_irep(mrb); - char_buf = (char *)mrb_malloc(mrb, buf_size); - if (char_buf == NULL) { - ret = MRB_DUMP_GENERAL_FAILURE; - goto error_exit; - } - // skip record size src += sizeof(uint32_t); @@ -98,34 +83,23 @@ read_rite_irep_record(mrb_state *mrb, const uint8_t *bin, uint32_t *len) } for (i = 0; i < plen; i++) { + mrb_value s; tt = *src++; //pool TT pool_data_len = bin_to_uint16(src); //pool data length src += sizeof(uint16_t); - if (pool_data_len > buf_size - 1) { - mrb_free(mrb, char_buf); - buf_size = pool_data_len + 1; - char_buf = (char *)mrb_malloc(mrb, buf_size); - if (char_buf == NULL) { - ret = MRB_DUMP_GENERAL_FAILURE; - goto error_exit; - } - } - memcpy(char_buf, src, pool_data_len); + s = mrb_str_new(mrb, (char *)src, pool_data_len); src += pool_data_len; - char_buf[pool_data_len] = '\0'; switch (tt) { //pool data case MRB_TT_FIXNUM: - fix_num = str_to_mrb_int(char_buf); - irep->pool[i] = mrb_fixnum_value(fix_num); + irep->pool[i] = mrb_str_to_inum(mrb, s, 10, FALSE); break; case MRB_TT_FLOAT: - f = str_to_mrb_float(char_buf); - irep->pool[i] = mrb_float_value(f); + irep->pool[i] = mrb_float_value(mrb_str_to_dbl(mrb, s, FALSE)); break; case MRB_TT_STRING: - irep->pool[i] = mrb_str_new(mrb, char_buf, pool_data_len); + irep->pool[i] = s; break; default: @@ -148,10 +122,6 @@ read_rite_irep_record(mrb_state *mrb, const uint8_t *bin, uint32_t *len) } for (i = 0; i < irep->slen; i++) { - static const mrb_sym mrb_sym_zero = { 0 }; - *irep->syms = mrb_sym_zero; - } - for (i = 0; i < irep->slen; i++) { snl = bin_to_uint16(src); //symbol name length src += sizeof(uint16_t); @@ -160,26 +130,16 @@ read_rite_irep_record(mrb_state *mrb, const uint8_t *bin, uint32_t *len) continue; } - if (snl > buf_size - 1) { - mrb_free(mrb, char_buf); - buf_size = snl + 1; - char_buf = (char *)mrb_malloc(mrb, buf_size); - if (char_buf == NULL) { - ret = MRB_DUMP_GENERAL_FAILURE; - goto error_exit; - } - } - memcpy(char_buf, src, snl); //symbol name + irep->syms[i] = mrb_intern2(mrb, (char *)src, snl); src += snl; - char_buf[snl] = '\0'; - irep->syms[i] = mrb_intern2(mrb, char_buf, snl); + + mrb_gc_arena_restore(mrb, ai); } } *len = src - bin; ret = MRB_DUMP_OK; error_exit: - mrb_free(mrb, char_buf); return ret; } @@ -236,7 +196,7 @@ read_rite_lineno_record(mrb_state *mrb, const uint8_t *bin, size_t irepno, uint3 int ret; size_t i, fname_len, niseq; char *fname; - short *lines; + uint16_t *lines; ret = MRB_DUMP_OK; *len = 0; @@ -259,7 +219,7 @@ read_rite_lineno_record(mrb_state *mrb, const uint8_t *bin, size_t irepno, uint3 bin += sizeof(uint32_t); // niseq *len += sizeof(uint32_t); - lines = (short *)mrb_malloc(mrb, niseq * sizeof(short)); + lines = (uint16_t *)mrb_malloc(mrb, niseq * sizeof(uint16_t)); for (i = 0; i < niseq; i++) { lines[i] = bin_to_uint16(bin); bin += sizeof(uint16_t); // niseq |
