more error handling at pic_open

This commit is contained in:
Yuichi Nishiwaki 2015-05-27 23:08:47 +09:00
parent 53dadede15
commit 6d5ef7bda5
1 changed files with 40 additions and 7 deletions

View File

@ -104,11 +104,11 @@ pic_init_features(pic_state *pic)
static void static void
pic_init_core(pic_state *pic) pic_init_core(pic_state *pic)
{ {
size_t ai = pic_gc_arena_preserve(pic);
pic_init_features(pic); pic_init_features(pic);
pic_deflibrary (pic, "(picrin base)") { pic_deflibrary (pic, "(picrin base)") {
size_t ai = pic_gc_arena_preserve(pic);
pic_define_syntactic_keyword(pic, pic->lib->env, pic->sDEFINE, pic->rDEFINE); pic_define_syntactic_keyword(pic, pic->lib->env, pic->sDEFINE, pic->rDEFINE);
pic_define_syntactic_keyword(pic, pic->lib->env, pic->sSETBANG, pic->rSETBANG); pic_define_syntactic_keyword(pic, pic->lib->env, pic->sSETBANG, pic->rSETBANG);
pic_define_syntactic_keyword(pic, pic->lib->env, pic->sQUOTE, pic->rQUOTE); pic_define_syntactic_keyword(pic, pic->lib->env, pic->sQUOTE, pic->rQUOTE);
@ -156,6 +156,10 @@ pic_open(int argc, char *argv[], char **envp)
pic = malloc(sizeof(pic_state)); pic = malloc(sizeof(pic_state));
if (! pic) {
goto EXIT_PIC;
}
/* turn off GC */ /* turn off GC */
pic->gc_enable = false; pic->gc_enable = false;
@ -171,14 +175,35 @@ pic_open(int argc, char *argv[], char **envp)
pic->stbase = pic->sp = calloc(PIC_STACK_SIZE, sizeof(pic_value)); pic->stbase = pic->sp = calloc(PIC_STACK_SIZE, sizeof(pic_value));
pic->stend = pic->stbase + PIC_STACK_SIZE; pic->stend = pic->stbase + PIC_STACK_SIZE;
if (! pic->sp) {
goto EXIT_SP;
}
/* callinfo */ /* callinfo */
pic->cibase = pic->ci = calloc(PIC_STACK_SIZE, sizeof(pic_callinfo)); pic->cibase = pic->ci = calloc(PIC_STACK_SIZE, sizeof(pic_callinfo));
pic->ciend = pic->cibase + PIC_STACK_SIZE; pic->ciend = pic->cibase + PIC_STACK_SIZE;
if (! pic->ci) {
goto EXIT_CI;
}
/* exception handler */ /* exception handler */
pic->xpbase = pic->xp = calloc(PIC_RESCUE_SIZE, sizeof(struct pic_proc *)); pic->xpbase = pic->xp = calloc(PIC_RESCUE_SIZE, sizeof(struct pic_proc *));
pic->xpend = pic->xpbase + PIC_RESCUE_SIZE; pic->xpend = pic->xpbase + PIC_RESCUE_SIZE;
if (! pic->xp) {
goto EXIT_XP;
}
/* GC arena */
pic->arena = calloc(PIC_ARENA_SIZE, sizeof(struct pic_object **));
pic->arena_size = PIC_ARENA_SIZE;
pic->arena_idx = 0;
if (! pic->arena) {
goto EXIT_ARENA;
}
/* memory heap */ /* memory heap */
pic->heap = pic_heap_open(pic); pic->heap = pic_heap_open(pic);
@ -201,11 +226,6 @@ pic_open(int argc, char *argv[], char **envp)
pic->libs = pic_nil_value(); pic->libs = pic_nil_value();
pic->lib = NULL; pic->lib = NULL;
/* GC arena */
pic->arena = calloc(PIC_ARENA_SIZE, sizeof(struct pic_object **));
pic->arena_size = PIC_ARENA_SIZE;
pic->arena_idx = 0;
/* raised error object */ /* raised error object */
pic->err = pic_undef_value(); pic->err = pic_undef_value();
@ -321,7 +341,20 @@ pic_open(int argc, char *argv[], char **envp)
pic_init_core(pic); pic_init_core(pic);
pic_gc_arena_restore(pic, ai);
return pic; return pic;
EXIT_ARENA:
free(pic->xp);
EXIT_XP:
free(pic->ci);
EXIT_CI:
free(pic->sp);
EXIT_SP:
free(pic);
EXIT_PIC:
return NULL;
} }
void void