🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c
Article #3845

unrestricted longjmp

#3845
From: David Adrien Tanguay
Date: Thu, 24 Aug 1995 14:49
43 lines
2286 bytes
I just read an article in Aug 95 Software Practice & Experience
by K. Helsgaun. It describes Cback, a little package to add backtracking
to C. One of the tricks needed to implement it is stack copying coupled
with setjmp/longjmp, i.e., implementing continuations in C. It's not
strictly conforming, but it is fairly portable.

This is about the dozenth time I've seen such code. Since I rather like
the idea, I am considering creating a proposal to add a more generalised
long jump. What follows is a brief sketch, fishing for comments or
condemnation. It's in the form of a <stkjmp.h>, or something to add to
<setjmp.h>. Don't sweat the names, they're just the first that came to mind.

typedef struct {
    int  status;
    long value;
    /* other implementation defined __* fields, notably a pointer to
       the saved stack copy */
} stk_t;
enum {
    STK_SET,             /* the stack save was properly set */
    STK_RETURN,          /* returning via a stkjump */
    STK_ERR_BUFFER_SIZE, /* supplied buffer is too small */
    STK_ERR_MALLOC,      /* malloc for stack save failed */
    STK_number
} status;

stk_t stkset( void* stack_copy, size_t buf_size );
/*  Like setjmp. The stack_copy will hold a copy of the contents of the
    stack, and buf_size is the size of stack_copy. If stack_copy is NULL,
    malloc() is called to create a buffer of a sufficient size. The struct
    returned contains a status, and a user supplied value when
    status == STK_RETURN. */

void stkjump( stk_t stkbuf, long value );
/*  Like longjmp. Restore the stack, as saved in stkbuf, and return value
    to the execution point saved in stkbuf. */

size_t stksize( void );
/*  Return the size needed for stack_buf if stkset were to be called from
    anywhere in the enclosing function (block?). */
-- 
David Tanguay       dat@Thinkage.on.ca       http://www.thinkage.on.ca/~dat/
Thinkage, Ltd.           Kitchener, Ontario, Canada          [43.24N 80.29W]
Technical Details
Message-ID: <DDtL6x.M1s@thinkage.on.ca>
Path: arc1-ca.pugleaf.net!thinkage.on.ca!dat

Show Full Headers
Complete Article Headers
Newsgroups: comp.std.c
Path: arc1-ca.pugleaf.net!thinkage.on.ca!dat
From: dat@thinkage.on.ca (David Adrien Tanguay)
Subject: unrestricted longjmp
Message-ID: <DDtL6x.M1s@thinkage.on.ca>
Sender: news@thinkage.on.ca
Organization: Thinkage Ltd., Kitchener, Ontario
Date: Thu, 24 Aug 1995 14:49:45 GMT