Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
m1lkweed authored Jan 25, 2024
1 parent 5b6807c commit c0bd0ad
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 12 deletions.
2 changes: 1 addition & 1 deletion example.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ closeable int close_me(int a){

[[noreturn]] void my_handler(int signum){
if(except_handler.frame)
xsiglongjmp(*except_handler.frame, signum);
siglongjmp(*except_handler.frame, signum);
exit(1);
}

Expand Down
18 changes: 7 additions & 11 deletions exception.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,11 @@
#ifndef _EXCEPTION_H_
#define _EXCEPTION_H_

#if defined __godbolt__ || defined __GODBOLT__
#include "https://raw.githubusercontent.com/m1lkweed/handy-headers/main/xsetjmp.h"
#else
#include "xsetjmp.h"
#endif
#include <setjmp.h>
#include <signal.h>

struct exception_frame {
xsigjmp_buf *frame;
sigjmp_buf *frame;
volatile sig_atomic_t exception;
};

Expand All @@ -24,15 +20,15 @@ void _$except_init$(void);
void throw(int id);

#define try do{ \
xsigjmp_buf *_$old_exception_frame$, _$new_exception_frame$; \
sigjmp_buf *_$old_exception_frame$, _$new_exception_frame$; \
volatile int _$except_dummy$ = -1; \
typedef int _$except_no_gotos$[_$except_dummy$]; \
_$old_exception_frame$ = except_handler.frame; \
except_handler.frame = &_$new_exception_frame$; \
except_handler.exception = 0; \
_$except_init$(); \
if((except_handler.exception = xsigsetjmp(_$new_exception_frame$, 0)) == 0){ \
for(; _$except_dummy$; ++_$except_dummy$)
if((except_handler.exception = sigsetjmp(_$new_exception_frame$, 0)) == 0){ \
for(_$except_dummy$ = 1; _$except_dummy$; --_$except_dummy$)

#define _$EXCEPT_EMPTY$_HELPER(...) = except_handler.exception, ## __VA_ARGS__
#define _$EXCEPT_EMPTY$(default, ...) default _$EXCEPT_EMPTY$_HELPER(__VA_ARGS__)
Expand All @@ -55,7 +51,7 @@ _Thread_local struct exception_frame except_handler = {0};

_Noreturn void _$exception_handler$(int signum){
if(except_handler.frame){
xsiglongjmp(*except_handler.frame, signum);
siglongjmp(*except_handler.frame, signum);
}else{
//Set handler to SIG_DFL and reraise signal, in case
//the system wants to print a message or core dump
Expand Down Expand Up @@ -148,7 +144,7 @@ _Noreturn void _$exception_handler$(int signum){
if(id != 0){
except_handler.exception = id;
if(except_handler.frame)
xsiglongjmp(*except_handler.frame, id);
siglongjmp(*except_handler.frame, id);
_$exception_handler$(id);
}
}
Expand Down

0 comments on commit c0bd0ad

Please sign in to comment.