mirror of
https://github.com/mruby/mruby
synced 2026-06-08 16:11:16 +00:00
refactor and move irep malloc away to mrb_add_irep()
This commit is contained in:
@@ -30,7 +30,7 @@ typedef struct mrb_irep {
|
||||
|
||||
#define MRB_ISEQ_NO_FREE 1
|
||||
|
||||
void mrb_add_irep(mrb_state *mrb, int n);
|
||||
mrb_irep *mrb_add_irep(mrb_state *mrb);
|
||||
|
||||
#if defined(__cplusplus)
|
||||
} /* extern "C" { */
|
||||
|
||||
+5
-11
@@ -2055,8 +2055,9 @@ scope_new(mrb_state *mrb, codegen_scope *prev, node *lv)
|
||||
static const codegen_scope codegen_scope_zero = { 0 };
|
||||
mrb_pool *pool = mrb_pool_open(mrb);
|
||||
codegen_scope *p = (codegen_scope *)mrb_pool_alloc(pool, sizeof(codegen_scope));
|
||||
if (!p) return 0;
|
||||
mrb_irep *irep;
|
||||
|
||||
if (!p) return 0;
|
||||
*p = codegen_scope_zero;
|
||||
p->mrb = mrb;
|
||||
p->mpool = pool;
|
||||
@@ -2079,11 +2080,9 @@ scope_new(mrb_state *mrb, codegen_scope *prev, node *lv)
|
||||
p->nlocals = p->sp;
|
||||
p->ai = mrb->arena_idx;
|
||||
|
||||
//because of a potential bad memory access in case of gc let's allocate the irep right now
|
||||
mrb_add_irep(mrb, mrb->irep_len);
|
||||
mrb->irep[mrb->irep_len] = (mrb_irep *)mrb_malloc(mrb, sizeof(mrb_irep));
|
||||
mrb->irep[mrb->irep_len]->plen = 0;
|
||||
p->idx = mrb->irep_len++;
|
||||
irep = mrb_add_irep(mrb);
|
||||
p->idx = irep->idx;
|
||||
|
||||
p->filename = prev->filename;
|
||||
if (p->filename) {
|
||||
p->lines = (short*)mrb_malloc(mrb, sizeof(short)*p->icapa);
|
||||
@@ -2097,13 +2096,8 @@ scope_finish(codegen_scope *s, int idx)
|
||||
mrb_state *mrb = s->mrb;
|
||||
mrb_irep *irep;
|
||||
|
||||
//Comment out these instructions already done in scope_new
|
||||
//mrb_add_irep(mrb, idx);
|
||||
//irep = mrb->irep[idx] = (mrb_irep *)mrb_malloc(mrb, sizeof(mrb_irep));
|
||||
irep = mrb->irep[idx];
|
||||
|
||||
irep->flags = 0;
|
||||
irep->idx = idx;
|
||||
if (s->iseq) {
|
||||
irep->iseq = (mrb_code *)codegen_realloc(s, s->iseq, sizeof(mrb_code)*s->pc);
|
||||
irep->ilen = s->pc;
|
||||
|
||||
+4
-15
@@ -47,7 +47,7 @@ static unsigned char* rite_fgets(RiteFILE*,unsigned char*,int,int);
|
||||
static int load_rite_header(FILE*,rite_binary_header*,unsigned char*);
|
||||
static int load_rite_irep_record(mrb_state*, RiteFILE*,unsigned char*,uint32_t*);
|
||||
static int read_rite_header(mrb_state*,unsigned char*,rite_binary_header*);
|
||||
static int read_rite_irep_record(mrb_state*,unsigned char*,mrb_irep*,uint32_t*);
|
||||
static int read_rite_irep_record(mrb_state*,unsigned char*,uint32_t*);
|
||||
|
||||
|
||||
static unsigned char
|
||||
@@ -321,7 +321,7 @@ read_rite_header(mrb_state *mrb, unsigned char *bin, rite_binary_header* bin_he
|
||||
}
|
||||
|
||||
static int
|
||||
read_rite_irep_record(mrb_state *mrb, unsigned char *src, mrb_irep *irep, uint32_t* len)
|
||||
read_rite_irep_record(mrb_state *mrb, unsigned char *src, uint32_t* len)
|
||||
{
|
||||
int i, ret = MRB_DUMP_OK;
|
||||
char *buf;
|
||||
@@ -330,6 +330,7 @@ read_rite_irep_record(mrb_state *mrb, unsigned char *src, mrb_irep *irep, uint32
|
||||
mrb_int fix_num;
|
||||
mrb_float f;
|
||||
int ai = mrb_gc_arena_save(mrb);
|
||||
mrb_irep *irep = mrb_add_irep(mrb);
|
||||
|
||||
recordStart = src;
|
||||
buf = (char *)mrb_malloc(mrb, bufsize);
|
||||
@@ -509,25 +510,13 @@ mrb_read_irep(mrb_state *mrb, const char *bin)
|
||||
//Read File Header Section
|
||||
if ((nirep = read_rite_header(mrb, src, &bin_header)) < 0)
|
||||
return nirep;
|
||||
|
||||
mrb_add_irep(mrb, sirep + nirep);
|
||||
|
||||
for (n=0,i=sirep; n<nirep; n++,i++) {
|
||||
static const mrb_irep mrb_irep_zero = { 0 };
|
||||
if ((mrb->irep[i] = (mrb_irep *)mrb_malloc(mrb, sizeof(mrb_irep))) == NULL) {
|
||||
ret = MRB_DUMP_GENERAL_FAILURE;
|
||||
goto error_exit;
|
||||
}
|
||||
*mrb->irep[i] = mrb_irep_zero;
|
||||
}
|
||||
src += sizeof(bin_header) + MRB_DUMP_SIZE_OF_SHORT; //header + crc
|
||||
|
||||
//Read Binary Data Section
|
||||
for (n=0,i=sirep; n<nirep; n++,i++) {
|
||||
src += MRB_DUMP_SIZE_OF_LONG; //record ren
|
||||
if ((ret = read_rite_irep_record(mrb, src, mrb->irep[i], &len)) != MRB_DUMP_OK)
|
||||
if ((ret = read_rite_irep_record(mrb, src, &len)) != MRB_DUMP_OK)
|
||||
goto error_exit;
|
||||
mrb->irep[mrb->irep_len++]->idx = i;
|
||||
src += len;
|
||||
}
|
||||
if (0 != bin_to_uint32(src)) { //dummy record len
|
||||
|
||||
+14
-5
@@ -109,20 +109,23 @@ mrb_close(mrb_state *mrb)
|
||||
mrb_free(mrb, mrb);
|
||||
}
|
||||
|
||||
void
|
||||
mrb_add_irep(mrb_state *mrb, int idx)
|
||||
mrb_irep*
|
||||
mrb_add_irep(mrb_state *mrb)
|
||||
{
|
||||
static const mrb_irep mrb_irep_zero = { 0 };
|
||||
mrb_irep *irep;
|
||||
|
||||
if (!mrb->irep) {
|
||||
int max = 256;
|
||||
|
||||
if (idx > max) max = idx+1;
|
||||
if (mrb->irep_len > max) max = mrb->irep_len+1;
|
||||
mrb->irep = (mrb_irep **)mrb_calloc(mrb, max, sizeof(mrb_irep*));
|
||||
mrb->irep_capa = max;
|
||||
}
|
||||
else if (mrb->irep_capa <= idx) {
|
||||
else if (mrb->irep_capa <= mrb->irep_len) {
|
||||
int i;
|
||||
size_t old_capa = mrb->irep_capa;
|
||||
while (mrb->irep_capa <= idx) {
|
||||
while (mrb->irep_capa <= mrb->irep_len) {
|
||||
mrb->irep_capa *= 2;
|
||||
}
|
||||
mrb->irep = (mrb_irep **)mrb_realloc(mrb, mrb->irep, sizeof(mrb_irep*)*mrb->irep_capa);
|
||||
@@ -130,6 +133,12 @@ mrb_add_irep(mrb_state *mrb, int idx)
|
||||
mrb->irep[i] = NULL;
|
||||
}
|
||||
}
|
||||
irep = (mrb_irep *)mrb_malloc(mrb, sizeof(mrb_irep));
|
||||
*irep = mrb_irep_zero;
|
||||
mrb->irep[mrb->irep_len] = irep;
|
||||
irep->idx = mrb->irep_len++;
|
||||
|
||||
return irep;
|
||||
}
|
||||
|
||||
mrb_value
|
||||
|
||||
Reference in New Issue
Block a user