remove memleaks using linked allocator

This commit is contained in:
Yukihiro Matsumoto
2012-09-03 08:34:31 +09:00
parent 5340126443
commit 4f7a1a167d
5 changed files with 39 additions and 17 deletions
+3 -2
View File
@@ -106,8 +106,8 @@ typedef struct mrb_state {
struct RClass *false_class;
struct RClass *nil_class;
struct RClass *symbol_class;
struct RClass *kernel_module;
struct heap_page *heaps;
struct heap_page *sweeps;
struct heap_page *free_heaps;
@@ -124,10 +124,10 @@ typedef struct mrb_state {
int gc_interval_ratio;
int gc_step_ratio;
int gc_disabled;
struct alloca_header *mems;
mrb_sym symidx;
struct kh_n2s *name2sym; /* symbol table */
struct kh_s2n *sym2name; /* reverse symbol table */
#ifdef INCLUDE_REGEXP
struct RNode *local_svar;/* regexp */
#endif
@@ -362,6 +362,7 @@ void mrb_pool_close(struct mrb_pool*);
void* mrb_pool_alloc(struct mrb_pool*, size_t);
void* mrb_pool_realloc(struct mrb_pool*, void*, size_t oldlen, size_t newlen);
int mrb_pool_can_realloc(struct mrb_pool*, void*, size_t);
void* mrb_alloca(mrb_state *mrb, size_t);
#if defined(__cplusplus)
} /* extern "C" { */
+1 -10
View File
@@ -2091,9 +2091,6 @@ scope_finish(codegen_scope *s, int idx)
irep->nregs = s->nregs;
mrb->arena_idx = s->ai;
if (!s->prev && s->filename) {
mrb_free(mrb, s->filename);
}
mrb_pool_close(s->mpool);
}
@@ -2499,19 +2496,13 @@ codegen_start(mrb_state *mrb, parser_state *p)
}
scope->mrb = mrb;
if (p->filename) {
int len = strlen(p->filename);
char *s = (char*)mrb_malloc(mrb, len+1);
memcpy(s, p->filename, len + 1);
scope->filename = s;
scope->filename = p->filename;
}
if (setjmp(scope->jmp) != 0) {
//if (scope->filename) mrb_free(mrb, scope->filename);
return -1;
}
// prepare irep
codegen(scope, p->tree, NOVAL);
// if (scope->filename) mrb_free(mrb, scope->filename);
mrb_pool_close(scope->mpool);
return 0;
}
+1 -3
View File
@@ -4789,7 +4789,6 @@ void
mrbc_context_free(mrb_state *mrb, mrbc_context *cxt)
{
mrb_free(mrb, cxt->syms);
mrb_free(mrb, cxt->filename);
mrb_free(mrb, cxt);
}
@@ -4798,10 +4797,9 @@ mrbc_filename(mrb_state *mrb, mrbc_context *c, const char *s)
{
if (s) {
int len = strlen(s);
char *p = (char *)mrb_malloc(mrb, len + 1);
char *p = (char *)mrb_alloca(mrb, len + 1);
memcpy(p, s, len + 1);
if (c->filename) mrb_free(mrb, c->filename);
c->filename = p;
c->lineno = 1;
}
+3 -2
View File
@@ -144,12 +144,13 @@ void
mrb_init_proc(mrb_state *mrb)
{
struct RProc *m;
mrb_code *call_iseq = (mrb_code *)mrb_malloc(mrb, sizeof(mrb_code));
mrb_irep *call_irep = (mrb_irep *)mrb_calloc(mrb, sizeof(mrb_irep), 1);
mrb_code *call_iseq = (mrb_code *)mrb_alloca(mrb, sizeof(mrb_code));
mrb_irep *call_irep = (mrb_irep *)mrb_alloca(mrb, sizeof(mrb_irep));
if ( call_iseq == NULL || call_irep == NULL )
return;
memset(call_irep, 0, sizeof(mrb_irep));
*call_iseq = MKOP_A(OP_CALL, 0);
call_irep->idx = -1;
call_irep->iseq = call_iseq;
+31
View File
@@ -42,6 +42,35 @@ allocf(mrb_state *mrb, void *p, size_t size, void *ud)
}
}
struct alloca_header {
struct alloca_header *next;
char buf[0];
};
void*
mrb_alloca(mrb_state *mrb, size_t size)
{
struct alloca_header *p;
p = mrb_malloc(mrb, sizeof(struct alloca_header)+size);
p->next = mrb->mems;
mrb->mems = p;
return (void*)p->buf;
}
static void
mrb_alloca_free(mrb_state *mrb)
{
struct alloca_header *p = mrb->mems;
struct alloca_header *tmp;
while (p) {
tmp = p;
p = p->next;
mrb_free(mrb, tmp);
}
}
mrb_state*
mrb_open()
{
@@ -66,11 +95,13 @@ mrb_close(mrb_state *mrb)
mrb_free(mrb, mrb->irep[i]->iseq);
mrb_free(mrb, mrb->irep[i]->pool);
mrb_free(mrb, mrb->irep[i]->syms);
mrb_free(mrb, mrb->irep[i]->lines);
mrb_free(mrb, mrb->irep[i]);
}
mrb_free(mrb, mrb->irep);
mrb_free_symtbl(mrb);
mrb_free_heap(mrb);
mrb_alloca_free(mrb);
mrb_free(mrb, mrb);
}