Slimmer setjmps
[akaros.git] / kern / include / err.h
1 /* Plan9 style error popping.  For details, read Documentation/plan9.txt */
2
3 #ifndef ROS_KERN_ERR_H
4 #define ROS_KERN_ERR_H
5
6 #include <setjmp.h>
7 #include <syscall.h>
8
9 struct errbuf {
10         struct jmpbuf jmpbuf;
11 };
12
13 #define ERRSTACK(x) struct errbuf *prev_errbuf; struct errbuf errstack[(x)];   \
14                     int curindex = 0;
15 #define waserror() (errpush(errstack, ARRAY_SIZE(errstack), &curindex,         \
16                             &prev_errbuf) ||                                   \
17                     setjmp(&(get_cur_errbuf()->jmpbuf)))
18 #define error(x,...) {set_errstr(x, ##__VA_ARGS__);                            \
19                       longjmp(&get_cur_errbuf()->jmpbuf, 1);}
20 #define nexterror() {errpop(errstack, ARRAY_SIZE(errstack), &curindex,         \
21                             prev_errbuf);                                      \
22                      longjmp(&(get_cur_errbuf())->jmpbuf, 1);}
23 #define poperror() {errpop(errstack, ARRAY_SIZE(errstack), &curindex,          \
24                            prev_errbuf);}
25
26 int errpush(struct errbuf *errstack, int stacksize, int *curindex,
27             struct errbuf **prev_errbuf);
28 void errpop(struct errbuf *errstack, int stacksize, int *curindex,
29             struct errbuf *prev_errbuf);
30
31 #endif /* ROS_KERN_ERR_H */