Re: getcontext/setcontext
[email]jerry.jeremiah@gmail.com[/email] writes:
[color=blue]
> I can't figure out why, but these functions don't work the way I
> expect.[/color]
The context includes current stack pointer. As soon as you return
from call_cc(), the context that you stored becomes invalid -- it
refers to stack locations that are "no longer there" -- have been
popped off the stack. Calling setcontext() on such context invokes
undefined behavior.
You may use the context in the function that called getcontext(),
or in any functions called from it; but never after the caller of
getcontext() returns.
Cheers,
--
In order to understand recursion you must first understand recursion.
Remove /-nsp/ for email.
Re: getcontext/setcontext
[email]jerry.jeremiah@gmail.com[/email] writes:[color=blue]
>I can't figure out why, but these functions don't work the way I
>expect. What happens is that it gets the context fine in the call_cc
>function and then it sets the context in the apply function just fine
>- I know this because of the second "getcontext returned..." message.
>But then instead of returning from call_cc just before the "main:
>i=..." message, call_cc returnes at the point that apply was called -
>and the "error! i=..." message is printed and the program ends. Why
>is it doing something I don't expect?[/color]
The compiler is keeping 'i' in a register. When you return from
the getcontext the second time, it returns the value of 'i' to the
original saved value.
scott
Re: getcontext/setcontext
On Oct 26, 6:10 pm, Paul Pluzhnikov <ppluzhnikov-...@charter.net>
wrote:[color=blue]
> jerry.jerem...@gmail.com writes:[color=green]
> > I can't figure out why, but these functions don't work the way I
> > expect.[/color]
>
> The context includes current stack pointer. As soon as you return
> from call_cc(), the context that you stored becomes invalid -- it
> refers to stack locations that are "no longer there" -- have been
> popped off the stack. Calling setcontext() on such context invokes
> undefined behavior.
>
> You may use the context in the function that called getcontext(),
> or in any functions called from it; but never after the caller of
> getcontext() returns.
>
> Cheers,[/color]
I thought getcontext stored the entire stack so that all stack
variables were saved. I figured this meant the stack (including the
correct return address of call_cc would be restored. If it only
stores the stack pointer then it isn't much more useful than lngjmp...
Jerry
Re: getcontext/setcontext
[email]jerry.jeremiah@gmail.com[/email] writes:
[color=blue]
> I thought getcontext stored the entire stack so that all stack
> variables were saved.[/color]
The wording in the man page could probably use some improvement.
It says
The ucontext_t type that ucp points to defines the user context and
includes the contents of the calling process' machine registers,
the signal mask, and the current execution stack
[color=blue]
> I figured this meant the stack (including the
> correct return address of call_cc would be restored.[/color]
That would require storing a copy of the *entire* stack (possibly
many MBytes). Where do you suppose storage for that copy comes from?
In theory, getcontext could malloc() that memory, but then this
sequence would leak it:
getcontext(&ctx); getcontext(&ctx);
[color=blue]
> If it only
> stores the stack pointer then it isn't much more useful than lngjmp...[/color]
That's right, it isn't.
Most of getcontext() utility comes from makecontext(), which provides
for machine-independent way to write coroutines.
Cheers,
--
In order to understand recursion you must first understand recursion.
Remove /-nsp/ for email.
Re: getcontext/setcontext
Thanks for the help.
I was trying to amke a "call with current continuation" function so I
was hoping it made a copy of the entire stack.
C'est la voie.
Jerry
On Oct 29, 5:30 pm, Paul Pluzhnikov <ppluzhnikov-...@charter.net>
wrote:[color=blue]
> jerry.jerem...@gmail.com writes:[color=green]
> > I thought getcontext stored the entire stack so that all stack
> > variables were saved.[/color]
>
> The wording in the man page could probably use some improvement.
> It says
>
> The ucontext_t type that ucp points to defines the user context and
> includes the contents of the calling process' machine registers,
> the signal mask, and the current execution stack
>[color=green]
> > I figured this meant the stack (including the
> > correct return address of call_cc) would be restored.[/color]
>
> That would require storing a copy of the *entire* stack (possibly
> many MBytes). Where do you suppose storage for that copy comes from?
>
> In theory, getcontext could malloc() that memory, but then this
> sequence would leak it:
>
> getcontext(&ctx); getcontext(&ctx);
>[color=green]
> > If it only
> > stores the stack pointer then it isn't much more useful than lngjmp...[/color]
>
> That's right, it isn't.
>
> Most of getcontext() utility comes from makecontext(), which provides
> for machine-independent way to write coroutines.
>
> Cheers,
> --
> In order to understand recursion you must first understand recursion.
> Remove /-nsp/ for email.[/color]