1
|
1 /* |
|
2 |
2847
|
3 Copyright (C) 1996, 1997 John W. Eaton |
1
|
4 |
|
5 This file is part of Octave. |
|
6 |
|
7 Octave is free software; you can redistribute it and/or modify it |
|
8 under the terms of the GNU General Public License as published by the |
|
9 Free Software Foundation; either version 2, or (at your option) any |
|
10 later version. |
|
11 |
|
12 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
15 for more details. |
|
16 |
|
17 You should have received a copy of the GNU General Public License |
|
18 along with Octave; see the file COPYING. If not, write to the Free |
1315
|
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
1
|
20 |
|
21 */ |
|
22 |
1297
|
23 #if defined (__GNUG__) |
|
24 #pragma implementation |
|
25 #endif |
|
26 |
240
|
27 #ifdef HAVE_CONFIG_H |
1192
|
28 #include <config.h> |
1
|
29 #endif |
|
30 |
1343
|
31 #include <cstddef> |
1
|
32 |
1755
|
33 #include <string> |
|
34 |
1
|
35 #include "SLStack.h" |
|
36 |
453
|
37 #include "CMatrix.h" |
1
|
38 |
1352
|
39 #include "error.h" |
1
|
40 #include "unwind-prot.h" |
|
41 #include "utils.h" |
|
42 |
2985
|
43 SLStack<unwind_elem> unwind_protect::list; |
1
|
44 |
2985
|
45 class |
|
46 saved_variable |
1
|
47 { |
2985
|
48 public: |
1755
|
49 |
2985
|
50 enum var_type |
|
51 { |
|
52 boolean, |
|
53 integer, |
|
54 string_type, |
|
55 generic_ptr, |
|
56 generic |
|
57 }; |
1
|
58 |
|
59 saved_variable (void); |
2985
|
60 |
|
61 saved_variable (bool *p, bool v); |
|
62 |
1
|
63 saved_variable (int *p, int v); |
2985
|
64 |
1755
|
65 saved_variable (string *p, const string& v); |
2985
|
66 |
1
|
67 saved_variable (void **p, void *v); |
2985
|
68 |
1
|
69 ~saved_variable (void); |
|
70 |
|
71 void restore_value (void); |
|
72 |
2985
|
73 static void restore (void *s); |
|
74 |
|
75 private: |
|
76 |
1
|
77 union |
|
78 { |
2985
|
79 bool *ptr_to_bool; |
1
|
80 int *ptr_to_int; |
|
81 void *gen_ptr; |
|
82 void **ptr_to_gen_ptr; |
|
83 }; |
|
84 |
|
85 union |
|
86 { |
2985
|
87 bool bool_value; |
1
|
88 int int_value; |
1755
|
89 const string *str_value; |
1
|
90 void *gen_ptr_value; |
|
91 }; |
|
92 |
|
93 var_type type_tag; |
2985
|
94 |
1
|
95 size_t size; |
|
96 }; |
|
97 |
|
98 saved_variable::saved_variable (void) |
|
99 { |
529
|
100 gen_ptr = 0; |
|
101 gen_ptr_value = 0; |
1
|
102 type_tag = generic; |
|
103 size = 0; |
|
104 } |
|
105 |
2985
|
106 saved_variable::saved_variable (bool *p, bool v) |
|
107 { |
3018
|
108 type_tag = boolean; |
2985
|
109 ptr_to_bool = p; |
|
110 bool_value = v; |
|
111 size = sizeof (bool); // Is this necessary? |
|
112 } |
|
113 |
1
|
114 saved_variable::saved_variable (int *p, int v) |
|
115 { |
|
116 type_tag = integer; |
|
117 ptr_to_int = p; |
|
118 int_value = v; |
1755
|
119 size = sizeof (int); // Is this necessary? |
|
120 } |
|
121 |
|
122 saved_variable::saved_variable (string *p, const string& v) |
|
123 { |
|
124 type_tag = string_type; |
|
125 gen_ptr = p; |
|
126 str_value = new string (v); |
|
127 size = sizeof (string); // Is this necessary? |
1
|
128 } |
|
129 |
|
130 saved_variable::saved_variable (void **p, void *v) |
|
131 { |
|
132 type_tag = generic_ptr; |
|
133 ptr_to_gen_ptr = p; |
|
134 gen_ptr_value = v; |
|
135 size = sizeof (void *); |
|
136 } |
|
137 |
|
138 saved_variable::~saved_variable (void) |
|
139 { |
1755
|
140 switch (type_tag) |
|
141 { |
|
142 case string_type: |
|
143 delete str_value; |
|
144 break; |
|
145 |
|
146 case generic: |
|
147 delete [] gen_ptr_value; // Can this be right? |
|
148 break; |
|
149 |
|
150 default: |
|
151 break; |
|
152 } |
1
|
153 } |
|
154 |
|
155 void |
|
156 saved_variable::restore_value (void) |
|
157 { |
|
158 switch (type_tag) |
|
159 { |
2985
|
160 case boolean: |
|
161 *ptr_to_bool = bool_value; |
|
162 break; |
|
163 |
1
|
164 case integer: |
|
165 *ptr_to_int = int_value; |
|
166 break; |
777
|
167 |
1755
|
168 case string_type: |
2800
|
169 (static_cast<string *> (gen_ptr)) -> assign (*str_value); |
1755
|
170 break; |
|
171 |
1
|
172 case generic_ptr: |
|
173 *ptr_to_gen_ptr = gen_ptr_value; |
|
174 break; |
777
|
175 |
1
|
176 case generic: |
|
177 memcpy (gen_ptr, gen_ptr_value, size); |
|
178 break; |
777
|
179 |
1
|
180 default: |
|
181 panic_impossible (); |
777
|
182 break; |
1
|
183 } |
|
184 } |
|
185 |
2985
|
186 void |
|
187 saved_variable::restore (void *s) |
1
|
188 { |
2800
|
189 saved_variable *sv = static_cast<saved_variable *> (s); |
1
|
190 sv->restore_value (); |
|
191 delete sv; |
|
192 } |
|
193 |
|
194 void |
2985
|
195 unwind_protect::add (unwind_elem::cleanup_func fptr, void *ptr) |
|
196 { |
|
197 unwind_elem el (fptr, ptr); |
|
198 list.push (el); |
|
199 } |
|
200 |
|
201 void |
|
202 unwind_protect::run (void) |
|
203 { |
|
204 unwind_elem el = list.pop (); |
|
205 |
|
206 unwind_elem::cleanup_func f = el.fptr (); |
|
207 |
|
208 if (f) |
|
209 f (el.ptr ()); |
|
210 } |
|
211 |
|
212 void |
|
213 unwind_protect::discard (void) |
1
|
214 { |
2985
|
215 list.pop (); |
|
216 } |
|
217 |
|
218 void |
|
219 unwind_protect::begin_frame (const string& tag) |
|
220 { |
|
221 unwind_elem elem (tag); |
|
222 list.push (elem); |
|
223 } |
|
224 |
|
225 void |
|
226 unwind_protect::run_frame (const string& tag) |
|
227 { |
|
228 while (! list.empty ()) |
|
229 { |
|
230 unwind_elem el = list.pop (); |
|
231 |
|
232 unwind_elem::cleanup_func f = el.fptr (); |
|
233 |
|
234 if (f) |
|
235 f (el.ptr ()); |
|
236 |
|
237 if (tag == el.tag ()) |
|
238 break; |
|
239 } |
1
|
240 } |
|
241 |
|
242 void |
2985
|
243 unwind_protect::discard_frame (const string& tag) |
1755
|
244 { |
2985
|
245 while (! list.empty ()) |
|
246 { |
|
247 unwind_elem el = list.pop (); |
|
248 |
|
249 if (tag == el.tag ()) |
|
250 break; |
|
251 } |
|
252 } |
|
253 |
|
254 void |
|
255 unwind_protect::run_all (void) |
|
256 { |
|
257 while (! list.empty ()) |
|
258 { |
|
259 unwind_elem el = list.pop (); |
|
260 |
|
261 unwind_elem::cleanup_func f = el.fptr (); |
|
262 |
|
263 if (f) |
|
264 f (el.ptr ()); |
|
265 } |
1755
|
266 } |
|
267 |
|
268 void |
2985
|
269 unwind_protect::discard_all (void) |
|
270 { |
|
271 list.clear (); |
|
272 } |
|
273 |
|
274 void |
|
275 unwind_protect::save_bool (bool *ptr, bool value) |
|
276 { |
|
277 saved_variable *s = new saved_variable (ptr, value); |
|
278 add (saved_variable::restore, s); |
|
279 } |
|
280 |
|
281 void |
|
282 unwind_protect::save_int (int *ptr, int value) |
1
|
283 { |
|
284 saved_variable *s = new saved_variable (ptr, value); |
2985
|
285 add (saved_variable::restore, s); |
|
286 } |
|
287 |
|
288 void |
|
289 unwind_protect::save_str (string *ptr, const string& value) |
|
290 { |
|
291 saved_variable *s = new saved_variable (ptr, value); |
|
292 add (saved_variable::restore, s); |
|
293 } |
|
294 |
|
295 void |
|
296 unwind_protect::save_ptr (void **ptr, void *value) |
|
297 { |
|
298 saved_variable *s = new saved_variable (ptr, value); |
|
299 add (saved_variable::restore, s); |
1
|
300 } |
|
301 |
|
302 /* |
|
303 ;;; Local Variables: *** |
|
304 ;;; mode: C++ *** |
|
305 ;;; End: *** |
|
306 */ |