1
|
1 // unwind-prot.cc -*- C++ -*- |
|
2 /* |
|
3 |
1009
|
4 Copyright (C) 1992, 1993, 1994, 1995 John W. Eaton |
1
|
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 |
1315
|
20 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
1
|
21 |
|
22 */ |
|
23 |
1297
|
24 #if defined (__GNUG__) |
|
25 #pragma implementation |
|
26 #endif |
|
27 |
240
|
28 #ifdef HAVE_CONFIG_H |
1192
|
29 #include <config.h> |
1
|
30 #endif |
|
31 |
|
32 #include <stddef.h> |
|
33 |
|
34 #include "SLStack.h" |
|
35 |
453
|
36 #include "CMatrix.h" |
1
|
37 |
|
38 #include "unwind-prot.h" |
|
39 #include "error.h" |
|
40 #include "utils.h" |
|
41 |
|
42 unwind_elem::unwind_elem (void) |
|
43 { |
529
|
44 unwind_elem_tag = 0; |
|
45 unwind_elem_fptr = 0; |
|
46 unwind_elem_ptr = 0; |
1
|
47 } |
|
48 |
|
49 unwind_elem::unwind_elem (char *t) |
|
50 { |
453
|
51 unwind_elem_tag = strsave (t); |
529
|
52 unwind_elem_fptr = 0; |
|
53 unwind_elem_ptr = 0; |
1
|
54 } |
|
55 |
|
56 unwind_elem::unwind_elem (cleanup_func f, void *p) |
|
57 { |
529
|
58 unwind_elem_tag = 0; |
453
|
59 unwind_elem_fptr = f; |
|
60 unwind_elem_ptr = p; |
1
|
61 } |
|
62 |
|
63 unwind_elem::unwind_elem (const unwind_elem& el) |
|
64 { |
453
|
65 unwind_elem_tag = strsave (el.unwind_elem_tag); |
|
66 unwind_elem_fptr = el.unwind_elem_fptr; |
|
67 unwind_elem_ptr = el.unwind_elem_ptr; |
1
|
68 } |
|
69 |
|
70 unwind_elem::~unwind_elem (void) |
|
71 { |
453
|
72 delete [] unwind_elem_tag; |
1
|
73 } |
|
74 |
|
75 unwind_elem& |
|
76 unwind_elem::operator = (const unwind_elem& el) |
|
77 { |
453
|
78 unwind_elem_tag = strsave (el.unwind_elem_tag); |
|
79 unwind_elem_fptr = el.unwind_elem_fptr; |
|
80 unwind_elem_ptr = el.unwind_elem_ptr; |
1
|
81 |
|
82 return *this; |
|
83 } |
|
84 |
|
85 char * |
|
86 unwind_elem::tag (void) |
|
87 { |
453
|
88 return unwind_elem_tag; |
1
|
89 } |
|
90 |
|
91 cleanup_func |
|
92 unwind_elem::fptr (void) |
|
93 { |
453
|
94 return unwind_elem_fptr; |
1
|
95 } |
|
96 |
|
97 void * |
|
98 unwind_elem::ptr (void) |
|
99 { |
453
|
100 return unwind_elem_ptr; |
1
|
101 } |
|
102 |
|
103 static SLStack <unwind_elem> unwind_protect_list; |
|
104 |
|
105 void |
|
106 add_unwind_protect (cleanup_func fptr, void *ptr) |
|
107 { |
|
108 unwind_elem el (fptr, ptr); |
|
109 unwind_protect_list.push (el); |
|
110 } |
|
111 |
|
112 void |
|
113 run_unwind_protect (void) |
|
114 { |
|
115 unwind_elem el = unwind_protect_list.pop (); |
|
116 |
|
117 cleanup_func f = el.fptr (); |
529
|
118 if (f) |
1
|
119 f (el.ptr ()); |
|
120 } |
|
121 |
|
122 void |
|
123 discard_unwind_protect (void) |
|
124 { |
|
125 unwind_protect_list.pop (); |
|
126 } |
|
127 |
|
128 void |
|
129 begin_unwind_frame (char *tag) |
|
130 { |
|
131 unwind_elem elem (tag); |
|
132 unwind_protect_list.push (elem); |
|
133 } |
|
134 |
|
135 void |
|
136 run_unwind_frame (char *tag) |
|
137 { |
|
138 while (! unwind_protect_list.empty ()) |
|
139 { |
|
140 unwind_elem el = unwind_protect_list.pop (); |
|
141 |
|
142 cleanup_func f = el.fptr (); |
529
|
143 if (f) |
1
|
144 f (el.ptr ()); |
|
145 |
|
146 char *t = el.tag (); |
529
|
147 if (t && strcmp (t, tag) == 0) |
1
|
148 break; |
|
149 } |
|
150 } |
|
151 |
|
152 void |
|
153 discard_unwind_frame (char *tag) |
|
154 { |
|
155 while (! unwind_protect_list.empty ()) |
|
156 { |
|
157 unwind_elem el = unwind_protect_list.pop (); |
|
158 char *t = el.tag (); |
529
|
159 if (t && strcmp (t, tag) == 0) |
1
|
160 break; |
|
161 } |
|
162 } |
|
163 |
|
164 void |
|
165 run_all_unwind_protects (void) |
|
166 { |
|
167 while (! unwind_protect_list.empty ()) |
|
168 { |
|
169 unwind_elem el = unwind_protect_list.pop (); |
|
170 |
|
171 cleanup_func f = el.fptr (); |
529
|
172 if (f) |
1
|
173 f (el.ptr ()); |
|
174 } |
|
175 } |
|
176 |
|
177 void |
|
178 discard_all_unwind_protects (void) |
|
179 { |
|
180 unwind_protect_list.clear (); |
|
181 } |
|
182 |
|
183 void |
|
184 matrix_cleanup (void *m) |
|
185 { |
|
186 delete [] (double *) m; |
|
187 } |
|
188 |
|
189 void |
|
190 complex_matrix_cleanup (void *cm) |
|
191 { |
|
192 delete [] (ComplexMatrix *) cm; |
|
193 } |
|
194 |
|
195 class saved_variable |
|
196 { |
|
197 public: |
|
198 enum var_type { integer, generic_ptr, generic }; |
|
199 |
|
200 saved_variable (void); |
|
201 saved_variable (int *p, int v); |
|
202 saved_variable (void **p, void *v); |
|
203 saved_variable (void *p, void *v, size_t sz); |
|
204 ~saved_variable (void); |
|
205 |
|
206 void restore_value (void); |
|
207 |
|
208 private: |
|
209 union |
|
210 { |
|
211 int *ptr_to_int; |
|
212 void *gen_ptr; |
|
213 void **ptr_to_gen_ptr; |
|
214 }; |
|
215 |
|
216 union |
|
217 { |
|
218 int int_value; |
|
219 void *gen_ptr_value; |
|
220 }; |
|
221 |
|
222 var_type type_tag; |
|
223 size_t size; |
|
224 }; |
|
225 |
|
226 saved_variable::saved_variable (void) |
|
227 { |
529
|
228 gen_ptr = 0; |
|
229 gen_ptr_value = 0; |
1
|
230 type_tag = generic; |
|
231 size = 0; |
|
232 } |
|
233 |
|
234 saved_variable::saved_variable (int *p, int v) |
|
235 { |
|
236 type_tag = integer; |
|
237 ptr_to_int = p; |
|
238 int_value = v; |
|
239 size = sizeof (int); |
|
240 } |
|
241 |
|
242 saved_variable::saved_variable (void **p, void *v) |
|
243 { |
|
244 type_tag = generic_ptr; |
|
245 ptr_to_gen_ptr = p; |
|
246 gen_ptr_value = v; |
|
247 size = sizeof (void *); |
|
248 } |
|
249 |
|
250 saved_variable::saved_variable (void *p, void *v, size_t sz) |
|
251 { |
|
252 gen_ptr = v; |
|
253 gen_ptr_value = new char [sz]; |
|
254 memcpy (gen_ptr_value, v, sz); |
|
255 size = sz; |
|
256 } |
|
257 |
|
258 saved_variable::~saved_variable (void) |
|
259 { |
|
260 if (type_tag == generic) |
|
261 delete [] gen_ptr_value; |
|
262 } |
|
263 |
|
264 void |
|
265 saved_variable::restore_value (void) |
|
266 { |
|
267 switch (type_tag) |
|
268 { |
|
269 case integer: |
|
270 *ptr_to_int = int_value; |
|
271 break; |
777
|
272 |
1
|
273 case generic_ptr: |
|
274 *ptr_to_gen_ptr = gen_ptr_value; |
|
275 break; |
777
|
276 |
1
|
277 case generic: |
|
278 memcpy (gen_ptr, gen_ptr_value, size); |
|
279 break; |
777
|
280 |
1
|
281 default: |
|
282 panic_impossible (); |
777
|
283 break; |
1
|
284 } |
|
285 } |
|
286 |
|
287 static void |
|
288 restore_saved_variable (void *s) |
|
289 { |
|
290 saved_variable *sv = (saved_variable *) s; |
|
291 sv->restore_value (); |
|
292 delete sv; |
|
293 } |
|
294 |
|
295 void |
|
296 unwind_protect_int_internal (int *ptr, int value) |
|
297 { |
|
298 saved_variable *s = new saved_variable (ptr, value); |
|
299 add_unwind_protect (restore_saved_variable, (void *) s); |
|
300 } |
|
301 |
|
302 void |
|
303 unwind_protect_ptr_internal (void **ptr, void *value) |
|
304 { |
|
305 saved_variable *s = new saved_variable (ptr, value); |
|
306 add_unwind_protect (restore_saved_variable, (void *) s); |
|
307 } |
|
308 |
|
309 void |
|
310 unwind_protect_var_internal (void *ptr, void *value, size_t size) |
|
311 { |
|
312 saved_variable *s = new saved_variable (ptr, value, size); |
|
313 add_unwind_protect (restore_saved_variable, (void *) s); |
|
314 } |
|
315 |
|
316 /* |
|
317 ;;; Local Variables: *** |
|
318 ;;; mode: C++ *** |
|
319 ;;; page-delimiter: "^/\\*" *** |
|
320 ;;; End: *** |
|
321 */ |