Skip to content

Commit

Permalink
cdump.c: add const qualifier for line number arrays.
Browse files Browse the repository at this point in the history
  • Loading branch information
matz committed Apr 1, 2022
1 parent 9de6065 commit d1f1b4e
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 22 deletions.
7 changes: 4 additions & 3 deletions include/mruby/debug.h
Expand Up @@ -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;

Expand Down
6 changes: 3 additions & 3 deletions src/cdump.c
Expand Up @@ -302,16 +302,16 @@ 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; i<len; i++) {
mrb_irep_debug_info_line *fmap = &info->files[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);
break;

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; i<len; i++) {
fprintf(fp, "\\x%02x", pmap[i]&0xff);
}
Expand Down
12 changes: 6 additions & 6 deletions src/debug.c
Expand Up @@ -63,7 +63,7 @@ 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)
mrb_packed_int_decode(const uint8_t *p, const uint8_t **newpos)
{
size_t i = 0, shift = 0;
uint32_t n = 0;
Expand Down Expand Up @@ -106,11 +106,11 @@ mrb_debug_get_line(mrb_state *mrb, const mrb_irep *irep, uint32_t pc)

case mrb_debug_line_flat_map: {
/* get upper bound */
mrb_irep_debug_info_line *ret = f->lines.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;
Expand All @@ -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);
Expand Down Expand Up @@ -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) {
Expand Down
17 changes: 10 additions & 7 deletions src/load.c
Expand Up @@ -352,33 +352,36 @@ 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: {
size_t c = (size_t)file->line_entry_count;
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;

Expand Down
6 changes: 3 additions & 3 deletions src/symbol.c
Expand Up @@ -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))
Expand All @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit d1f1b4e

Please sign in to comment.