forked from DC-SWAT/DreamShell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexceptions.h
More file actions
164 lines (126 loc) · 4.1 KB
/
exceptions.h
File metadata and controls
164 lines (126 loc) · 4.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/**
* \file exceptions.h
* \brief Exception handling for DreamShell
* \date 2007-2014
* \author SWAT www.dc-swat.ru
*/
#ifndef _DS_EXECEPTIONS_H_
#define _DS_EXECEPTIONS_H_
#include <kos/thread.h>
#include "setjmp.h"
#define USE_DS_EXCEPTIONS 1
#define EXPT_GUARD_STACK_SIZE 16
#define EXPT_GUARD_ST_DYNAMIC -1
#define EXPT_GUARD_ST_STATIC_FREE 0
typedef struct expt_guard_stack {
int pos;
jmp_buf jump[EXPT_GUARD_STACK_SIZE];
/**
* If equal to EXPT_GUARD_ST_DYNAMIC, then dynamic.
* If equal to EXPT_GUARD_ST_STATIC_FREE, then static and not used
* Otherwise index in static
*/
int type;
} expt_quard_stack_t;
/**
* Initialize the exception system.
*/
int expt_init();
/**
* Shutdown the exception system.
*/
void expt_shutdown();
/**
* Get guard stack from current thread.
*/
expt_quard_stack_t *expt_get_stack();
/**
* Print catched exception place
*/
void expt_print_place(char *file, int line, const char *func);
/** \name Protected section.
*
* To protect a code from exception :
* \code
* char * buffer = malloc(32);
* EXPT_GUARD_BEGIN;
* // Run code to protect here. This instruction makes a bus error or
* // something like that on most machine.
* *(int *) (buffer+1) = 0xDEADBEEF;
*
* EXPT_GUARD_CATCH;
* // Things to do if hell happen
* printf("Error\n");
* free(buffer);
* return -1;
*
* EXPT_GUARD_END;
* // Life continue ...
* memset(buffer,0,32);
* // ...
* \endcode
*
*/
#ifdef USE_DS_EXCEPTIONS
/**
* Throw exception.
*/
#define EXPT_GUARD_THROW *(int *)1 = 0xdeadbeef
/**
* Get exceptions stack from current thread.
*/
#define EXPT_GUARD_INIT expt_quard_stack_t *__expt = expt_get_stack()
/**
* Start a protected section.
* \warning : it is FORBIDEN to do "return" inside a guarded section,
* use EXPT_GUARD_RETURN instead.
*/
#define EXPT_GUARD_BEGIN_NEXT \
do { \
__expt->pos++; \
if (__expt->pos >= EXPT_GUARD_STACK_SIZE) \
EXPT_GUARD_THROW; \
if (!setjmp(__expt->jump[__expt->pos])) { \
/**
* Get stack and start a protected section.
*/
#define EXPT_GUARD_BEGIN \
EXPT_GUARD_INIT; \
EXPT_GUARD_BEGIN_NEXT
/**
* Catch a protected section.
*/
#define EXPT_GUARD_CATCH \
} else { \
expt_print_place(__FILE__, __LINE__, __FUNCTION__)
/**
* End of protected section.
*/
#define EXPT_GUARD_END \
} \
__expt->pos--; \
} while(0)
/**
* Return in middle of a guarded section.
* \warning : to be used exclusively inbetween EXPT_GUARD_BEGIN and
* EXPT_GUARD_END. Never use normal "return" in this case.
*/
#define EXPT_GUARD_RETURN __expt->pos--; return
#define EXPT_GUARD_ASSIGN(dst, src, catched) \
EXPT_GUARD_BEGIN; \
dst = src; \
EXPT_GUARD_CATCH; \
catched; \
EXPT_GUARD_END
#else /* ifdef USE_DS_EXCEPTIONS */
#define EXPT_GUARD_THROW
#define EXPT_GUARD_BEGIN \
if (1) { \
#define EXPT_GUARD_CATCH \
} else { \
#define EXPT_GUARD_END }
#define EXPT_GUARD_RETURN \
return
#define EXPT_GUARD_ASSIGN(dst, src, errval) dst = src
#endif /* ifdef USE_DS_EXCEPTIONS */
#endif /* ifndef _DS_EXECEPTIONS_H_*/