comparison src/cs/riviera/support/exception.h @ 0:b6a5e36de839

src/cs: initial import from Magnetite
author Mychaela Falconia <falcon@freecalypso.org>
date Sun, 15 Jul 2018 04:39:26 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:b6a5e36de839
1 /*
2 ******************************
3 *
4 *
5 * Initialial version: Laurent Deniau, Laurent.Deniau@cern.ch
6 *
7 * For more information, please see the paper:
8 * http://cern.ch/Laurent.Deniau/html/oopc/exception.html
9 *
10 * -----------------------------------------------------------
11 *
12 * Strong rework and adaption to riviera by Christophe Favergeon
13 *
14 ******************************
15 */
16
17 #ifndef RVF_EXCEPTION_H
18 #define RVF_EXCEPTION_H
19
20 #include "rvf/rvf_api.h"
21 #include "rvf/rvf_target.h"
22
23 #include <string.h>
24
25
26
27 //#ifndef __STDC__
28 //# error "exception.h needs ISO C compiler to work properly"
29 //#endif
30
31 #include <setjmp.h>
32
33 typedef enum K_RVF_EXCEPTIONS { E_invalid=1, E_not_enough_memory,E_unknown } T_RVF_EXCEPTIONS;
34
35 typedef void (*T_RVF_RELEASE_PROTECTED_POINTER)(void *p);
36
37 /*
38 choose context savings
39 */
40
41 #define rvf_save_context_buffer_(context) setjmp(context)
42 #define rvf_restore_context_buffer_(context, val) longjmp(context, val)
43
44 /*
45 some hidden types used to handle exceptions
46 */
47
48 /* type of stack of protected pointer */
49 struct _protectedPtr_ {
50 struct _protectedPtr_ *next;
51 struct _protectedPtr_ *previous;
52 void *ptr;
53 T_RVF_RELEASE_PROTECTED_POINTER func;
54 };
55
56 /* type of stack of exception */
57 struct _exceptionContext_ {
58 struct _exceptionContext_ *next;
59 struct _protectedPtr_ *stack;
60 jmp_buf context;
61 };
62
63 extern struct _exceptionContext_ *const _returnExceptionContext_[MAX_RVF_TASKS];
64 extern struct _exceptionContext_ *_currentExceptionContext_[MAX_RVF_TASKS];
65
66 /* exception keywords */
67 #define try \
68 do { \
69 struct _exceptionContext_ _localExceptionContext_ ;\
70 memset(&_localExceptionContext_,0,sizeof(struct _exceptionContext_));\
71 _localExceptionContext_.next=_currentExceptionContext_[rvf_get_taskid()];\
72 _currentExceptionContext_[rvf_get_taskid()] = &_localExceptionContext_; \
73 do { \
74 int const exception = \
75 rvf_save_context_buffer_(_currentExceptionContext_[rvf_get_taskid()]->context); \
76 if (!exception) {
77
78 #define catch(except) \
79 } else if ((int)(except) == exception) { \
80 _currentExceptionContext_[rvf_get_taskid()] = _currentExceptionContext_[rvf_get_taskid()]->next;
81
82 #define catch_any \
83 } else { \
84 _currentExceptionContext_[rvf_get_taskid()] = _currentExceptionContext_[rvf_get_taskid()]->next;
85
86 #define endtry \
87 } \
88 } while(0); \
89 if (_currentExceptionContext_[rvf_get_taskid()] == &_localExceptionContext_) { \
90 rvf_forget_protected_ptr();\
91 _currentExceptionContext_[rvf_get_taskid()] = _currentExceptionContext_[rvf_get_taskid()]->next; \
92 } \
93 } while(0)
94
95 #define rethrow throw(exception)
96 #define break_try break
97
98 /*
99 #define return_try(...) \
100 do { \
101 _currentExceptionContext_ = _returnExceptionContext_; \
102 return __VA_ARGS__; \
103 } while(0)
104 */
105
106 #define throw(except) _exceptionThrow_((int)(except))
107
108
109 /*
110 extern declarations
111 */
112
113 extern void _exceptionThrow_(int except);
114
115 extern void rvf_forget_protected_ptr();
116 extern void rvf_protect_pointer(T_RVF_MB_ID mb_id,void *p,T_RVF_RELEASE_PROTECTED_POINTER func);
117
118 #define RVF_PROTECT(bank,p) rvf_protect_pointer(bank,p,(T_RVF_RELEASE_PROTECTED_POINTER)rvf_free_buf)
119
120 #define THROW_IF_ERROR(err) if (err!=0) throw(E_unknown)
121 #define THROW_IF_NULL(p) if (p==NULL) throw(E_not_enough_memory)
122 #define THROW_IF_YELLOW(b,p) if ((p==NULL) || (rvf_get_mb_status(b)==RVF_YELLOW)) throw(E_not_enough_memory)
123 #endif