Mercurial > hg > octave-nkf
annotate src/unwind-prot.cc @ 8771:d3382daaf4d2
Use CARBON_LIBS instead of LIBS for framework Carbon.
author | Thomas Treichl <Thomas.Treichl@gmx.net> |
---|---|
date | Tue, 17 Feb 2009 00:28:48 -0500 |
parents | 40c428ea3408 |
children | eb63fbe60fab |
rev | line source |
---|---|
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, | |
7752
40c428ea3408
initial implementation of dbup and dbdown
John W. Eaton <jwe@octave.org>
parents:
7048
diff
changeset
|
48 size_type, |
2985 | 49 string_type, |
50 generic_ptr, | |
51 generic | |
52 }; | |
1 | 53 |
54 saved_variable (void); | |
2985 | 55 |
56 saved_variable (bool *p, bool v); | |
57 | |
1 | 58 saved_variable (int *p, int v); |
2985 | 59 |
7752
40c428ea3408
initial implementation of dbup and dbdown
John W. Eaton <jwe@octave.org>
parents:
7048
diff
changeset
|
60 saved_variable (size_t *p, size_t v); |
40c428ea3408
initial implementation of dbup and dbdown
John W. Eaton <jwe@octave.org>
parents:
7048
diff
changeset
|
61 |
3523 | 62 saved_variable (std::string *p, const std::string& v); |
2985 | 63 |
1 | 64 saved_variable (void **p, void *v); |
2985 | 65 |
1 | 66 ~saved_variable (void); |
67 | |
68 void restore_value (void); | |
69 | |
2985 | 70 static void restore (void *s); |
71 | |
72 private: | |
73 | |
1 | 74 union |
75 { | |
2985 | 76 bool *ptr_to_bool; |
1 | 77 int *ptr_to_int; |
7752
40c428ea3408
initial implementation of dbup and dbdown
John W. Eaton <jwe@octave.org>
parents:
7048
diff
changeset
|
78 size_t *ptr_to_size_t; |
1 | 79 void *gen_ptr; |
80 void **ptr_to_gen_ptr; | |
81 }; | |
82 | |
83 union | |
84 { | |
2985 | 85 bool bool_value; |
1 | 86 int int_value; |
7752
40c428ea3408
initial implementation of dbup and dbdown
John W. Eaton <jwe@octave.org>
parents:
7048
diff
changeset
|
87 size_t size_t_value; |
5760 | 88 std::string *str_value; |
1 | 89 void *gen_ptr_value; |
90 }; | |
91 | |
92 var_type type_tag; | |
2985 | 93 |
1 | 94 size_t size; |
95 }; | |
96 | |
97 saved_variable::saved_variable (void) | |
98 { | |
529 | 99 gen_ptr = 0; |
100 gen_ptr_value = 0; | |
1 | 101 type_tag = generic; |
102 size = 0; | |
103 } | |
104 | |
2985 | 105 saved_variable::saved_variable (bool *p, bool v) |
106 { | |
3018 | 107 type_tag = boolean; |
2985 | 108 ptr_to_bool = p; |
109 bool_value = v; | |
110 size = sizeof (bool); // Is this necessary? | |
111 } | |
112 | |
1 | 113 saved_variable::saved_variable (int *p, int v) |
114 { | |
115 type_tag = integer; | |
116 ptr_to_int = p; | |
117 int_value = v; | |
1755 | 118 size = sizeof (int); // Is this necessary? |
119 } | |
120 | |
7752
40c428ea3408
initial implementation of dbup and dbdown
John W. Eaton <jwe@octave.org>
parents:
7048
diff
changeset
|
121 saved_variable::saved_variable (size_t *p, size_t v) |
40c428ea3408
initial implementation of dbup and dbdown
John W. Eaton <jwe@octave.org>
parents:
7048
diff
changeset
|
122 { |
40c428ea3408
initial implementation of dbup and dbdown
John W. Eaton <jwe@octave.org>
parents:
7048
diff
changeset
|
123 type_tag = size_type; |
40c428ea3408
initial implementation of dbup and dbdown
John W. Eaton <jwe@octave.org>
parents:
7048
diff
changeset
|
124 ptr_to_size_t = p; |
40c428ea3408
initial implementation of dbup and dbdown
John W. Eaton <jwe@octave.org>
parents:
7048
diff
changeset
|
125 size_t_value = v; |
40c428ea3408
initial implementation of dbup and dbdown
John W. Eaton <jwe@octave.org>
parents:
7048
diff
changeset
|
126 size = sizeof (size_t); // Is this necessary? |
40c428ea3408
initial implementation of dbup and dbdown
John W. Eaton <jwe@octave.org>
parents:
7048
diff
changeset
|
127 } |
40c428ea3408
initial implementation of dbup and dbdown
John W. Eaton <jwe@octave.org>
parents:
7048
diff
changeset
|
128 |
3523 | 129 saved_variable::saved_variable (std::string *p, const std::string& v) |
1755 | 130 { |
131 type_tag = string_type; | |
132 gen_ptr = p; | |
3523 | 133 str_value = new std::string (v); |
3549 | 134 size = sizeof (std::string); // Is this necessary? |
1 | 135 } |
136 | |
137 saved_variable::saved_variable (void **p, void *v) | |
138 { | |
139 type_tag = generic_ptr; | |
140 ptr_to_gen_ptr = p; | |
141 gen_ptr_value = v; | |
142 size = sizeof (void *); | |
143 } | |
144 | |
145 saved_variable::~saved_variable (void) | |
146 { | |
1755 | 147 switch (type_tag) |
148 { | |
149 case string_type: | |
150 delete str_value; | |
151 break; | |
152 | |
153 case generic: | |
5775 | 154 // FIXME |
3263 | 155 // delete [] gen_ptr_value; |
1755 | 156 break; |
157 | |
158 default: | |
159 break; | |
160 } | |
1 | 161 } |
162 | |
163 void | |
164 saved_variable::restore_value (void) | |
165 { | |
166 switch (type_tag) | |
167 { | |
2985 | 168 case boolean: |
169 *ptr_to_bool = bool_value; | |
170 break; | |
171 | |
1 | 172 case integer: |
173 *ptr_to_int = int_value; | |
174 break; | |
777 | 175 |
7752
40c428ea3408
initial implementation of dbup and dbdown
John W. Eaton <jwe@octave.org>
parents:
7048
diff
changeset
|
176 case size_type: |
40c428ea3408
initial implementation of dbup and dbdown
John W. Eaton <jwe@octave.org>
parents:
7048
diff
changeset
|
177 *ptr_to_size_t = size_t_value; |
40c428ea3408
initial implementation of dbup and dbdown
John W. Eaton <jwe@octave.org>
parents:
7048
diff
changeset
|
178 break; |
40c428ea3408
initial implementation of dbup and dbdown
John W. Eaton <jwe@octave.org>
parents:
7048
diff
changeset
|
179 |
1755 | 180 case string_type: |
3523 | 181 (static_cast<std::string *> (gen_ptr)) -> assign (*str_value); |
1755 | 182 break; |
183 | |
1 | 184 case generic_ptr: |
185 *ptr_to_gen_ptr = gen_ptr_value; | |
186 break; | |
777 | 187 |
1 | 188 case generic: |
189 memcpy (gen_ptr, gen_ptr_value, size); | |
190 break; | |
777 | 191 |
1 | 192 default: |
193 panic_impossible (); | |
777 | 194 break; |
1 | 195 } |
196 } | |
197 | |
2985 | 198 void |
199 saved_variable::restore (void *s) | |
1 | 200 { |
2800 | 201 saved_variable *sv = static_cast<saved_variable *> (s); |
1 | 202 sv->restore_value (); |
203 delete sv; | |
204 } | |
205 | |
206 void | |
2985 | 207 unwind_protect::add (unwind_elem::cleanup_func fptr, void *ptr) |
208 { | |
209 unwind_elem el (fptr, ptr); | |
4214 | 210 elt_list.push (el); |
2985 | 211 } |
212 | |
213 void | |
214 unwind_protect::run (void) | |
215 { | |
4214 | 216 unwind_elem el = elt_list.top (); |
217 | |
218 elt_list.pop (); | |
2985 | 219 |
220 unwind_elem::cleanup_func f = el.fptr (); | |
221 | |
222 if (f) | |
223 f (el.ptr ()); | |
224 } | |
225 | |
226 void | |
227 unwind_protect::discard (void) | |
1 | 228 { |
4214 | 229 elt_list.pop (); |
2985 | 230 } |
231 | |
232 void | |
3523 | 233 unwind_protect::begin_frame (const std::string& tag) |
2985 | 234 { |
235 unwind_elem elem (tag); | |
4214 | 236 elt_list.push (elem); |
2985 | 237 } |
238 | |
239 void | |
3523 | 240 unwind_protect::run_frame (const std::string& tag) |
2985 | 241 { |
4214 | 242 while (! elt_list.empty ()) |
2985 | 243 { |
4214 | 244 unwind_elem el = elt_list.top (); |
245 | |
246 elt_list.pop (); | |
2985 | 247 |
248 unwind_elem::cleanup_func f = el.fptr (); | |
249 | |
250 if (f) | |
251 f (el.ptr ()); | |
252 | |
253 if (tag == el.tag ()) | |
254 break; | |
255 } | |
1 | 256 } |
257 | |
258 void | |
3523 | 259 unwind_protect::discard_frame (const std::string& tag) |
1755 | 260 { |
4214 | 261 while (! elt_list.empty ()) |
2985 | 262 { |
4214 | 263 unwind_elem el = elt_list.top (); |
264 | |
265 elt_list.pop (); | |
2985 | 266 |
267 if (tag == el.tag ()) | |
268 break; | |
269 } | |
270 } | |
271 | |
272 void | |
273 unwind_protect::run_all (void) | |
274 { | |
4214 | 275 while (! elt_list.empty ()) |
2985 | 276 { |
4214 | 277 unwind_elem el = elt_list.top (); |
278 | |
279 elt_list.pop (); | |
2985 | 280 |
281 unwind_elem::cleanup_func f = el.fptr (); | |
282 | |
283 if (f) | |
284 f (el.ptr ()); | |
285 } | |
1755 | 286 } |
287 | |
288 void | |
2985 | 289 unwind_protect::discard_all (void) |
290 { | |
4214 | 291 while (! elt_list.empty ()) |
292 elt_list.pop (); | |
2985 | 293 } |
294 | |
295 void | |
296 unwind_protect::save_bool (bool *ptr, bool value) | |
297 { | |
298 saved_variable *s = new saved_variable (ptr, value); | |
299 add (saved_variable::restore, s); | |
300 } | |
301 | |
302 void | |
303 unwind_protect::save_int (int *ptr, int value) | |
1 | 304 { |
305 saved_variable *s = new saved_variable (ptr, value); | |
2985 | 306 add (saved_variable::restore, s); |
307 } | |
308 | |
309 void | |
7752
40c428ea3408
initial implementation of dbup and dbdown
John W. Eaton <jwe@octave.org>
parents:
7048
diff
changeset
|
310 unwind_protect::save_size_t (size_t *ptr, size_t value) |
40c428ea3408
initial implementation of dbup and dbdown
John W. Eaton <jwe@octave.org>
parents:
7048
diff
changeset
|
311 { |
40c428ea3408
initial implementation of dbup and dbdown
John W. Eaton <jwe@octave.org>
parents:
7048
diff
changeset
|
312 saved_variable *s = new saved_variable (ptr, value); |
40c428ea3408
initial implementation of dbup and dbdown
John W. Eaton <jwe@octave.org>
parents:
7048
diff
changeset
|
313 add (saved_variable::restore, s); |
40c428ea3408
initial implementation of dbup and dbdown
John W. Eaton <jwe@octave.org>
parents:
7048
diff
changeset
|
314 } |
40c428ea3408
initial implementation of dbup and dbdown
John W. Eaton <jwe@octave.org>
parents:
7048
diff
changeset
|
315 |
40c428ea3408
initial implementation of dbup and dbdown
John W. Eaton <jwe@octave.org>
parents:
7048
diff
changeset
|
316 void |
3523 | 317 unwind_protect::save_str (std::string *ptr, const std::string& value) |
2985 | 318 { |
319 saved_variable *s = new saved_variable (ptr, value); | |
320 add (saved_variable::restore, s); | |
321 } | |
322 | |
323 void | |
324 unwind_protect::save_ptr (void **ptr, void *value) | |
325 { | |
326 saved_variable *s = new saved_variable (ptr, value); | |
327 add (saved_variable::restore, s); | |
1 | 328 } |
329 | |
330 /* | |
331 ;;; Local Variables: *** | |
332 ;;; mode: C++ *** | |
333 ;;; End: *** | |
334 */ |