123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231 |
- /*++
- Copyright (c) 2013 Minoca Corp.
- This file is licensed under the terms of the GNU General Public License
- version 3. Alternative licensing terms are available. Contact
- info@minocacorp.com for details. See the LICENSE file at the root of this
- project for complete licensing information.
- Module Name:
- setjmp.c
- Abstract:
- This module implements the setjmp and longjmp functions used for non-local
- goto statements.
- Author:
- Evan Green 28-Jul-2013
- Environment:
- User Mode C Library
- --*/
- //
- // ------------------------------------------------------------------- Includes
- //
- #include "libcp.h"
- #include <setjmp.h>
- #include <signal.h>
- //
- // ---------------------------------------------------------------- Definitions
- //
- //
- // ------------------------------------------------------ Data Type Definitions
- //
- //
- // ----------------------------------------------- Internal Function Prototypes
- //
- void
- ClpLongJump (
- jmp_buf Environment,
- int Value
- );
- //
- // -------------------------------------------------------------------- Globals
- //
- //
- // ------------------------------------------------------------------ Functions
- //
- LIBC_API
- void
- longjmp (
- jmp_buf Environment,
- int Value
- )
- /*++
- Routine Description:
- This routine restores the environment saved by the most recent invocation
- of setjmp with the given environment buffer. If there is no such invocation,
- or if the function containing the invocation of setjmp has terminated in
- the interim, the behavior is undefined. Most likely, the app will crash
- spectacularly.
- Arguments:
- Environment - Supplies the pointer to the previously saved environment to
- restore.
- Value - Supplies the value to set as the return value from the setjmp
- function. If this value is 0, it will be set to 1.
- Return Value:
- None, this routine does not return.
- --*/
- {
- if (Value == 0) {
- Value = 1;
- }
- return ClpLongJump(Environment, Value);
- }
- LIBC_API
- void
- _longjmp (
- jmp_buf Environment,
- int Value
- )
- /*++
- Routine Description:
- This routine restores the environment saved by the most recent invocation
- of setjmp with the given environment buffer. If there is no such invocation,
- or if the function containing the invocation of setjmp has terminated in
- the interim, the behavior is undefined. Most likely, the app will crash
- spectacularly.
- Arguments:
- Environment - Supplies the pointer to the previously saved environment to
- restore.
- Value - Supplies the value to set as the return value from the setjmp
- function. If this value is 0, it will be set to 1.
- Return Value:
- None, this routine does not return.
- --*/
- {
- if (Value == 0) {
- Value = 1;
- }
- return ClpLongJump(Environment, Value);
- }
- LIBC_API
- void
- siglongjmp (
- sigjmp_buf Environment,
- int Value
- )
- /*++
- Routine Description:
- This routine restores the environment saved by the most recent invocation
- of setjmp with the given environment buffer. It works just like the longjmp
- function, except it also restores the signal mask if the given environment
- buffer was initialized with sigsetjmp with a non-zero save mask value.
- Arguments:
- Environment - Supplies the pointer to the previously saved environment to
- restore.
- Value - Supplies the value to set as the return value from the setjmp
- function. If this value is 0, it will be set to 1.
- Return Value:
- None, this routine does not return.
- --*/
- {
- if (Environment[0] != 0) {
- sigprocmask(SIG_SETMASK, (sigset_t *)&(Environment[1]), NULL);
- }
- if (Value == 0) {
- Value = 1;
- }
- return ClpLongJump(Environment, Value);
- }
- void
- ClpSetJump (
- jmp_buf Environment,
- int SaveMask
- )
- /*++
- Routine Description:
- This routine saves the calling environment into the given buffer for
- later use by longjmp.
- Arguments:
- Environment - Supplies the pointer to the environment to save the
- application context in.
- SaveMask - Supplies a value indicating if the caller would like the
- current signal mask to be saved in the environment as well.
- Return Value:
- 0 if this was the direct call to setjmp.
- Non-zero if this was a call from longjmp.
- --*/
- {
- Environment[0] = SaveMask;
- if (SaveMask != 0) {
- ASSERT(sizeof(sigset_t) <= sizeof(unsigned long long));
- sigprocmask(SIG_BLOCK, NULL, (sigset_t *)&(Environment[1]));
- }
- return;
- }
- //
- // --------------------------------------------------------- Internal Functions
- //
|