improve allocation through malloc: avoid allocating labels in the stack.

This commit is contained in:
Vitor Santos Costa 2010-04-15 11:37:39 +01:00
parent 78b3213d5a
commit b737ce447f
4 changed files with 32 additions and 7 deletions

View File

@ -3516,14 +3516,16 @@ do_pass(int pass_no, yamop **entry_codep, int assembling, int *clause_has_blobsp
code_p = a_il((CELL)*entry_codep, _Ystop, code_p, pass_no, cip);
}
if (!pass_no) {
#if !USE_SYSTEM_MALLOC
if (CellPtr(cip->label_offset+cip->cpc->rnd1) > ASP-256) {
Yap_Error_Size = 256+((char *)(cip->label_offset+cip->cpc->rnd1) - (char *)H);
save_machine_regs();
longjmp(cip->CompilerBotch, 3);
}
if ( (char *)(cip->label_offset+cip->cpc->rnd1) >= cip->freep)
cip->freep = (char *)(cip->label_offset+(cip->cpc->rnd1+1));
#endif
cip->label_offset[cip->cpc->rnd1] = (CELL) code_p;
}
/* reset dealloc_found in case there was a branch */
@ -3819,7 +3821,7 @@ init_dbterms_list(yamop *code_p, PredEntry *ap)
yamop *
Yap_assemble(int mode, Term t, PredEntry *ap, int is_fact, struct intermediates *cip)
Yap_assemble(int mode, Term t, PredEntry *ap, int is_fact, struct intermediates *cip, UInt max_label)
{
/*
* the assembly proccess is done in two passes: 1 - a first pass
@ -3832,7 +3834,15 @@ Yap_assemble(int mode, Term t, PredEntry *ap, int is_fact, struct intermediates
int clause_has_blobs = FALSE;
int clause_has_dbterm = FALSE;
#if USE_SYSTEM_MALLOC
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
cip->label_offset = (Int *)cip->freep;
#endif
cip->code_addr = NULL;
code_p = do_pass(0, &entry_code, mode, &clause_has_blobs, &clause_has_dbterm, cip, size);
if (clause_has_dbterm) {
@ -3852,6 +3862,9 @@ 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
Yap_FreeCodeSpace((ADDR)cip->label_offset);
#endif
return NULL;
}
cl = (LogUpdClause *)((CODEADDR)x-(UInt)size);
@ -3866,6 +3879,9 @@ 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
Yap_FreeCodeSpace((ADDR)cip->label_offset);
#endif
return NULL;
}
cl = (StaticClause *)((CODEADDR)x-(UInt)size);
@ -3875,6 +3891,9 @@ 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
Yap_FreeCodeSpace((ADDR)cip->label_offset);
#endif
return entry_code;
} else {
while ((cip->code_addr = (yamop *) Yap_AllocCodeSpace(size)) == NULL) {
@ -3882,6 +3901,9 @@ 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
Yap_FreeCodeSpace((ADDR)cip->label_offset);
#endif
return NULL;
}
}
@ -3905,6 +3927,9 @@ 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
Yap_FreeCodeSpace((ADDR)cip->label_offset);
#endif
return entry_code;
}

View File

@ -3560,7 +3560,7 @@ Yap_cclause(volatile Term inp_clause, int NOfArgs, Term mod, volatile Term src)
#endif
/* phase 3: assemble code */
acode = Yap_assemble(ASSEMBLING_CLAUSE, src, cglobs.cint.CurrentPred, (cglobs.is_a_fact && !cglobs.hasdbrefs && !(cglobs.cint.CurrentPred->PredFlags & TabledPredFlag)), &cglobs.cint);
acode = Yap_assemble(ASSEMBLING_CLAUSE, src, cglobs.cint.CurrentPred, (cglobs.is_a_fact && !cglobs.hasdbrefs && !(cglobs.cint.CurrentPred->PredFlags & TabledPredFlag)), &cglobs.cint, cglobs.labelno+1);
/* check first if there was space for us */
Yap_ReleaseCMem (&cglobs.cint);
if (acode == NULL) {

View File

@ -553,7 +553,6 @@ recover_from_failed_susp_on_cls(struct intermediates *cint, UInt sz)
OPCODE ecls = Yap_opcode(_expand_clauses);
UInt log_upd_pred = cint->CurrentPred->PredFlags & LogUpdatePredFlag;
Yap_ReleaseCMem(cint);
while (cpc) {
switch(cpc->op) {
case enter_lu_op:
@ -635,6 +634,7 @@ recover_from_failed_susp_on_cls(struct intermediates *cint, UInt sz)
}
cpc = cpc->nextInst;
}
Yap_ReleaseCMem(cint);
if (cint->code_addr) {
Yap_FreeCodeSpace((char *)cint->code_addr);
cint->code_addr = NULL;
@ -3420,7 +3420,7 @@ Yap_PredIsIndexable(PredEntry *ap, UInt NSlots, yamop *next_pc)
/* globals for assembler */
IPredArity = ap->ArityOfPE;
if (cint.CodeStart) {
if ((indx_out = Yap_assemble(ASSEMBLING_INDEX, TermNil, ap, FALSE, &cint)) == NULL) {
if ((indx_out = Yap_assemble(ASSEMBLING_INDEX, TermNil, ap, FALSE, &cint, cint.i_labelno+1)) == NULL) {
if (!Yap_growheap(FALSE, Yap_Error_Size, NULL)) {
Yap_ReleaseCMem(&cint);
Yap_Error(OUT_OF_HEAP_ERROR, TermNil, Yap_ErrorMessage);
@ -4612,7 +4612,7 @@ ExpandIndex(PredEntry *ap, int ExtraArgs, yamop *nextop) {
/* globals for assembler */
IPredArity = ap->ArityOfPE;
if (cint.CodeStart) {
if ((indx_out = Yap_assemble(ASSEMBLING_EINDEX, TermNil, ap, FALSE, &cint)) == NULL) {
if ((indx_out = Yap_assemble(ASSEMBLING_EINDEX, TermNil, ap, FALSE, &cint, cint.i_labelno+1)) == NULL) {
if (!Yap_growheap(FALSE, Yap_Error_Size, NULL)) {
Yap_Error(OUT_OF_HEAP_ERROR, TermNil, Yap_ErrorMessage);
Yap_ReleaseCMem(&cint);

View File

@ -318,7 +318,7 @@ typedef enum special_label_op_enum {
#define Two 2
yamop *STD_PROTO(Yap_assemble,(int,Term,struct pred_entry *,int, struct intermediates *));
yamop *STD_PROTO(Yap_assemble,(int,Term,struct pred_entry *,int, struct intermediates *, UInt));
void STD_PROTO(Yap_emit,(compiler_vm_op,Int,CELL, struct intermediates *));
void STD_PROTO(Yap_emit_3ops,(compiler_vm_op,CELL,CELL,CELL, struct intermediates *));
void STD_PROTO(Yap_emit_4ops,(compiler_vm_op,CELL,CELL,CELL,CELL, struct intermediates *));