From 052c20a29b3ef7fdd412e6eddecc21c9c1a7fba6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADtor=20Santos=20Costa=20II?= Date: Thu, 15 Apr 2010 22:49:25 +0100 Subject: [PATCH] more memory allocation fixes. --- C/amasm.c | 50 ++++++++++++++++---------------------------------- C/compiler.c | 1 + C/computils.c | 5 +++++ C/index.c | 2 ++ 4 files changed, 24 insertions(+), 34 deletions(-) diff --git a/C/amasm.c b/C/amasm.c index 313706b37..e92d79c42 100644 --- a/C/amasm.c +++ b/C/amasm.c @@ -3836,21 +3836,23 @@ Yap_assemble(int mode, Term t, PredEntry *ap, int is_fact, struct intermediates int clause_has_dbterm = FALSE; #if USE_SYSTEM_MALLOC - if (!Yap_LabelFirstArray && max_label <= DEFAULT_NLABELS) { - Yap_LabelFirstArray = (Int *)Yap_AllocCodeSpace(sizeof(Int)*DEFAULT_NLABELS); - Yap_LabelFirstArraySz = DEFAULT_NLABELS; - if (!Yap_LabelFirstArray) { - save_machine_regs(); - longjmp(cip->CompilerBotch, OUT_OF_HEAP_BOTCH); + if (!cip->label_offset) { + if (!Yap_LabelFirstArray && max_label <= DEFAULT_NLABELS) { + Yap_LabelFirstArray = (Int *)Yap_AllocCodeSpace(sizeof(Int)*DEFAULT_NLABELS); + Yap_LabelFirstArraySz = DEFAULT_NLABELS; + if (!Yap_LabelFirstArray) { + save_machine_regs(); + longjmp(cip->CompilerBotch, OUT_OF_HEAP_BOTCH); + } } - } - if (Yap_LabelFirstArray && max_label <= Yap_LabelFirstArraySz) { - cip->label_offset = Yap_LabelFirstArray; - } else { - cip->label_offset = (Int *)Yap_AllocCodeSpace(sizeof(Int)*max_label); - if (!cip->label_offset) { - save_machine_regs(); - longjmp(cip->CompilerBotch, OUT_OF_HEAP_BOTCH); + if (Yap_LabelFirstArray && max_label <= Yap_LabelFirstArraySz) { + cip->label_offset = Yap_LabelFirstArray; + } else { + cip->label_offset = (Int *)Yap_AllocCodeSpace(sizeof(Int)*max_label); + if (!cip->label_offset) { + save_machine_regs(); + longjmp(cip->CompilerBotch, OUT_OF_HEAP_BOTCH); + } } } #else @@ -3875,10 +3877,6 @@ Yap_assemble(int mode, Term t, PredEntry *ap, int is_fact, struct intermediates UInt osize; if(!(x = fetch_clause_space(&t,size,cip,&osize))){ -#if USE_SYSTEM_MALLOC - if (cip->label_offset != Yap_LabelFirstArray) - Yap_FreeCodeSpace((ADDR)cip->label_offset); -#endif return NULL; } cl = (LogUpdClause *)((CODEADDR)x-(UInt)size); @@ -3893,10 +3891,6 @@ Yap_assemble(int mode, Term t, PredEntry *ap, int is_fact, struct intermediates StaticClause *cl; UInt osize; if(!(x = fetch_clause_space(&t,size,cip,&osize))) { -#if USE_SYSTEM_MALLOC - if (cip->label_offset != Yap_LabelFirstArray) - Yap_FreeCodeSpace((ADDR)cip->label_offset); -#endif return NULL; } cl = (StaticClause *)((CODEADDR)x-(UInt)size); @@ -3906,10 +3900,6 @@ Yap_assemble(int mode, Term t, PredEntry *ap, int is_fact, struct intermediates cl->usc.ClSource = x; cl->ClSize = osize; ProfEnd=code_p; -#if USE_SYSTEM_MALLOC - if (cip->label_offset != Yap_LabelFirstArray) - Yap_FreeCodeSpace((ADDR)cip->label_offset); -#endif return entry_code; } else { while ((cip->code_addr = (yamop *) Yap_AllocCodeSpace(size)) == NULL) { @@ -3917,10 +3907,6 @@ Yap_assemble(int mode, Term t, PredEntry *ap, int is_fact, struct intermediates if (!Yap_growheap(TRUE, size, cip)) { Yap_Error_TYPE = OUT_OF_HEAP_ERROR; Yap_Error_Size = size; -#if USE_SYSTEM_MALLOC - if (cip->label_offset != Yap_LabelFirstArray) - Yap_FreeCodeSpace((ADDR)cip->label_offset); -#endif return NULL; } } @@ -3944,10 +3930,6 @@ Yap_assemble(int mode, Term t, PredEntry *ap, int is_fact, struct intermediates Yap_inform_profiler_of_clause(entry_code, ProfEnd, ap, mode == ASSEMBLING_INDEX); } #endif /* LOW_PROF */ -#if USE_SYSTEM_MALLOC - if (cip->label_offset != Yap_LabelFirstArray) - Yap_FreeCodeSpace((ADDR)cip->label_offset); -#endif return entry_code; } diff --git a/C/compiler.c b/C/compiler.c index 3b97148a8..06f58392a 100644 --- a/C/compiler.c +++ b/C/compiler.c @@ -3405,6 +3405,7 @@ Yap_cclause(volatile Term inp_clause, int NOfArgs, Term mod, volatile Term src) cglobs.cint.BlobsStart = cglobs.cint.icpc = NULL; cglobs.cint.dbterml = NULL; cglobs.cint.blks = NULL; + cglobs.cint.label_offset = NULL; cglobs.cint.freep = cglobs.cint.freep0 = (char *) (H + maxvnum+(sizeof(Int)/sizeof(CELL))*MaxTemps+MaxTemps); diff --git a/C/computils.c b/C/computils.c index 0bba95a3d..11abe4b26 100644 --- a/C/computils.c +++ b/C/computils.c @@ -170,7 +170,12 @@ Yap_ReleaseCMem (struct intermediates *cip) p = nextp; } cip->blks = NULL; + if (cip->label_offset && + cip->label_offset != Yap_LabelFirstArray) { + Yap_FreeCodeSpace((ADDR)cip->label_offset); + } #endif + cip->label_offset = NULL; } char * diff --git a/C/index.c b/C/index.c index dde4ca808..d019ee394 100644 --- a/C/index.c +++ b/C/index.c @@ -3419,6 +3419,7 @@ Yap_PredIsIndexable(PredEntry *ap, UInt NSlots, yamop *next_pc) Yap_BuildMegaClause(ap); cint.CodeStart = cint.BlobsStart = cint.cpc = cint.icpc = NULL; cint.expand_block = NULL; + cint.label_offset = NULL; Yap_ErrorMessage = NULL; if (compile_index(&cint) == (UInt)FAILCODE) { Yap_ReleaseCMem(&cint); @@ -4499,6 +4500,7 @@ ExpandIndex(PredEntry *ap, int ExtraArgs, yamop *nextop) { cint.blks = NULL; cint.cls = NULL; cint.code_addr = NULL; + cint.label_offset = NULL; if ((cb = setjmp(cint.CompilerBotch)) == 3) { restore_machine_regs(); /* grow stack */