Stack loss in calling Yap from C

View: New views
2 Messages — Rating Filter:   Alert me  

Stack loss in calling Yap from C

by Tony Wilson-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

When setting up the goal, stack space is used and is non-recoverable as
the stack boundary is only taken at the point of YAP_EnterGoal. This
might not normally be a problem, but for the fact that garbage
collection also seems to cause a crash if it occurs in a Enter/LeaveGoal
callback. Surrounding the whole thing with a dummy call stops the stack
loss, but obviously is not a serious solution. May I suggest a
YAP_PrepareGoal or something similar.

Regards,
Tony
Durban, South Africa

#define STACK_RECOVER_FIX

#ifdef STACK_RECOVER_FIX
    YAP_dogoalinfo DUMMY;
    YAP_EnterGoal( YAP_AtomToPred(YAP_LookupAtom("true")), 0, &DUMMY );
#endif
    YAP_Term goalArgs[arity+ret];

    for(int i = 0; i < arity; i++)
        goalArgs[i] = YAP_MkIntTerm(A[i]);

    if(ret) {
        goalArgs[arity] = YAP_MkVarTerm;
        sl = YAP_InitSlot(goalArgs[arity]);
    }

    YAP_dogoalinfo goalInfo;
    YAP_PredEntryPtr goalPred;

    if( arity+ret == 0)
        goalPred = YAP_AtomToPred(YAP_LookupAtom(name));
    else {
        YAP_Term g =
YAP_MkApplTerm(YAP_MkFunctor(YAP_LookupAtom(name),arity+ret), arity+ret,
goalArgs);
        YAP_Functor goalFunctor = YAP_FunctorOfTerm(g);
        goalPred = YAP_FunctorToPred(goalFunctor);
    }
    result = YAP_EnterGoal( goalPred, goalArgs, &goalInfo );    // STACK
fence

    if(ret) {
        ... = YAP_GetFromSlot(sl);
        YAP_RecoverSlots(1);
    }
    YAP_LeaveGoal(TRUE, &goalInfo);   // STACK recover
#ifdef STACK_RECOVER_FIX
    YAP_LeaveGoal(TRUE, &DUMMY);
#endif


------------------------------------------------------------------------------
Check out the new SourceForge.net Marketplace.
It is the best place to buy or sell services for
just about anything Open Source.
http://p.sf.net/sfu/Xq1LFB
_______________________________________________
Yap-users mailing list
Yap-users@...
https://lists.sourceforge.net/lists/listinfo/yap-users

Re: Stack loss in calling Yap from C

by Vitor Santos Costa-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi Tony

Sorry for taking so long to answer...

If I understand right, your concern is that the YAP_MkSlots are using
up space which is never recovered.

Did you try swapping the calls

   if(ret) {
       ... = YAP_GetFromSlot(sl);
       YAP_RecoverSlots(1);
   }
   YAP_LeaveGoal(TRUE, &goalInfo);   // STACK recover

by

   YAP_LeaveGoal(TRUE, &goalInfo);   // STACK recover
   if(ret) {
       ... = YAP_GetFromSlot(sl);
       YAP_RecoverSlots(1);
   }

The idea is that the slots follow a stack discipline, so if you
allocate first you should only recover last.

Sorry if I misunderstood :(

Cheers

Vitor


On Wed, Jan 7, 2009 at 10:49 PM, Tony Wilson <soduktile@...> wrote:

> When setting up the goal, stack space is used and is non-recoverable as
> the stack boundary is only taken at the point of YAP_EnterGoal. This
> might not normally be a problem, but for the fact that garbage
> collection also seems to cause a crash if it occurs in a Enter/LeaveGoal
> callback. Surrounding the whole thing with a dummy call stops the stack
> loss, but obviously is not a serious solution. May I suggest a
> YAP_PrepareGoal or something similar.
>
> Regards,
> Tony
> Durban, South Africa
>
> #define STACK_RECOVER_FIX
>
> #ifdef STACK_RECOVER_FIX
>    YAP_dogoalinfo DUMMY;
>    YAP_EnterGoal( YAP_AtomToPred(YAP_LookupAtom("true")), 0, &DUMMY );
> #endif
>    YAP_Term goalArgs[arity+ret];
>
>    for(int i = 0; i < arity; i++)
>        goalArgs[i] = YAP_MkIntTerm(A[i]);
>
>    if(ret) {
>        goalArgs[arity] = YAP_MkVarTerm;
>        sl = YAP_InitSlot(goalArgs[arity]);
>    }
>
>    YAP_dogoalinfo goalInfo;
>    YAP_PredEntryPtr goalPred;
>
>    if( arity+ret == 0)
>        goalPred = YAP_AtomToPred(YAP_LookupAtom(name));
>    else {
>        YAP_Term g =
> YAP_MkApplTerm(YAP_MkFunctor(YAP_LookupAtom(name),arity+ret), arity+ret,
> goalArgs);
>        YAP_Functor goalFunctor = YAP_FunctorOfTerm(g);
>        goalPred = YAP_FunctorToPred(goalFunctor);
>    }
>    result = YAP_EnterGoal( goalPred, goalArgs, &goalInfo );    // STACK
> fence
>
>    if(ret) {
>        ... = YAP_GetFromSlot(sl);
>        YAP_RecoverSlots(1);
>    }
>    YAP_LeaveGoal(TRUE, &goalInfo);   // STACK recover
> #ifdef STACK_RECOVER_FIX
>    YAP_LeaveGoal(TRUE, &DUMMY);
> #endif
>
>
> ------------------------------------------------------------------------------
> Check out the new SourceForge.net Marketplace.
> It is the best place to buy or sell services for
> just about anything Open Source.
> http://p.sf.net/sfu/Xq1LFB
> _______________________________________________
> Yap-users mailing list
> Yap-users@...
> https://lists.sourceforge.net/lists/listinfo/yap-users
>

------------------------------------------------------------------------------
This SF.net email is sponsored by:
SourcForge Community
SourceForge wants to tell your story.
http://p.sf.net/sfu/sf-spreadtheword
_______________________________________________
Yap-users mailing list
Yap-users@...
https://lists.sourceforge.net/lists/listinfo/yap-users