Changeset 91e22dc in mainline for uspace/lib/c/include/setjmp.h


Ignore:
Timestamp:
2013-11-15T08:39:36Z (10 years ago)
Author:
Vojtech Horky <vojtechhorky@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
6a5b999
Parents:
8797bae
Message:

setjmp() shall be a macro according to C standard

As a matter of fact, when it is a function, the context that was
saved may no longer exist. Excerpt from IRC follows:

vhotspur: jermar, hi: what makes you think setjmp needs to be macro
vhotspur: hmmmm, C99 states that setjmp is macro and longjmp is function,

POSIX states that setjmp can be either macro or function

jermar: from what we have learned about context_save over the years
jermar: you basically capture the stack pointer in context_save()
jermar: if it is a function, you immediately deallocate part of that stack
jermar: so later to return to something which has once been a stack,

but is not anymore

jermar: for instance, you could expect to find the return address from

the call to setjmp() on it, but that could have been overwritten
by some further use of the stack

Also fixed bad definition of buffer type.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/c/include/setjmp.h

    r8797bae r91e22dc  
    3131 * @{
    3232 */
    33 /** @file
     33/** @file Long jump implementation.
     34 *
     35 * Implementation inspired by Jiri Zarevucky's code from
     36 * http://bazaar.launchpad.net/~zarevucky-jiri/helenos/stdc/revision/1544/uspace/lib/posix/setjmp.h
    3437 */
    3538
     
    3740#define LIBC_SETJMP_H_
    3841
     42#include <libarch/fibril.h>
     43
     44struct jmp_buf_interal {
     45        context_t context;
     46        int return_value;
     47};
     48typedef struct jmp_buf_interal jmp_buf[1];
     49
    3950/*
    40  * We hide the structure to allow smooth inclusion from libposix
    41  * as no other types are necessary (and thus no includes are needed).
     51 * Specified as extern to minimize number of included headers
     52 * because this file is used as is in libposix too.
    4253 */
    43 struct jmp_buf_interal;
    44 typedef struct jmp_buf_interal *jmp_buf;
     54extern int context_save(context_t *ctx) __attribute__((returns_twice));
    4555
    46 extern int setjmp(jmp_buf env);
     56/**
     57 * Save current environment (registers).
     58 *
     59 * This function may return twice.
     60 *
     61 * @param env Variable where to save the environment (of type jmp_buf).
     62 * @return Whether the call returned after longjmp.
     63 * @retval 0 Environment was saved, normal execution.
     64 * @retval other longjmp was executed and returned here.
     65 */
     66#define setjmp(env) \
     67        ((env)[0].return_value = 0, \
     68        context_save(&(env)[0].context), \
     69        (env)[0].return_value)
     70
    4771extern void longjmp(jmp_buf env, int val) __attribute__((noreturn));
    4872
Note: See TracChangeset for help on using the changeset viewer.