comparison gsm-fw/riviera/support/exception.c @ 143:afceeeb2cba1

Our nuc-fw is destined to become gsm-fw, so I went ahead and did the big hg mv
author Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
date Tue, 12 Nov 2013 05:35:48 +0000
parents nuc-fw/riviera/support/exception.c@7d7950d7f924
children
comparison
equal deleted inserted replaced
142:15d5977390c2 143:afceeeb2cba1
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 // Authorization to use this source code communicated to Christophe Favergeon
18 // by email
19
20 #include "../../include/config.h"
21
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <assert.h>
25 #include "exception.h"
26
27
28 /* global stack of exception context */
29 struct _exceptionContext_ *const _returnExceptionContext_[MAX_RVF_TASKS]=
30 #if (!GSMLITE)
31 {NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,
32 NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL};
33 #else
34 {NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL};
35 #endif
36
37 struct _exceptionContext_ *_currentExceptionContext_ [MAX_RVF_TASKS]=
38 #if (!GSMLITE)
39 {NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,
40 NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL};
41 #else
42 {NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL};
43 #endif
44
45 /* delete protected pointers and throw exception */
46 void
47 _exceptionThrow_(int exception)
48 {
49 struct _protectedPtr_ *p;
50 struct _protectedPtr_ *oldp;
51
52 /* no exception context saved, exit program */
53 if (!_currentExceptionContext_[rvf_get_taskid()]) exit(exception);
54
55 /* free pointers stored on the current exception context pointers stack */
56 p=_currentExceptionContext_[rvf_get_taskid()]->stack;
57
58 while(p)
59 {
60 oldp=p->previous;
61 p->func(p->ptr);
62 rvf_free_buf(p);
63 p=oldp;
64 }
65
66 _currentExceptionContext_[rvf_get_taskid()]->stack=NULL;
67
68 /* jump to previous exception context */
69 rvf_restore_context_buffer_(_currentExceptionContext_[rvf_get_taskid()]->context, exception);
70 }
71
72
73 // Protect a pointer when there is a try/ctahc block active
74 void rvf_protect_pointer(T_RVF_MB_ID mb_id,void *p,T_RVF_RELEASE_PROTECTED_POINTER func)
75 {
76
77 struct _protectedPtr_ *ptr;
78 struct _exceptionContext_ *context;
79 T_RVF_MB_STATUS err;
80
81
82
83 if (_currentExceptionContext_[rvf_get_taskid()])
84 {
85
86 if (p==NULL)
87 throw(E_not_enough_memory);
88
89 context=_currentExceptionContext_[rvf_get_taskid()];
90 err=rvf_get_buf(mb_id,sizeof(struct _protectedPtr_),(void*)&ptr);
91
92
93 if (err==RVF_GREEN)
94 {
95 ptr->next=NULL;
96 ptr->previous=NULL;
97 ptr->ptr=p;
98 ptr->func=func;
99
100 if (context->stack==NULL)
101 {
102 context->stack=ptr;
103 }
104 else
105 {
106 ptr->previous=context->stack;
107 context->stack->next=ptr;
108 context->stack=ptr;
109 }
110 }
111 else
112 {
113 if (p!=NULL)
114 rvf_free_buf(p);
115 throw(E_not_enough_memory);
116 }
117 }
118 }
119
120 void rvf_forget_protected_ptr()
121 {
122 struct _protectedPtr_ *p;
123 struct _protectedPtr_ *oldp;
124
125 p=_currentExceptionContext_[rvf_get_taskid()]->stack;
126
127 while(p)
128 {
129 oldp=p->previous;
130 rvf_free_buf(p);
131 p=oldp;
132 }
133
134 _currentExceptionContext_[rvf_get_taskid()]->stack=NULL;
135 }