1 /* unwind_prot.c - a simple unwind-protect system for internal variables */
3 /* I can't stand it anymore! Please can't we just write the
4 whole Unix system in lisp or something? */
6 /* Copyright (C) 1987-2009 Free Software Foundation, Inc.
8 This file is part of GNU Bash, the Bourne Again SHell.
10 Bash is free software: you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation, either version 3 of the License, or
13 (at your option) any later version.
15 Bash is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with Bash. If not, see <http://www.gnu.org/licenses/>.
24 /* **************************************************************** */
26 /* Unwind Protection Scheme for Bash */
28 /* **************************************************************** */
31 #include "bashtypes.h"
34 #if defined (HAVE_UNISTD_H)
43 # define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
48 #include "unwind_prot.h"
52 /* Structure describing a saved variable and the value to restore it to. */
56 char desired_setting[1]; /* actual size is `size' */
59 /* If HEAD.CLEANUP is null, then ARG.V contains a tag to throw back to.
60 If HEAD.CLEANUP is restore_variable, then SV.V contains the saved
61 variable. Otherwise, call HEAD.CLEANUP (ARG.V) to clean up. */
68 struct uwp_head uwp_head;
72 struct uwp_head uwp_head;
78 static void without_interrupts __P((VFunction *, char *, char *));
79 static void unwind_frame_discard_internal __P((char *, char *));
80 static void unwind_frame_run_internal __P((char *, char *));
81 static void add_unwind_protect_internal __P((Function *, char *));
82 static void remove_unwind_protect_internal __P((char *, char *));
83 static void run_unwind_protects_internal __P((char *, char *));
84 static void clear_unwind_protects_internal __P((char *, char *));
85 static inline void restore_variable __P((SAVED_VAR *));
86 static void unwind_protect_mem_internal __P((char *, char *));
88 static UNWIND_ELT *unwind_protect_list = (UNWIND_ELT *)NULL;
90 #define uwpalloc(elt) (elt) = (UNWIND_ELT *)xmalloc (sizeof (UNWIND_ELT))
91 #define uwpfree(elt) free(elt)
93 /* Run a function without interrupts. This relies on the fact that the
94 FUNCTION cannot change the value of interrupt_immediately. (I.e., does
97 without_interrupts (function, arg1, arg2)
101 int old_interrupt_immediately;
103 old_interrupt_immediately = interrupt_immediately;
104 interrupt_immediately = 0;
106 (*function)(arg1, arg2);
108 interrupt_immediately = old_interrupt_immediately;
111 /* Start the beginning of a region. */
113 begin_unwind_frame (tag)
116 add_unwind_protect ((Function *)NULL, tag);
119 /* Discard the unwind protects back to TAG. */
121 discard_unwind_frame (tag)
124 if (unwind_protect_list)
125 without_interrupts (unwind_frame_discard_internal, tag, (char *)NULL);
128 /* Run the unwind protects back to TAG. */
130 run_unwind_frame (tag)
133 if (unwind_protect_list)
134 without_interrupts (unwind_frame_run_internal, tag, (char *)NULL);
137 /* Add the function CLEANUP with ARG to the list of unwindable things. */
139 add_unwind_protect (cleanup, arg)
143 without_interrupts (add_unwind_protect_internal, (char *)cleanup, arg);
146 /* Remove the top unwind protect from the list. */
148 remove_unwind_protect ()
150 if (unwind_protect_list)
152 (remove_unwind_protect_internal, (char *)NULL, (char *)NULL);
155 /* Run the list of cleanup functions in unwind_protect_list. */
157 run_unwind_protects ()
159 if (unwind_protect_list)
161 (run_unwind_protects_internal, (char *)NULL, (char *)NULL);
164 /* Erase the unwind-protect list. If flags is 1, free the elements. */
166 clear_unwind_protect_list (flags)
171 if (unwind_protect_list)
173 flag = flags ? "" : (char *)NULL;
175 (clear_unwind_protects_internal, flag, (char *)NULL);
180 have_unwind_protects ()
182 return (unwind_protect_list != 0);
185 /* **************************************************************** */
187 /* The Actual Functions */
189 /* **************************************************************** */
192 add_unwind_protect_internal (cleanup, arg)
199 elt->head.next = unwind_protect_list;
200 elt->head.cleanup = cleanup;
202 unwind_protect_list = elt;
206 remove_unwind_protect_internal (ignore1, ignore2)
207 char *ignore1, *ignore2;
211 elt = unwind_protect_list;
214 unwind_protect_list = unwind_protect_list->head.next;
220 run_unwind_protects_internal (ignore1, ignore2)
221 char *ignore1, *ignore2;
223 unwind_frame_run_internal ((char *) NULL, (char *) NULL);
227 clear_unwind_protects_internal (flag, ignore)
232 while (unwind_protect_list)
233 remove_unwind_protect_internal ((char *)NULL, (char *)NULL);
235 unwind_protect_list = (UNWIND_ELT *)NULL;
239 unwind_frame_discard_internal (tag, ignore)
244 while (elt = unwind_protect_list)
246 unwind_protect_list = unwind_protect_list->head.next;
247 if (elt->head.cleanup == 0 && (STREQ (elt->arg.v, tag)))
257 /* Restore the value of a variable, based on the contents of SV.
258 sv->desired_setting is a block of memory SIZE bytes long holding the
259 value itself. This block of memory is copied back into the variable. */
261 restore_variable (sv)
264 FASTCOPY (sv->desired_setting, sv->variable, sv->size);
268 unwind_frame_run_internal (tag, ignore)
273 while (elt = unwind_protect_list)
275 unwind_protect_list = elt->head.next;
277 /* If tag, then compare. */
278 if (!elt->head.cleanup)
280 if (tag && STREQ (elt->arg.v, tag))
288 if (elt->head.cleanup == (Function *) restore_variable)
289 restore_variable (&elt->sv.v);
291 (*(elt->head.cleanup)) (elt->arg.v);
299 unwind_protect_mem_internal (var, psize)
306 size = *(int *) psize;
307 allocated = size + offsetof (UNWIND_ELT, sv.v.desired_setting[0]);
308 elt = (UNWIND_ELT *)xmalloc (allocated);
309 elt->head.next = unwind_protect_list;
310 elt->head.cleanup = (Function *) restore_variable;
311 elt->sv.v.variable = var;
312 elt->sv.v.size = size;
313 FASTCOPY (var, elt->sv.v.desired_setting, size);
314 unwind_protect_list = elt;
317 /* Save the value of a variable so it will be restored when unwind-protects
318 are run. VAR is a pointer to the variable. SIZE is the size in
321 unwind_protect_mem (var, size)
325 without_interrupts (unwind_protect_mem_internal, var, (char *) &size);