fix slot leaks

also moe errot gandking
This commit is contained in:
Vítor Santos Costa 2015-10-09 10:31:07 +01:00
parent d5c21324a4
commit bc2dabbca5
4 changed files with 37 additions and 11 deletions

View File

@ -1048,7 +1048,7 @@ Term Yap_Parse(UInt prio) {
CACHE_REGS
Volatile Term t;
JMPBUFF FailBuff;
yhandle_t sls = Yap_CurrentSlot(PASS_REGS1);
yhandle_t sls = Yap_StartSlots();
if (!sigsetjmp(FailBuff.JmpBuff, 0)) {

View File

@ -67,6 +67,7 @@ static inline void Yap_RebootSlots__(int wid USES_REGS) {
/// @brief declares a new set of slots.
/// Used to tell how many slots we had when we entered a segment of code.
//#define Yap_StartSlots() ( printf("[<<<%s,%s,%d-%ld\n",__FILE__,__FUNCTION__,__LINE__,LOCAL_CurSlot)?Yap_StartSlots__(PASS_REGS1): -1)
#define Yap_StartSlots() Yap_StartSlots__(PASS_REGS1)
static inline yhandle_t Yap_StartSlots__(USES_REGS1) {
@ -78,7 +79,9 @@ static inline yhandle_t Yap_StartSlots__(USES_REGS1) {
return LOCAL_CurSlot;
}
/// @brief reset slots to a well-known position in the stack
//#define Yap_CloseSlots(slot) ( printf("- %s,%s,%d %ld>>>]\n",__FILE__,__FUNCTION__,__LINE__, slot)?Yap_CloseSlots__(slot PASS_REGS):-1)
#define Yap_CloseSlots(slot) Yap_CloseSlots__(slot PASS_REGS)
static inline void Yap_CloseSlots__(yhandle_t slot USES_REGS) {
@ -143,9 +146,10 @@ static inline void ensure_slots(int N USES_REGS) {
}
}
/// @brief create a new slot with term t
//#define Yap_InitSlot(t) ( printf("+%d %s,%s,%d>>>]\n",1,__FILE__,__FUNCTION__,__LINE__)?Yap_InitSlot__(t PASS_REGS):-1)
#define Yap_InitSlot(t) Yap_InitSlot__(t PASS_REGS)
/// @brief create a new slot with term t
static inline yhandle_t Yap_InitSlot__(Term t USES_REGS) {
yhandle_t old_slots = LOCAL_CurSlot;
// fprintf(stderr,"IS %s:%d\n", __FUNCTION__, __LINE__);
@ -156,6 +160,7 @@ static inline yhandle_t Yap_InitSlot__(Term t USES_REGS) {
return old_slots;
}
//#define Yap_NewSlots(n) ( printf("+%d %s,%s,%d>>>]\n",n,__FILE__,__FUNCTION__,__LINE__)?Yap_NewSlots__(n PASS_REGS):-1)
#define Yap_NewSlots(n) Yap_NewSlots__(n PASS_REGS)
/// @brief allocate n empty new slots
@ -172,6 +177,7 @@ static inline yhandle_t Yap_NewSlots__(int n USES_REGS) {
return old_slots;
}
//#define Yap_InitSlots(n, ts) ( printf("+%d %s,%s,%d>>>]\n",n,__FILE__,__FUNCTION__,__LINE__)?Yap_InitSlots__(n, ts PASS_REGS):-1)
#define Yap_InitSlots(n, ts) Yap_InitSlots__(n, ts PASS_REGS)
/// @brief create n new slots with terms ts[]
@ -190,15 +196,16 @@ static inline yhandle_t Yap_InitSlots__(int n, Term *ts USES_REGS) {
/// @brief Succeeds if it is to recover the space allocated for $n$ contiguos
/// slots starting at topSlot.
static inline bool Yap_RecoverSlots(int n, yhandle_t topSlot USES_REGS) {
if (topSlot + n < LOCAL_CurSlot)
if (topSlot+n < LOCAL_CurSlot)
return false;
// fprintf(stderr,"RS %s:%d\n", __FUNCTION__, __LINE__);
#ifdef DEBUG
if (topSlot + n > LOCAL_CurSlot) {
if (n > LOCAL_CurSlot) {
Yap_Error(SYSTEM_ERROR_INTERNAL, 0 , "Inconsistent slot state in Yap_RecoverSlots.", 0);
return false;
}
#endif
LOCAL_CurSlot -= n;
// fprintf(stderr,"RS %s:%d\n", __FUNCTION__, __LINE__);
return true;
}

View File

@ -35,8 +35,9 @@ BEGIN_ERRORS()
E(DOMAIN_ERROR_OUT_OF_RANGE, DOMAIN_ERROR, "out_of_range")
E(DOMAIN_ERROR_OPERATOR_PRIORITY, DOMAIN_ERROR, "operator_priority")
E(DOMAIN_ERROR_OPERATOR_SPECIFIER, DOMAIN_ERROR, "operator_specifier")
E(DOMAIN_ERROR_RADIX, DOMAIN_ERROR, "radix")
E(DOMAIN_ERROR_PROLOG_FLAG, DOMAIN_ERROR, "prolog_flag")
E(DOMAIN_ERROR_RADIX, DOMAIN_ERROR, "radix")
E(DOMAIN_ERROR_READ_OPTION, DOMAIN_ERROR, "read_option")
E(DOMAIN_ERROR_SHIFT_COUNT_OVERFLOW, DOMAIN_ERROR, "shift_count_overflow")
E(DOMAIN_ERROR_SOURCE_SINK, DOMAIN_ERROR, "source_sink")
E(DOMAIN_ERROR_SOLUTIONS, DOMAIN_ERROR, "solutions")

View File

@ -620,6 +620,14 @@ static parser_state_t initParser(Term opts, FEnv *fe, REnv *re, int inp_stream,
fe->nargs = nargs;
fe->args = setReadEnv(opts, fe, re, inp_stream);
}
if (fe->args == NULL) {
if (LOCAL_Error_TYPE == DOMAIN_ERROR_OUT_OF_RANGE)
LOCAL_Error_TYPE = DOMAIN_ERROR_READ_OPTION;
if (LOCAL_Error_TYPE)
Yap_Error(LOCAL_Error_TYPE, LOCAL_Error_Term, NULL);
fe->t = 0;
return YAP_PARSING_FINISHED;;
}
if (GLOBAL_Stream[inp_stream].status & Push_Eof_Stream_f) {
fe->t = MkAtomTerm(AtomEof);
GLOBAL_Stream[inp_stream].status &= ~Push_Eof_Stream_f;
@ -790,7 +798,9 @@ static Int
yhandle_t h = Yap_InitSlot(ARG1);
if ((rc = Yap_read_term(LOCAL_c_input_stream, ARG2, 2)) == 0)
return FALSE;
return Yap_unify(Yap_GetFromSlot(h), rc);
Term tf = Yap_GetFromSlot(h);
Yap_RecoverSlots(1, h PASS_REGS);
return Yap_unify(tf, rc);
}
static Int read_term(
@ -806,7 +816,9 @@ static Int read_term(
}
out = Yap_read_term(inp_stream, ARG3, 3);
UNLOCK(GLOBAL_Stream[inp_stream].streamlock);
return out != 0L && Yap_unify(Yap_GetFromSlot(h), out);
Term tf = Yap_GetFromSlot(h);
Yap_RecoverSlots(1, h PASS_REGS);
return out != 0L && Yap_unify(tf, out);
}
#define READ_CLAUSE_DEFS() \
@ -952,7 +964,9 @@ static Int read_clause2(USES_REGS1) {
Term rc;
yhandle_t h = Yap_InitSlot(ARG1);
rc = Yap_read_term(LOCAL_c_input_stream, Deref(ARG2), -2);
return rc && Yap_unify(Yap_GetFromSlot(h), rc);
Term tf = Yap_GetFromSlot(h);
Yap_RecoverSlots(1, h PASS_REGS);
return rc && Yap_unify(tf, rc);
}
/**
@ -986,7 +1000,9 @@ static Int read_clause(
inp_stream = Yap_CheckTextStream(ARG1, Input_Stream_f, "read/3");
out = Yap_read_term(inp_stream, t3, -3);
UNLOCK(GLOBAL_Stream[inp_stream].streamlock);
return out && Yap_unify(Yap_GetFromSlot(h), out);
Term tf = Yap_GetFromSlot(h);
Yap_RecoverSlots(1, h PASS_REGS);
return out && Yap_unify(tf, out);
}
/**
@ -1139,7 +1155,9 @@ Term Yap_StringToTerm(const char *s, size_t len, encoding_t *encp, int prio,
UNLOCK(GLOBAL_Stream[stream].streamlock);
if (rval && bindings) {
*bindings = Yap_GetFromSlot(sl);
Yap_RecoverSlots(sl, 1 PASS_REGS);
}
if (bindings) {
Yap_RecoverSlots(sl, 1 PASS_REGS);
}
return rval;
}