1
|
1 /* |
|
2 |
7017
|
3 Copyright (C) 1993, 1994, 1995, 1996, 1997, 1999, 2000, 2002, 2004, |
|
4 2005, 2006, 2007 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 |
7016
|
10 Free Software Foundation; either version 3 of the License, or (at your |
|
11 option) any later version. |
1
|
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 |
7016
|
19 along with Octave; see the file COPYING. If not, see |
|
20 <http://www.gnu.org/licenses/>. |
1
|
21 |
|
22 */ |
|
23 |
240
|
24 #ifdef HAVE_CONFIG_H |
1192
|
25 #include <config.h> |
1
|
26 #endif |
|
27 |
1343
|
28 #include <cstddef> |
7048
|
29 #include <cstring> |
1
|
30 |
453
|
31 #include "CMatrix.h" |
1
|
32 |
1352
|
33 #include "error.h" |
1
|
34 #include "unwind-prot.h" |
|
35 #include "utils.h" |
|
36 |
4214
|
37 std::stack<unwind_elem> unwind_protect::elt_list; |
1
|
38 |
2985
|
39 class |
|
40 saved_variable |
1
|
41 { |
2985
|
42 public: |
1755
|
43 |
2985
|
44 enum var_type |
|
45 { |
|
46 boolean, |
|
47 integer, |
|
48 string_type, |
|
49 generic_ptr, |
|
50 generic |
|
51 }; |
1
|
52 |
|
53 saved_variable (void); |
2985
|
54 |
|
55 saved_variable (bool *p, bool v); |
|
56 |
1
|
57 saved_variable (int *p, int v); |
2985
|
58 |
3523
|
59 saved_variable (std::string *p, const std::string& v); |
2985
|
60 |
1
|
61 saved_variable (void **p, void *v); |
2985
|
62 |
1
|
63 ~saved_variable (void); |
|
64 |
|
65 void restore_value (void); |
|
66 |
2985
|
67 static void restore (void *s); |
|
68 |
|
69 private: |
|
70 |
1
|
71 union |
|
72 { |
2985
|
73 bool *ptr_to_bool; |
1
|
74 int *ptr_to_int; |
|
75 void *gen_ptr; |
|
76 void **ptr_to_gen_ptr; |
|
77 }; |
|
78 |
|
79 union |
|
80 { |
2985
|
81 bool bool_value; |
1
|
82 int int_value; |
5760
|
83 std::string *str_value; |
1
|
84 void *gen_ptr_value; |
|
85 }; |
|
86 |
|
87 var_type type_tag; |
2985
|
88 |
1
|
89 size_t size; |
|
90 }; |
|
91 |
|
92 saved_variable::saved_variable (void) |
|
93 { |
529
|
94 gen_ptr = 0; |
|
95 gen_ptr_value = 0; |
1
|
96 type_tag = generic; |
|
97 size = 0; |
|
98 } |
|
99 |
2985
|
100 saved_variable::saved_variable (bool *p, bool v) |
|
101 { |
3018
|
102 type_tag = boolean; |
2985
|
103 ptr_to_bool = p; |
|
104 bool_value = v; |
|
105 size = sizeof (bool); // Is this necessary? |
|
106 } |
|
107 |
1
|
108 saved_variable::saved_variable (int *p, int v) |
|
109 { |
|
110 type_tag = integer; |
|
111 ptr_to_int = p; |
|
112 int_value = v; |
1755
|
113 size = sizeof (int); // Is this necessary? |
|
114 } |
|
115 |
3523
|
116 saved_variable::saved_variable (std::string *p, const std::string& v) |
1755
|
117 { |
|
118 type_tag = string_type; |
|
119 gen_ptr = p; |
3523
|
120 str_value = new std::string (v); |
3549
|
121 size = sizeof (std::string); // Is this necessary? |
1
|
122 } |
|
123 |
|
124 saved_variable::saved_variable (void **p, void *v) |
|
125 { |
|
126 type_tag = generic_ptr; |
|
127 ptr_to_gen_ptr = p; |
|
128 gen_ptr_value = v; |
|
129 size = sizeof (void *); |
|
130 } |
|
131 |
|
132 saved_variable::~saved_variable (void) |
|
133 { |
1755
|
134 switch (type_tag) |
|
135 { |
|
136 case string_type: |
|
137 delete str_value; |
|
138 break; |
|
139 |
|
140 case generic: |
5775
|
141 // FIXME |
3263
|
142 // delete [] gen_ptr_value; |
1755
|
143 break; |
|
144 |
|
145 default: |
|
146 break; |
|
147 } |
1
|
148 } |
|
149 |
|
150 void |
|
151 saved_variable::restore_value (void) |
|
152 { |
|
153 switch (type_tag) |
|
154 { |
2985
|
155 case boolean: |
|
156 *ptr_to_bool = bool_value; |
|
157 break; |
|
158 |
1
|
159 case integer: |
|
160 *ptr_to_int = int_value; |
|
161 break; |
777
|
162 |
1755
|
163 case string_type: |
3523
|
164 (static_cast<std::string *> (gen_ptr)) -> assign (*str_value); |
1755
|
165 break; |
|
166 |
1
|
167 case generic_ptr: |
|
168 *ptr_to_gen_ptr = gen_ptr_value; |
|
169 break; |
777
|
170 |
1
|
171 case generic: |
|
172 memcpy (gen_ptr, gen_ptr_value, size); |
|
173 break; |
777
|
174 |
1
|
175 default: |
|
176 panic_impossible (); |
777
|
177 break; |
1
|
178 } |
|
179 } |
|
180 |
2985
|
181 void |
|
182 saved_variable::restore (void *s) |
1
|
183 { |
2800
|
184 saved_variable *sv = static_cast<saved_variable *> (s); |
1
|
185 sv->restore_value (); |
|
186 delete sv; |
|
187 } |
|
188 |
|
189 void |
2985
|
190 unwind_protect::add (unwind_elem::cleanup_func fptr, void *ptr) |
|
191 { |
|
192 unwind_elem el (fptr, ptr); |
4214
|
193 elt_list.push (el); |
2985
|
194 } |
|
195 |
|
196 void |
|
197 unwind_protect::run (void) |
|
198 { |
4214
|
199 unwind_elem el = elt_list.top (); |
|
200 |
|
201 elt_list.pop (); |
2985
|
202 |
|
203 unwind_elem::cleanup_func f = el.fptr (); |
|
204 |
|
205 if (f) |
|
206 f (el.ptr ()); |
|
207 } |
|
208 |
|
209 void |
|
210 unwind_protect::discard (void) |
1
|
211 { |
4214
|
212 elt_list.pop (); |
2985
|
213 } |
|
214 |
|
215 void |
3523
|
216 unwind_protect::begin_frame (const std::string& tag) |
2985
|
217 { |
|
218 unwind_elem elem (tag); |
4214
|
219 elt_list.push (elem); |
2985
|
220 } |
|
221 |
|
222 void |
3523
|
223 unwind_protect::run_frame (const std::string& tag) |
2985
|
224 { |
4214
|
225 while (! elt_list.empty ()) |
2985
|
226 { |
4214
|
227 unwind_elem el = elt_list.top (); |
|
228 |
|
229 elt_list.pop (); |
2985
|
230 |
|
231 unwind_elem::cleanup_func f = el.fptr (); |
|
232 |
|
233 if (f) |
|
234 f (el.ptr ()); |
|
235 |
|
236 if (tag == el.tag ()) |
|
237 break; |
|
238 } |
1
|
239 } |
|
240 |
|
241 void |
3523
|
242 unwind_protect::discard_frame (const std::string& tag) |
1755
|
243 { |
4214
|
244 while (! elt_list.empty ()) |
2985
|
245 { |
4214
|
246 unwind_elem el = elt_list.top (); |
|
247 |
|
248 elt_list.pop (); |
2985
|
249 |
|
250 if (tag == el.tag ()) |
|
251 break; |
|
252 } |
|
253 } |
|
254 |
|
255 void |
|
256 unwind_protect::run_all (void) |
|
257 { |
4214
|
258 while (! elt_list.empty ()) |
2985
|
259 { |
4214
|
260 unwind_elem el = elt_list.top (); |
|
261 |
|
262 elt_list.pop (); |
2985
|
263 |
|
264 unwind_elem::cleanup_func f = el.fptr (); |
|
265 |
|
266 if (f) |
|
267 f (el.ptr ()); |
|
268 } |
1755
|
269 } |
|
270 |
|
271 void |
2985
|
272 unwind_protect::discard_all (void) |
|
273 { |
4214
|
274 while (! elt_list.empty ()) |
|
275 elt_list.pop (); |
2985
|
276 } |
|
277 |
|
278 void |
|
279 unwind_protect::save_bool (bool *ptr, bool value) |
|
280 { |
|
281 saved_variable *s = new saved_variable (ptr, value); |
|
282 add (saved_variable::restore, s); |
|
283 } |
|
284 |
|
285 void |
|
286 unwind_protect::save_int (int *ptr, int value) |
1
|
287 { |
|
288 saved_variable *s = new saved_variable (ptr, value); |
2985
|
289 add (saved_variable::restore, s); |
|
290 } |
|
291 |
|
292 void |
3523
|
293 unwind_protect::save_str (std::string *ptr, const std::string& value) |
2985
|
294 { |
|
295 saved_variable *s = new saved_variable (ptr, value); |
|
296 add (saved_variable::restore, s); |
|
297 } |
|
298 |
|
299 void |
|
300 unwind_protect::save_ptr (void **ptr, void *value) |
|
301 { |
|
302 saved_variable *s = new saved_variable (ptr, value); |
|
303 add (saved_variable::restore, s); |
1
|
304 } |
|
305 |
|
306 /* |
|
307 ;;; Local Variables: *** |
|
308 ;;; mode: C++ *** |
|
309 ;;; End: *** |
|
310 */ |