From d1f1b4ea38cd5a2a9d0b2aa3547d4f2828180299 Mon Sep 17 00:00:00 2001 From: "Yukihiro \"Matz\" Matsumoto" Date: Thu, 31 Mar 2022 18:28:01 +0900 Subject: [PATCH] cdump.c: add `const` qualifier for line number arrays. --- include/mruby/debug.h | 7 ++++--- src/cdump.c | 6 +++--- src/debug.c | 12 ++++++------ src/load.c | 17 ++++++++++------- src/symbol.c | 6 +++--- 5 files changed, 26 insertions(+), 22 deletions(-) diff --git a/include/mruby/debug.h b/include/mruby/debug.h index 7319ccb3f..4a62cce42 100644 --- a/include/mruby/debug.h +++ b/include/mruby/debug.h @@ -31,10 +31,11 @@ typedef struct mrb_irep_debug_info_file { uint32_t line_entry_count; mrb_debug_line_type line_type; union { + const char *s; void *ptr; - uint16_t *ary; - mrb_irep_debug_info_line *flat_map; - uint8_t *packed_map; + const uint16_t *ary; + const mrb_irep_debug_info_line *flat_map; + const uint8_t *packed_map; } lines; } mrb_irep_debug_info_file; diff --git a/src/cdump.c b/src/cdump.c index b4dfc4e87..12173583d 100644 --- a/src/cdump.c +++ b/src/cdump.c @@ -302,7 +302,7 @@ cdump_debug(mrb_state *mrb, const char *name, int n, mrb_irep_debug_info *info, line_type = "mrb_debug_line_flat_map"; fprintf(fp, "static struct mrb_irep_debug_info_line %s_debug_lines_%d[%d] = {", name, n, len); for (i=0; ifiles[0]->lines.flat_map[i]; + const mrb_irep_debug_info_line *fmap = &info->files[0]->lines.flat_map[i]; fprintf(fp, "\t{.start_pos=0x%04x,.line=%d},\n", fmap->start_pos, fmap->line); } fputs("};\n", fp); @@ -310,8 +310,8 @@ cdump_debug(mrb_state *mrb, const char *name, int n, mrb_irep_debug_info *info, case mrb_debug_line_packed_map: line_type = "mrb_debug_line_packed_map"; - fprintf(fp, "static char %s_debug_lines_%d[] = \"", name, n); - uint8_t *pmap = info->files[0]->lines.packed_map; + fprintf(fp, "static const char %s_debug_lines_%d[] = \"", name, n); + const uint8_t *pmap = info->files[0]->lines.packed_map; for (i=0; ilines.flat_map; + const mrb_irep_debug_info_line *ret = f->lines.flat_map; uint32_t count = f->line_entry_count; while (count > 0) { int32_t step = count / 2; - mrb_irep_debug_info_line *it = ret + step; + const mrb_irep_debug_info_line *it = ret + step; if (!(pc < it->start_pos)) { ret = it + 1; count -= step + 1; @@ -131,8 +131,8 @@ mrb_debug_get_line(mrb_state *mrb, const mrb_irep *irep, uint32_t pc) } case mrb_debug_line_packed_map: { - uint8_t *p = f->lines.packed_map; - uint8_t *pend = p + f->line_entry_count; + const uint8_t *p = f->lines.packed_map; + const uint8_t *pend = p + f->line_entry_count; uint32_t pos = 0, line = 0, line_diff; while (p < pend) { pos += mrb_packed_int_decode(p, &p); @@ -209,7 +209,7 @@ mrb_debug_info_append_file(mrb_state *mrb, mrb_irep_debug_info *d, packed_size += mrb_packed_int_len(lines[start_pos+i]-prev_line); prev_line = lines[start_pos + i]; } - p = f->lines.packed_map = (uint8_t*)mrb_malloc(mrb, packed_size); + f->lines.packed_map = p = (uint8_t*)mrb_malloc(mrb, packed_size); pend = p + packed_size; prev_line = 0; prev_pc = 0; for (i = 0; i < file_pc_count; ++i) { diff --git a/src/load.c b/src/load.c index bb22a6130..d8e2b22ac 100644 --- a/src/load.c +++ b/src/load.c @@ -352,11 +352,12 @@ read_debug_record(mrb_state *mrb, const uint8_t *start, const uint8_t *end, mrb_ size_t l = sizeof(uint16_t) * (size_t)file->line_entry_count; if (bin + l > end) return MRB_DUMP_GENERAL_FAILURE; - file->lines.ary = (uint16_t *)mrb_malloc(mrb, l); + uint16_t *ary = (uint16_t *)mrb_malloc(mrb, l); for (l = 0; l < file->line_entry_count; ++l) { - file->lines.ary[l] = bin_to_uint16(bin); + ary[l] = bin_to_uint16(bin); bin += sizeof(uint16_t); } + file->lines.ary = ary; } break; case mrb_debug_line_flat_map: { @@ -364,21 +365,23 @@ read_debug_record(mrb_state *mrb, const uint8_t *start, const uint8_t *end, mrb_ size_t n = sizeof(mrb_irep_debug_info_line); if (bin + c*n > end) return MRB_DUMP_GENERAL_FAILURE; - file->lines.flat_map = (mrb_irep_debug_info_line*)mrb_calloc(mrb, c, n); + mrb_irep_debug_info_line *flat_map = (mrb_irep_debug_info_line*)mrb_calloc(mrb, c, n); for (size_t l = 0; l < file->line_entry_count; ++l) { - file->lines.flat_map[l].start_pos = bin_to_uint32(bin); + flat_map[l].start_pos = bin_to_uint32(bin); bin += sizeof(uint32_t); - file->lines.flat_map[l].line = bin_to_uint16(bin); + flat_map[l].line = bin_to_uint16(bin); bin += sizeof(uint16_t); } + file->lines.flat_map = flat_map; } break; case mrb_debug_line_packed_map: { size_t l = (size_t)file->line_entry_count; if (bin + l > end) return MRB_DUMP_GENERAL_FAILURE; - file->lines.packed_map = (uint8_t*)mrb_calloc(mrb, 1, l); - memcpy(file->lines.packed_map, bin, file->line_entry_count); + uint8_t *packed_map = (uint8_t*)mrb_malloc(mrb, l); + memcpy(packed_map, bin, file->line_entry_count); + file->lines.packed_map = packed_map; bin += file->line_entry_count; } break; diff --git a/src/symbol.c b/src/symbol.c index cc8986eaa..c371752c9 100644 --- a/src/symbol.c +++ b/src/symbol.c @@ -136,7 +136,7 @@ symhash(const char *key, size_t len) size_t mrb_packed_int_len(uint32_t num); size_t mrb_packed_int_encode(uint32_t num, uint8_t *p, uint8_t *pend); -uint32_t mrb_packed_int_decode(uint8_t *p, uint8_t **newpos); +uint32_t mrb_packed_int_decode(const uint8_t *p, const uint8_t **newpos); #define sym_lit_p(mrb, i) (mrb->symflags[i>>3]&(1<<(i&7))) #define sym_lit_set(mrb, i) mrb->symflags[i>>3]|=(1<<(i&7)) @@ -154,7 +154,7 @@ sym_check(mrb_state *mrb, const char *name, size_t len, mrb_sym i) } else { /* length in BER */ - symlen = mrb_packed_int_decode((uint8_t*)symname, (uint8_t**)&symname); + symlen = mrb_packed_int_decode((const uint8_t*)symname, (const uint8_t**)&symname); } if (len == symlen && memcmp(symname, name, len) == 0) { return TRUE; @@ -347,7 +347,7 @@ sym2name_len(mrb_state *mrb, mrb_sym sym, char *buf, mrb_int *lenp) const char *symname = mrb->symtbl[sym]; if (!sym_lit_p(mrb, sym)) { - size_t len = mrb_packed_int_decode((uint8_t*)symname, (uint8_t**)&symname); + uint32_t len = mrb_packed_int_decode((const uint8_t*)symname, (const uint8_t**)&symname); if (lenp) *lenp = (mrb_int)len; } else if (lenp) {