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 |
1343
|
32 #include <cstddef> |
1
|
33 |
|
34 #include "SLStack.h" |
|
35 |
453
|
36 #include "CMatrix.h" |
1
|
37 |
1352
|
38 #include "error.h" |
1
|
39 #include "unwind-prot.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); |
|
204 |
|
205 void restore_value (void); |
|
206 |
|
207 private: |
|
208 union |
|
209 { |
|
210 int *ptr_to_int; |
|
211 void *gen_ptr; |
|
212 void **ptr_to_gen_ptr; |
|
213 }; |
|
214 |
|
215 union |
|
216 { |
|
217 int int_value; |
|
218 void *gen_ptr_value; |
|
219 }; |
|
220 |
|
221 var_type type_tag; |
|
222 size_t size; |
|
223 }; |
|
224 |
|
225 saved_variable::saved_variable (void) |
|
226 { |
529
|
227 gen_ptr = 0; |
|
228 gen_ptr_value = 0; |
1
|
229 type_tag = generic; |
|
230 size = 0; |
|
231 } |
|
232 |
|
233 saved_variable::saved_variable (int *p, int v) |
|
234 { |
|
235 type_tag = integer; |
|
236 ptr_to_int = p; |
|
237 int_value = v; |
|
238 size = sizeof (int); |
|
239 } |
|
240 |
|
241 saved_variable::saved_variable (void **p, void *v) |
|
242 { |
|
243 type_tag = generic_ptr; |
|
244 ptr_to_gen_ptr = p; |
|
245 gen_ptr_value = v; |
|
246 size = sizeof (void *); |
|
247 } |
|
248 |
|
249 saved_variable::~saved_variable (void) |
|
250 { |
|
251 if (type_tag == generic) |
|
252 delete [] gen_ptr_value; |
|
253 } |
|
254 |
|
255 void |
|
256 saved_variable::restore_value (void) |
|
257 { |
|
258 switch (type_tag) |
|
259 { |
|
260 case integer: |
|
261 *ptr_to_int = int_value; |
|
262 break; |
777
|
263 |
1
|
264 case generic_ptr: |
|
265 *ptr_to_gen_ptr = gen_ptr_value; |
|
266 break; |
777
|
267 |
1
|
268 case generic: |
|
269 memcpy (gen_ptr, gen_ptr_value, size); |
|
270 break; |
777
|
271 |
1
|
272 default: |
|
273 panic_impossible (); |
777
|
274 break; |
1
|
275 } |
|
276 } |
|
277 |
|
278 static void |
|
279 restore_saved_variable (void *s) |
|
280 { |
|
281 saved_variable *sv = (saved_variable *) s; |
|
282 sv->restore_value (); |
|
283 delete sv; |
|
284 } |
|
285 |
|
286 void |
|
287 unwind_protect_int_internal (int *ptr, int value) |
|
288 { |
|
289 saved_variable *s = new saved_variable (ptr, value); |
|
290 add_unwind_protect (restore_saved_variable, (void *) s); |
|
291 } |
|
292 |
|
293 void |
|
294 unwind_protect_ptr_internal (void **ptr, void *value) |
|
295 { |
|
296 saved_variable *s = new saved_variable (ptr, value); |
|
297 add_unwind_protect (restore_saved_variable, (void *) s); |
|
298 } |
|
299 |
|
300 /* |
|
301 ;;; Local Variables: *** |
|
302 ;;; mode: C++ *** |
|
303 ;;; page-delimiter: "^/\\*" *** |
|
304 ;;; End: *** |
|
305 */ |