diff --git a/C/globals.c b/C/globals.c index 7d8ca0126..637066b64 100644 --- a/C/globals.c +++ b/C/globals.c @@ -285,7 +285,6 @@ static Term GrowArena(Term arena, CELL *pt, size_t old_size, size_t size, if (size < 4*MIN_ARENA_SIZE) { size = 4*MIN_ARENA_SIZE; } - exit_cell_space(cspace); while (HR + size > ASP - MIN_ARENA_SIZE) { XREGS[arity + 1] = arena; if (!Yap_gcl(size * sizeof(CELL), arity + 1, ENV, gc_P(P, CP))) { @@ -295,10 +294,10 @@ static Term GrowArena(Term arena, CELL *pt, size_t old_size, size_t size, arena = XREGS[arity + 1]; adjust_cps(size PASS_REGS); } - pt = ArenaLimit(arena)+1; + pt = ArenaLimit(arena); sz = old_size + size; if (pt == HR) { - HR += size+1; + HR += size; HR[ - 1] = EndSpecials; } else { if ((sz = Yap_InsertInGlobal(pt, size * sizeof(CELL))) == 0) { @@ -669,12 +668,13 @@ static Term CopyTermToArena(Term t, Term arena, bool share, bool copy_att_vars, size_t min_grow USES_REGS) { size_t old_size = ArenaSz(arena); cell_space_t cspace; - enter_cell_space( & cspace); int res = 0; Term tn; + int restarts = 0; restart: t = Deref(t); + enter_cell_space( & cspace); if (IsVarTerm(t)) { ASP = ArenaLimit(arena); HR = HB = ArenaPt(arena); @@ -819,6 +819,8 @@ error_handler: case -1: if (arena == LOCAL_GlobalArena) LOCAL_GlobalArenaOverflows++; + restarts++; + min_grow += (restarts < 16 ? 16*1024*restarts*restarts : 128*1024*1024); if ((arena=GrowArena(arena, old_top, old_size, min_grow, arity + 3, &cspace PASS_REGS))==0) { Yap_Error(RESOURCE_ERROR_STACK, TermNil, LOCAL_ErrorMessage); return 0L; @@ -1678,8 +1680,10 @@ static Int p_nb_queue_enqueue(USES_REGS1) { if (!qd) return FALSE; arena = GetQueueArena(qd, "enqueue"); - if (arena == 0L) - return FALSE; + if (arena == 0L) { + return FALSE; + } + if (IsPairTerm(qd[QUEUE_HEAD])) { min_size = ArenaPt(arena) - RepPair(qd[QUEUE_HEAD]); } else { diff --git a/C/grow.c b/C/grow.c index d70562173..431af42c8 100755 --- a/C/grow.c +++ b/C/grow.c @@ -896,9 +896,6 @@ static_growglobal(size_t request, CELL **ptr, CELL *hsplit USES_REGS) Int size = request/sizeof(CELL); char vb_msg1 = '\0', *vb_msg2; bool do_grow = true; - - printf("request=%ld at %p, shift %p, %ld <-> %ld\n",request, hsplit, ptr,hsplit-H0,HR-hsplit); - /* request is the amount of memory we requesd, in bytes; base_move is the shift in global stacks we had to do @@ -911,8 +908,6 @@ static_growglobal(size_t request, CELL **ptr, CELL *hsplit USES_REGS) if (hsplit < H0 || hsplit > HR) return false; - if (hsplit == H0) - hsplit = NULL; if (hsplit == HR && Unsigned(HR)+request < Unsigned(ASP)-StackGap( PASS_REGS1 )) { return request; } @@ -1533,7 +1528,6 @@ UInt Yap_InsertInGlobal(CELL *where, size_t howmuch) { CACHE_REGS - printf("where=%p, hm=%d\n",where,howmuch); if ((howmuch = static_growglobal(howmuch, NULL, where PASS_REGS)) == 0) return 0; #ifdef TABLING diff --git a/C/terms.c b/C/terms.c index 4927b0df0..3839b6d6a 100644 --- a/C/terms.c +++ b/C/terms.c @@ -82,25 +82,20 @@ typedef struct non_single_struct_t { #define WALK_COMPLEX_TERM__(LIST0, STRUCT0, PRIMI0) \ \ - int lvl = push_text_stack();\ - CELL *pt0, *pt0_end; \ - size_t auxsz = 1024 * sizeof(struct non_single_struct_t);\ - struct non_single_struct_t *to_visit0=NULL, *to_visit,* to_visit_max;\ - CELL *InitialH = HR;\ - tr_fr_ptr TR0 = TR;\ - if (TR > (tr_fr_ptr)LOCAL_TrailTop - 256) { \ + reset:\ + lvl = push_text_stack();\ + to_visit0 = Malloc(auxsz); \ +pt0 = pt0_; pt0_end = pt0_end_; \ +to_visit = to_visit0, \ + to_visit_max = to_visit + auxsz/sizeof(struct non_single_struct_t);\ + \ + if (TR > (tr_fr_ptr)LOCAL_TrailTop - 256) { \ /* Trail overflow */\ goto trail_overflow;\ }\ if (HR + 1024 > ASP) { \ goto global_overflow;\ }\ - reset:\ - to_visit0 = Realloc(to_visit0,auxsz); \ -pt0 = pt0_; pt0_end = pt0_end_; \ -to_visit = to_visit0, \ - to_visit_max = to_visit + auxsz/sizeof(struct non_single_struct_t);\ - \ while (to_visit >= to_visit0) { \ CELL d0; \ CELL *ptd0; \ @@ -193,7 +188,8 @@ aux_overflow : { \ } \ clean_tr(TR0 PASS_REGS); \ auxsz += auxsz;\ - goto reset; } + pop_text_stack(lvl); \ + goto reset; } #define def_trail_overflow() \ trail_overflow: { \ @@ -221,6 +217,7 @@ global_overflow : { \ *ptd0 = to_visit->d0; \ } \ clean_tr(TR0 PASS_REGS); \ + pop_text_stack(lvl); \ HR = InitialH; \ LOCAL_Error_TYPE = RESOURCE_ERROR_STACK; \ size_t expand = 0L; \ @@ -228,7 +225,7 @@ global_overflow : { \ Yap_ThrowError(RESOURCE_ERROR_STACK, TermNil, sizeof(CELL)*(HR-H0)); \ return false;\ }\ - goto reset;\ + goto reset;\ } #define CYC_LIST \ @@ -261,6 +258,13 @@ if (IS_VISIT_MARKER) { \ @brief routine to locate all variables in a term, and its applications */ static Term cyclic_complex_term(CELL *pt0_, CELL *pt0_end_ USES_REGS) { + CELL *pt0, *pt0_end; + int lvl; + size_t auxsz = 1024 * sizeof(struct non_single_struct_t); + struct non_single_struct_t *to_visit0, *to_visit,* to_visit_max; + CELL *InitialH = HR; + tr_fr_ptr TR0 = TR; + WALK_COMPLEX_TERM__(CYC_LIST, CYC_APPL, {}); /* leave an empty slot to fill in later */ END_WALK(); @@ -308,21 +312,22 @@ static Term BREAK_LOOP(CELL d0,struct non_single_struct_t *to_visit ) { static int cycles_in_complex_term( CELL *pt0_, CELL *pt0_end_ USES_REGS) { CELL *pt0, *pt0_end; - int lvl = push_text_stack(); size_t auxsz = 1024 * sizeof(struct non_single_struct_t); - struct non_single_struct_t *to_visit0=NULL, *to_visit, *to_visit_max; + struct non_single_struct_t *to_visit0, *to_visit, *to_visit_max; + int lvl; + + reset: + lvl = push_text_stack(); + pt0 = pt0_, pt0_end = pt0_end_; + to_visit0 = Malloc(auxsz); + to_visit= to_visit0; + to_visit_max = to_visit0 + auxsz/sizeof(struct non_single_struct_t); CELL *InitialH = HR; tr_fr_ptr TR0 = TR; if (TR > (tr_fr_ptr)LOCAL_TrailTop - 256) { \ /* Trail overflow */\ goto trail_overflow;\ }\ - - reset: - pt0 = pt0_, pt0_end = pt0_end_; - to_visit0 = Realloc(to_visit0,auxsz); - to_visit= to_visit0; - to_visit_max = to_visit0 + auxsz/sizeof(struct non_single_struct_t); auxsz *= 2; int rc = 0; CELL *ptf; @@ -467,6 +472,12 @@ static Int cycles_in_term(USES_REGS1) /* cyclic_term(+T) */ @brief routine to locate all variables in a term, and its applications */ static bool ground_complex_term(CELL * pt0_, CELL * pt0_end_ USES_REGS) { + CELL *pt0, *pt0_end; + int lvl; + size_t auxsz = 1024 * sizeof(struct non_single_struct_t); + struct non_single_struct_t *to_visit0, *to_visit,* to_visit_max; + CELL *InitialH = HR; + tr_fr_ptr TR0 = TR; WALK_COMPLEX_TERM(); /* leave an empty slot to fill in later */ @@ -515,6 +526,12 @@ static Int ground(USES_REGS1) /* ground(+T) */ static Int var_in_complex_term(CELL *pt0_, CELL *pt0_end_ , Term v USES_REGS) { + CELL *pt0, *pt0_end; + int lvl; + size_t auxsz = 1024 * sizeof(struct non_single_struct_t); + struct non_single_struct_t *to_visit0, *to_visit,* to_visit_max; + CELL *InitialH = HR; + tr_fr_ptr TR0 = TR; WALK_COMPLEX_TERM(); @@ -596,7 +613,14 @@ static Term vars_in_complex_term(CELL *pt0_, CELL *pt0_end_ , } CELL output = AbsPair(HR); - WALK_COMPLEX_TERM(); + CELL *pt0, *pt0_end; + int lvl; + size_t auxsz = 1024 * sizeof(struct non_single_struct_t); + struct non_single_struct_t *to_visit0, *to_visit,* to_visit_max; + CELL *InitialH = HR; + tr_fr_ptr TR0 = TR; + + WALK_COMPLEX_TERM(); /* do or pt2 are unbound */ if (HR + 1024 > ASP) { @@ -756,6 +780,12 @@ typedef struct att_rec { static Term attvars_in_complex_term( CELL *pt0_, CELL *pt0_end_ , Term inp USES_REGS) { + CELL *pt0, *pt0_end; + int lvl; + size_t auxsz = 1024 * sizeof(struct non_single_struct_t); + struct non_single_struct_t *to_visit0, *to_visit,* to_visit_max; + CELL *InitialH = HR; + tr_fr_ptr TR0 = TR; CELL output = inp; WALK_COMPLEX_TERM(); if (IsAttVar(ptd0)) { @@ -821,27 +851,36 @@ static Int term_attvars(USES_REGS1) /* variables in term t */ */ static Term new_vars_in_complex_term( CELL *pt0_, CELL *pt0_end_ , Term inp USES_REGS) { + CELL *pt0, *pt0_end; + int lvl; + size_t auxsz = 1024 * sizeof(struct non_single_struct_t); + struct non_single_struct_t *to_visit0, *to_visit,* to_visit_max; + CELL *InitialH = HR; + tr_fr_ptr TR0 = TR; Int n=0; CELL output = TermNil; - { - int lvl = push_text_stack(); + Term inp0 = inp; + while (!IsVarTerm(inp) && IsPairTerm(inp)) { Term t = HeadOfTerm(inp); if (IsVarTerm(t)) { n++; - TrailTerm(TR++) = t; - *VarOfTerm(t) = TermFoundVar; - if ((tr_fr_ptr)LOCAL_TrailTop - TR < 1024) { - - if (!Yap_growtrail(n * sizeof(tr_fr_ptr *), true)) { - goto trail_overflow; - } - } - } - inp = TailOfTerm(inp); - } - pop_text_stack(lvl); - } + TrailTerm(TR++) = t; + *VarOfTerm(t) = TermFoundVar; + if ((tr_fr_ptr)LOCAL_TrailTop - TR < 1024) { + size_t expand = (tr_fr_ptr)LOCAL_TrailTop - TR; + clean_tr(TR0 PASS_REGS); + *HR++ = inp0; + /* Trail overflow */ + if (!Yap_growtrail(expand, false)) { + Yap_ThrowError(RESOURCE_ERROR_TRAIL, TermNil, expand); + } + inp = *--HR; + continue; + } + inp = TailOfTerm(inp); + } + } WALK_COMPLEX_TERM(); output = MkPairTerm((CELL)ptd0, output); TrailTerm(TR++) = *ptd0; @@ -904,6 +943,12 @@ static Term vars_within_complex_term( CELL *pt0_, CELL *pt0_end_, Term inp USES_REGS) { Int n=0; CELL output = AbsPair(HR); + CELL *pt0, *pt0_end; + int lvl; + size_t auxsz = 1024 * sizeof(struct non_single_struct_t); + struct non_single_struct_t *to_visit0, *to_visit,* to_visit_max; + CELL *InitialH = HR; + tr_fr_ptr TR0 = TR; while (!IsVarTerm(inp) && IsPairTerm(inp)) { Term t = HeadOfTerm(inp); @@ -1012,6 +1057,12 @@ return Yap_unify(ARG2, t) && Yap_unify(ARG3, out); static Term non_singletons_in_complex_term(CELL * pt0_, CELL * pt0_end_ USES_REGS) { + CELL *pt0, *pt0_end; + int lvl; + size_t auxsz = 1024 * sizeof(struct non_single_struct_t); + struct non_single_struct_t *to_visit0, *to_visit,* to_visit_max; + CELL *InitialH = HR; + tr_fr_ptr TR0 = TR; WALK_COMPLEX_TERM__({}, {}, FOUND_VAR_AGAIN()); /* do or pt2 are unbound */ @@ -1080,6 +1131,12 @@ if (singles) { \ static Int numbervars_in_complex_term(CELL * pt0_, CELL * pt0_end_, Int numbv, int singles USES_REGS) { + CELL *pt0, *pt0_end; + int lvl; + size_t auxsz = 1024 * sizeof(struct non_single_struct_t); + struct non_single_struct_t *to_visit0, *to_visit,* to_visit_max; + CELL *InitialH = HR; + tr_fr_ptr TR0 = TR; WALK_COMPLEX_TERM__({}, {}, {}); @@ -1162,6 +1219,12 @@ if (FunctorOfTerm(d0) == FunctorDollarVar) { \ static int max_numbered_var(CELL * pt0_, CELL * pt0_end_, Int * maxp USES_REGS) { + CELL *pt0, *pt0_end; + int lvl; + size_t auxsz = 1024 * sizeof(struct non_single_struct_t); + struct non_single_struct_t *to_visit0, *to_visit,* to_visit_max; + CELL *InitialH = HR; + tr_fr_ptr TR0 = TR; WALK_COMPLEX_TERM__({}, MAX_NUMBERED, {}); END_WALK(); diff --git a/C/text.c b/C/text.c index f7effd524..737ae5a88 100644 --- a/C/text.c +++ b/C/text.c @@ -195,7 +195,8 @@ void *Realloc(void *pt, size_t sz USES_REGS) { if (!pt) return Malloc(sz PASS_REGS); old--; - sz = ALIGN_BY_TYPE(sz + sizeof(struct mblock), Yap_Max(CELLSIZE,sizeof(struct mblock))); + sz = ALIGN_BY_TYPE(sz, Yap_Max(CELLSIZE,sizeof(struct mblock))); + sz += 2*sizeof(struct mblock); o = realloc(old, sz); if (o->next) { o->next->prev = o; diff --git a/C/utilpreds.c b/C/utilpreds.c index 153f8abbb..1c6b52e15 100644 --- a/C/utilpreds.c +++ b/C/utilpreds.c @@ -151,7 +151,8 @@ clean_complex_tr(tr_fr_ptr TR0 USES_REGS) { #define expand_stack(S0,SP,SF,TYPE) \ { size_t sz = SF-S0, used = SP-S0; \ - S0 = Realloc(S0, (1024+sz)*sizeof(TYPE) PASS_REGS); \ + sz += 1024;\ + S0 = Realloc(S0, sz*sizeof(TYPE) PASS_REGS); \ SP = S0+used; SF = S0+sz; } #define MIN_ARENA_SIZE (1048L) @@ -206,6 +207,8 @@ int Yap_copy_complex_term(register CELL *pt0, register CELL *pt0_end, to_visit++; // move to new list d0 = *headp; + if ((ADDR)TR > LOCAL_TrailTop - MIN_ARENA_SIZE) + goto trail_overflow; TrailedMaBind(headp, AbsPair(HR)); pt0 = headp; pt0_end = headp + 1; @@ -305,6 +308,8 @@ int Yap_copy_complex_term(register CELL *pt0, register CELL *pt0_end, if (++to_visit >= to_visit_max-32) { expand_stack(to_visit0, to_visit, to_visit_max, struct cp_frame); } + if ((ADDR)TR > LOCAL_TrailTop - MIN_ARENA_SIZE) + goto trail_overflow; TrailedMaBind(headp,AbsAppl(HR)); ptf = HR; *ptf++ = (CELL)f; @@ -344,18 +349,14 @@ int Yap_copy_complex_term(register CELL *pt0, register CELL *pt0_end, } to_visit = bp; new = *ptf; - if (TR > (tr_fr_ptr)LOCAL_TrailTop - 256) { - /* Trail overflow */ - if (!Yap_growtrail((TR - TR0) * sizeof(tr_fr_ptr *), TRUE)) { - goto trail_overflow; - } - } + if ((ADDR)TR > LOCAL_TrailTop - MIN_ARENA_SIZE) + goto trail_overflow; TrailedMaBind(ptd0, new); ptf++; } else { /* first time we met this term */ RESET_VARIABLE(ptf); - if ((ADDR)TR > LOCAL_TrailTop - MIN_ARENA_SIZE) + if ((ADDR)TR > LOCAL_TrailTop - MIN_ARENA_SIZE) goto trail_overflow; TrailedMaBind(ptd0, (CELL)ptf); ptf++;