Mercurial > octave
annotate src/unwind-prot.cc @ 9376:d58086453171
refactor unwind_protect
author | Jaroslav Hajek <highegg@gmail.com> |
---|---|
date | Tue, 23 Jun 2009 07:55:44 +0200 |
parents | eb63fbe60fab |
children | b3ab22ee8544 |
rev | line source |
---|---|
1 | 1 /* |
2 | |
7017 | 3 Copyright (C) 1993, 1994, 1995, 1996, 1997, 1999, 2000, 2002, 2004, |
8920 | 4 2005, 2006, 2007, 2008 John W. Eaton |
9376
d58086453171
refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
5 Copyright (C) 2009 VZLU Prague |
1 | 6 |
7 This file is part of Octave. | |
8 | |
9 Octave is free software; you can redistribute it and/or modify it | |
10 under the terms of the GNU General Public License as published by the | |
7016 | 11 Free Software Foundation; either version 3 of the License, or (at your |
12 option) any later version. | |
1 | 13 |
14 Octave is distributed in the hope that it will be useful, but WITHOUT | |
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
17 for more details. | |
18 | |
19 You should have received a copy of the GNU General Public License | |
7016 | 20 along with Octave; see the file COPYING. If not, see |
21 <http://www.gnu.org/licenses/>. | |
1 | 22 |
23 */ | |
24 | |
240 | 25 #ifdef HAVE_CONFIG_H |
1192 | 26 #include <config.h> |
1 | 27 #endif |
28 | |
1343 | 29 #include <cstddef> |
7048 | 30 #include <cstring> |
1 | 31 |
1352 | 32 #include "error.h" |
1 | 33 #include "unwind-prot.h" |
34 #include "utils.h" | |
35 | |
9376
d58086453171
refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
36 std::stack<unwind_protect::elem> unwind_protect::elt_list; |
1 | 37 |
9376
d58086453171
refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
38 std::stack<std::pair <std::string, unwind_protect::frame_id_t> > unwind_protect::tag_list; |
2985 | 39 |
9376
d58086453171
refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
40 unwind_protect::restore_mem::restore_mem (void *ptr, size_t size) |
d58086453171
refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
41 : rptr (ptr), sptr (reinterpret_cast<void *> (new char[size])), rsize (size) |
1 | 42 { |
9376
d58086453171
refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
43 std::memcpy (sptr, rptr, size); |
7752
40c428ea3408
initial implementation of dbup and dbdown
John W. Eaton <jwe@octave.org>
parents:
7048
diff
changeset
|
44 } |
40c428ea3408
initial implementation of dbup and dbdown
John W. Eaton <jwe@octave.org>
parents:
7048
diff
changeset
|
45 |
9376
d58086453171
refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
46 unwind_protect::restore_mem::~restore_mem (void) |
1 | 47 { |
9376
d58086453171
refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
48 std::memcpy (rptr, sptr, rsize); |
d58086453171
refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
49 delete [] reinterpret_cast<char *> (sptr); |
2985 | 50 } |
51 | |
52 void | |
3523 | 53 unwind_protect::begin_frame (const std::string& tag) |
2985 | 54 { |
9376
d58086453171
refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
55 tag_list.push (std::make_pair (tag, begin_frame ())); |
2985 | 56 } |
57 | |
58 void | |
3523 | 59 unwind_protect::run_frame (const std::string& tag) |
2985 | 60 { |
9376
d58086453171
refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
61 while (! tag_list.empty ()) |
2985 | 62 { |
9376
d58086453171
refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
63 std::pair<std::string, frame_id_t> top = tag_list.top (); |
d58086453171
refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
64 tag_list.pop (); |
2985 | 65 |
9376
d58086453171
refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
66 run_frame (top.second); |
d58086453171
refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
67 if (top.first == tag) |
d58086453171
refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
68 break; |
2985 | 69 } |
1 | 70 } |
71 | |
72 void | |
3523 | 73 unwind_protect::discard_frame (const std::string& tag) |
1755 | 74 { |
9376
d58086453171
refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
75 while (! tag_list.empty ()) |
2985 | 76 { |
9376
d58086453171
refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
77 std::pair<std::string, frame_id_t> top = tag_list.top (); |
d58086453171
refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
78 tag_list.pop (); |
2985 | 79 |
9376
d58086453171
refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
80 run_frame (top.second); |
d58086453171
refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
81 if (top.first == tag) |
d58086453171
refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
82 break; |
d58086453171
refactor unwind_protect
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
83 } |
1 | 84 } |
85 | |
86 /* | |
87 ;;; Local Variables: *** | |
88 ;;; mode: C++ *** | |
89 ;;; End: *** | |
90 */ |