Hello,
I am trying to implement setjmp for functions.
Code:
#include <setjmp.h>
#include <stdio.h>
jmp_buf arr[3];
void DISP(int x , int i)
{
if(!setjmp(arr[i]) ) { printf("FUNC SAVED!\n"); return; }
printf("Func Continues here for buf : %d\n", i);
printf(" x :%d at buf : %d\n", x , i);
}
int main(void)
{
int value;
int rerun = 0;
if(!rerun)
{
value = 3;
DISP(0, 0);
}
if(!rerun)
{
value = 2;
DISP(1, 1);
}
if(!rerun)
{
value = 1;
DISP(2, 2);
}
rerun = 1;
longjmp(arr[0], 1);
}
Does setjmp not save any of the parameter information?
Also, I found this article on setjmp implemented coroutines, it seems you need to allocated memory via an array :
http://fanf.livejournal.com/105413.html
Code:
void *cogo(jmp_buf here, void (*fun)(void*), void *arg) {
if (tos == NULL) tos = (char*)&arg;
tos += STACKDIR STACKSIZE;
char n[STACKDIR (tos - (char*)&arg)];
coarg = n; // ensure optimizer keeps n
if (setjmp(here)) return(coarg);
fun(arg);
abort();
}
Could I possibly use malloc to allocate memory for the stack too?
thank you!