1
|
1 // unwind-prot.cc -*- C++ -*- |
|
2 /* |
|
3 |
|
4 Copyright (C) 1992, 1993 John W. Eaton |
|
5 |
|
6 This file is part of Octave. |
|
7 |
|
8 Octave is free software; you can redistribute it and/or modify it |
|
9 under the terms of the GNU General Public License as published by the |
|
10 Free Software Foundation; either version 2, or (at your option) any |
|
11 later version. |
|
12 |
|
13 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
16 for more details. |
|
17 |
|
18 You should have received a copy of the GNU General Public License |
|
19 along with Octave; see the file COPYING. If not, write to the Free |
|
20 Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. |
|
21 |
|
22 */ |
|
23 |
|
24 #ifdef __GNUG__ |
|
25 #pragma implementation |
|
26 #endif |
|
27 |
|
28 #include <stddef.h> |
|
29 |
|
30 #include "SLStack.h" |
|
31 |
|
32 #include "Matrix.h" |
|
33 |
|
34 #include "unwind-prot.h" |
|
35 #include "error.h" |
|
36 #include "utils.h" |
|
37 |
|
38 class unwind_elem |
|
39 { |
|
40 public: |
|
41 unwind_elem (void); |
|
42 unwind_elem (char *t); |
|
43 unwind_elem (cleanup_func f, void *p); |
|
44 unwind_elem (const unwind_elem& el); |
|
45 ~unwind_elem (void); |
|
46 |
|
47 unwind_elem& operator = (const unwind_elem& el); |
|
48 |
|
49 char *tag (void); |
|
50 cleanup_func fptr (void); |
|
51 void *ptr (void); |
|
52 |
|
53 private: |
|
54 char *_tag; |
|
55 cleanup_func _fptr; |
|
56 void *_ptr; |
|
57 }; |
|
58 |
|
59 unwind_elem::unwind_elem (void) |
|
60 { |
|
61 _tag = (char *) NULL; |
|
62 _fptr = (cleanup_func) NULL; |
|
63 _ptr = (void *) NULL; |
|
64 } |
|
65 |
|
66 unwind_elem::unwind_elem (char *t) |
|
67 { |
|
68 _tag = strsave (t); |
|
69 _fptr = (cleanup_func) NULL; |
|
70 _ptr = (void *) NULL; |
|
71 } |
|
72 |
|
73 unwind_elem::unwind_elem (cleanup_func f, void *p) |
|
74 { |
|
75 _tag = (char *) NULL; |
|
76 _fptr = f; |
|
77 _ptr = p; |
|
78 } |
|
79 |
|
80 unwind_elem::unwind_elem (const unwind_elem& el) |
|
81 { |
|
82 _tag = strsave (el._tag); |
|
83 _fptr = el._fptr; |
|
84 _ptr = el._ptr; |
|
85 } |
|
86 |
|
87 unwind_elem::~unwind_elem (void) |
|
88 { |
|
89 delete [] _tag; |
|
90 } |
|
91 |
|
92 unwind_elem& |
|
93 unwind_elem::operator = (const unwind_elem& el) |
|
94 { |
|
95 _tag = strsave (el._tag); |
|
96 _fptr = el._fptr; |
|
97 _ptr = el._ptr; |
|
98 |
|
99 return *this; |
|
100 } |
|
101 |
|
102 char * |
|
103 unwind_elem::tag (void) |
|
104 { |
|
105 return _tag; |
|
106 } |
|
107 |
|
108 cleanup_func |
|
109 unwind_elem::fptr (void) |
|
110 { |
|
111 return _fptr; |
|
112 } |
|
113 |
|
114 void * |
|
115 unwind_elem::ptr (void) |
|
116 { |
|
117 return _ptr; |
|
118 } |
|
119 |
|
120 static SLStack <unwind_elem> unwind_protect_list; |
|
121 |
|
122 void |
|
123 add_unwind_protect (cleanup_func fptr, void *ptr) |
|
124 { |
|
125 unwind_elem el (fptr, ptr); |
|
126 unwind_protect_list.push (el); |
|
127 } |
|
128 |
|
129 void |
|
130 run_unwind_protect (void) |
|
131 { |
|
132 unwind_elem el = unwind_protect_list.pop (); |
|
133 |
|
134 cleanup_func f = el.fptr (); |
|
135 if (f != (cleanup_func) NULL) |
|
136 f (el.ptr ()); |
|
137 } |
|
138 |
|
139 void |
|
140 discard_unwind_protect (void) |
|
141 { |
|
142 unwind_protect_list.pop (); |
|
143 } |
|
144 |
|
145 void |
|
146 begin_unwind_frame (char *tag) |
|
147 { |
|
148 unwind_elem elem (tag); |
|
149 unwind_protect_list.push (elem); |
|
150 } |
|
151 |
|
152 void |
|
153 run_unwind_frame (char *tag) |
|
154 { |
|
155 while (! unwind_protect_list.empty ()) |
|
156 { |
|
157 unwind_elem el = unwind_protect_list.pop (); |
|
158 |
|
159 cleanup_func f = el.fptr (); |
|
160 if (f != (cleanup_func) NULL) |
|
161 f (el.ptr ()); |
|
162 |
|
163 char *t = el.tag (); |
|
164 if (t != (char *) NULL && strcmp (t, tag) == 0) |
|
165 break; |
|
166 } |
|
167 } |
|
168 |
|
169 void |
|
170 discard_unwind_frame (char *tag) |
|
171 { |
|
172 while (! unwind_protect_list.empty ()) |
|
173 { |
|
174 unwind_elem el = unwind_protect_list.pop (); |
|
175 char *t = el.tag (); |
|
176 if (t != (char *) NULL && strcmp (t, tag) == 0) |
|
177 break; |
|
178 } |
|
179 } |
|
180 |
|
181 void |
|
182 run_all_unwind_protects (void) |
|
183 { |
|
184 while (! unwind_protect_list.empty ()) |
|
185 { |
|
186 unwind_elem el = unwind_protect_list.pop (); |
|
187 |
|
188 cleanup_func f = el.fptr (); |
|
189 if (f != (cleanup_func) NULL) |
|
190 f (el.ptr ()); |
|
191 } |
|
192 } |
|
193 |
|
194 void |
|
195 discard_all_unwind_protects (void) |
|
196 { |
|
197 unwind_protect_list.clear (); |
|
198 } |
|
199 |
|
200 void |
|
201 matrix_cleanup (void *m) |
|
202 { |
|
203 delete [] (double *) m; |
|
204 } |
|
205 |
|
206 void |
|
207 complex_matrix_cleanup (void *cm) |
|
208 { |
|
209 delete [] (ComplexMatrix *) cm; |
|
210 } |
|
211 |
|
212 class saved_variable |
|
213 { |
|
214 public: |
|
215 enum var_type { integer, generic_ptr, generic }; |
|
216 |
|
217 saved_variable (void); |
|
218 saved_variable (int *p, int v); |
|
219 saved_variable (void **p, void *v); |
|
220 saved_variable (void *p, void *v, size_t sz); |
|
221 ~saved_variable (void); |
|
222 |
|
223 void restore_value (void); |
|
224 |
|
225 private: |
|
226 union |
|
227 { |
|
228 int *ptr_to_int; |
|
229 void *gen_ptr; |
|
230 void **ptr_to_gen_ptr; |
|
231 }; |
|
232 |
|
233 union |
|
234 { |
|
235 int int_value; |
|
236 void *gen_ptr_value; |
|
237 }; |
|
238 |
|
239 var_type type_tag; |
|
240 size_t size; |
|
241 }; |
|
242 |
|
243 saved_variable::saved_variable (void) |
|
244 { |
|
245 gen_ptr = (void *) NULL; |
|
246 gen_ptr_value = (void *) NULL; |
|
247 type_tag = generic; |
|
248 size = 0; |
|
249 } |
|
250 |
|
251 saved_variable::saved_variable (int *p, int v) |
|
252 { |
|
253 type_tag = integer; |
|
254 ptr_to_int = p; |
|
255 int_value = v; |
|
256 size = sizeof (int); |
|
257 } |
|
258 |
|
259 saved_variable::saved_variable (void **p, void *v) |
|
260 { |
|
261 type_tag = generic_ptr; |
|
262 ptr_to_gen_ptr = p; |
|
263 gen_ptr_value = v; |
|
264 size = sizeof (void *); |
|
265 } |
|
266 |
|
267 saved_variable::saved_variable (void *p, void *v, size_t sz) |
|
268 { |
|
269 gen_ptr = v; |
|
270 gen_ptr_value = new char [sz]; |
|
271 memcpy (gen_ptr_value, v, sz); |
|
272 size = sz; |
|
273 } |
|
274 |
|
275 saved_variable::~saved_variable (void) |
|
276 { |
|
277 if (type_tag == generic) |
|
278 delete [] gen_ptr_value; |
|
279 } |
|
280 |
|
281 void |
|
282 saved_variable::restore_value (void) |
|
283 { |
|
284 switch (type_tag) |
|
285 { |
|
286 case integer: |
|
287 *ptr_to_int = int_value; |
|
288 break; |
|
289 case generic_ptr: |
|
290 *ptr_to_gen_ptr = gen_ptr_value; |
|
291 break; |
|
292 case generic: |
|
293 memcpy (gen_ptr, gen_ptr_value, size); |
|
294 break; |
|
295 default: |
|
296 panic_impossible (); |
|
297 } |
|
298 } |
|
299 |
|
300 static void |
|
301 restore_saved_variable (void *s) |
|
302 { |
|
303 saved_variable *sv = (saved_variable *) s; |
|
304 sv->restore_value (); |
|
305 delete sv; |
|
306 } |
|
307 |
|
308 void |
|
309 unwind_protect_int_internal (int *ptr, int value) |
|
310 { |
|
311 saved_variable *s = new saved_variable (ptr, value); |
|
312 add_unwind_protect (restore_saved_variable, (void *) s); |
|
313 } |
|
314 |
|
315 void |
|
316 unwind_protect_ptr_internal (void **ptr, void *value) |
|
317 { |
|
318 saved_variable *s = new saved_variable (ptr, value); |
|
319 add_unwind_protect (restore_saved_variable, (void *) s); |
|
320 } |
|
321 |
|
322 void |
|
323 unwind_protect_var_internal (void *ptr, void *value, size_t size) |
|
324 { |
|
325 saved_variable *s = new saved_variable (ptr, value, size); |
|
326 add_unwind_protect (restore_saved_variable, (void *) s); |
|
327 } |
|
328 |
|
329 /* |
|
330 ;;; Local Variables: *** |
|
331 ;;; mode: C++ *** |
|
332 ;;; page-delimiter: "^/\\*" *** |
|
333 ;;; End: *** |
|
334 */ |