473,321 Members | 1,669 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,321 software developers and data experts.

longjmp to the callee

Hello.

I'm trying to find an elegant way to longjmp to the callee
(aka "generators for C")

So far I've come to this experimental program (gcc only as it uses
__builtin_frame_address, although we can do without it):

================================================== ====================
/* longjmp to the callee */

#include <setjmp.h>
#include <stdio.h>

jmp_buf F;
jmp_buf F2;

static inline char* bfr ()
{
return __builtin_frame_address (0);
}

char *top;

int foo()
{
int i;
for (i = 0; i < 5; i++) {
printf (".i=%i\n", i);
if (i == 2) {
if (!setjmp (F2)) {
top = bfr();
//printf ("Mytop=%p\n", top);
longjmp (F, 1);
} else {
printf ("resumed...\n");
}
}
printf ("i=%i\n", i);
}
return 0;
}

int bar ()
{
return foo ();
}

int potatoe ()
{
int i, j;
for (i = 0, j = 10; i < j; i++)
printf ("%i ", i);
printf ("\n");
return 132;
}

int main ()
{
int i, j;
char *thistop;
for (i = 0; i < 3; i++)
if (!setjmp (F)) {
printf ("trying...\n");
bar();
} else {
/* preserve the stack */
j=40;
char CAP1[j];
thistop = bfr();
char GAP2 [top-thistop];

printf ("caught...\n");

/* test that stack is not polluted */
potatoe ();

/* longjmp to the callee and resume */
longjmp (F2, 2);
}
return 0;
}
================================================== ===========

Notes:
longjmp already supports jumping to a callee. The problem
is that we have to preserve the stack (and thus the function
potatoe() which is supposed to polute it).
The solution I found is to use C99 variable-length arrays to
displace the stacktop beyond the region we want to preserve.

I'm looking for ideas/enhancements to make this more portable,
safe, fast, etc. It must have been done before, right?
jf
Nov 14 '05 #1
2 1393
In article <aa**************************@posting.google.com >,
Jerald Fijerald <jf*@freemail.gr> wrote:
Hello.

I'm trying to find an elegant way to longjmp to the callee
(aka "generators for C")


You can't. longjmp can only jump into stack[1] frames that are still
live.

If this is a "fun" project, you may have found an excuse to go learn
about functional languages that support continuations (which, at a first
glance, appears to be exactly what you're looking for).

If you can't change your tools, you might find something not entirely
unlike what you're looking for at
http://www.chiark.greenend.org.uk/~s...oroutines.html
dave

[1] That's the function-invocation stack, which need not be represented
with the "usual" block of memory that the hardware treats as a stack.
Just mentioning it so that the pedants don't jump on me for it.

--
Dave Vandervies dj******@csclub.uwaterloo.ca
Yes, I'm a middle-aged fogy. Forty years ago I was a young fogy.
In fifteen years or so, I'll be an old fogy.
--Mike Andrews in the scary devil monastery
Nov 14 '05 #2


Dave Vandervies wrote:
In article <aa**************************@posting.google.com >,
Jerald Fijerald <jf*@freemail.gr> wrote:
Hello.

I'm trying to find an elegant way to longjmp to the callee
(aka "generators for C")

You can't. longjmp can only jump into stack[1] frames that are still
live.

If this is a "fun" project, you may have found an excuse to go learn
about functional languages that support continuations (which, at a first
glance, appears to be exactly what you're looking for).

If you can't change your tools, you might find something not entirely
unlike what you're looking for at
http://www.chiark.greenend.org.uk/~s...oroutines.html
dave

[1] That's the function-invocation stack, which need not be represented
with the "usual" block of memory that the hardware treats as a stack.
Just mentioning it so that the pedants don't jump on me for it.


Then I guess we'll have to longjmp() on you for it.

--
Er*********@sun.com

Nov 14 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Thomas Baruchel | last post by:
Hi, wondering about: func1: setjmp() ; func2(); func2: {FILE *f; f = fopen(); func3(); fclose(f)} func3 : if() longjmp; else return; Note that FILE *fis a local variable in func2.
22
by: Nimmi Srivastav | last post by:
Can someone kindly clarify the distinction between long jumps and gotos? Why is one frowned upon and not the other? Is there really any situation where use of longjmp becomes inevitable? A...
12
by: Michael B Allen | last post by:
Should setjmp/longjmp really be used in a fast mundane ANSI C piece of code? Or is it frowned apon like goto? I have a need but I don't want to use something that is costly, isn't supported...
2
by: Ravi Uday | last post by:
Hi, Can anyone explain me why do we use setjmp and longjump functions. I read through the manual pages/doc but wasnt able to get a clear picture of the same. Any small example illustrating...
4
by: Jrferguson | last post by:
I have a C program that I am trying to port to a Motorola 68k based system. It makes use of setjmp and longjmp which are not supported by my C compiler. I understand the general principle behind...
8
by: Zheng Da | last post by:
I wrote a simple one as follow: typedef struct __myjmp_buf { int efp; int epc; }myjmp_buf; int mysetjmp(myjmp_buf env) {
9
by: Csaba Gabor | last post by:
Inside a function, I'd like to know the call stack. By this I mean that I'd like to know the function that called this one, that one's caller and so on. So I thought to do: <script...
6
by: Clausfor | last post by:
Hello, I have a problem with restoring variables in the setjmp/longjmp functions: K&R2 for longjmp says: "Accessible objects have the same value they had when longjmp was called, except for...
7
by: VK | last post by:
I was getting this effect N times but each time I was in rush to just make it work, and later I coudn't recall anymore what was the original state I was working around. This time I nailed the...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.