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 2 1414
In article <aa************ **************@ posting.google. com>,
Jerald Fijerald <jf*@freemail.g r> 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
Dave Vandervies wrote: In article <aa************ **************@ posting.google. com>, Jerald Fijerald <jf*@freemail.g r> 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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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.
|
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 real-life
code snippet would surely help.
Regards,
Nimmi
|
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 consistenly, or something
that might pull in exotic text segments, etc.
Specifically I have a hairly algorithm loop that uses what is currently
a macro V. Here's a snipplet:
for (k = d; k >= -d; k -= 2) {
if (k == -d || (k != d && V(fwd, m, k - 1)...
|
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 these would be real helping.
Also, what happens when one jumps from one process/task to other, even though
it is OT can anybody clear this concept.
|
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 setjmp/longjmp, but I am somewhat lost
in the detail of how to actually implement this in assembler. (My compiler
supports in-line assembler which I hope will prove usefull). I will be very
gratefull for any help or pointers with this problem. I am...
| |
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)
{
|
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 type='text/javascript'>
function myFunc(lev) {
// if (lev) return myFunc(lev-1);
var aStack=;
nextFunc = arguments.callee;
|
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 automatic non volatile variables of the function
invoking setjmp, these will be undefined if modified after the setjmp
call"
|
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 bastard so posting
it before I forgot again...
By taking this minimum code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html>
<head>
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
| |
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |