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-2020 Free Software Foundation, Inc.
8 This file is part of GNU Bush, the Bourne Again SHell.
10 Bush 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 Bush 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 Bush. If not, see <http://www.gnu.org/licenses/>.
24 /* **************************************************************** */
26 /* Unwind Protection Scheme for Bush */
28 /* **************************************************************** */
31 #include "bushtypes.h"
34 #if defined (HAVE_UNISTD_H)
43 # define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
48 #include "unwind_prot.h"
51 #include "error.h" /* for internal_warning */
54 /* Structure describing a saved variable and the value to restore it to. */
58 char desired_setting
[1]; /* actual size is `size' */
61 /* If HEAD.CLEANUP is null, then ARG.V contains a tag to throw back to.
62 If HEAD.CLEANUP is restore_variable, then SV.V contains the saved
63 variable. Otherwise, call HEAD.CLEANUP (ARG.V) to clean up. */
70 struct uwp_head uwp_head
;
74 struct uwp_head uwp_head
;
79 static void without_interrupts
PARAMS((VFunction
*, char *, char *));
80 static void unwind_frame_discard_internal
PARAMS((char *, char *));
81 static void unwind_frame_run_internal
PARAMS((char *, char *));
82 static void add_unwind_protect_internal
PARAMS((Function
*, char *));
83 static void remove_unwind_protect_internal
PARAMS((char *, char *));
84 static void run_unwind_protects_internal
PARAMS((char *, char *));
85 static void clear_unwind_protects_internal
PARAMS((char *, char *));
86 static inline void restore_variable
PARAMS((SAVED_VAR
*));
87 static void unwind_protect_mem_internal
PARAMS((char *, char *));
89 static UNWIND_ELT
*unwind_protect_list
= (UNWIND_ELT
*)NULL
;
91 /* Allocating from a cache of unwind-protect elements */
92 #define UWCACHESIZE 128
94 sh_obj_cache_t uwcache
= {0, 0, 0};
97 #define uwpalloc(elt) (elt) = (UNWIND_ELT *)xmalloc (sizeof (UNWIND_ELT))
98 #define uwpfree(elt) free(elt)
100 #define uwpalloc(elt) ocache_alloc (uwcache, UNWIND_ELT, elt)
101 #define uwpfree(elt) ocache_free (uwcache, UNWIND_ELT, elt)
107 ocache_create (uwcache
, UNWIND_ELT
, UWCACHESIZE
);
110 /* Run a function without interrupts. This relies on the fact that the
111 FUNCTION cannot call QUIT (). */
113 without_interrupts (function
, arg1
, arg2
)
117 (*function
)(arg1
, arg2
);
120 /* Start the beginning of a region. */
122 begin_unwind_frame (tag
)
125 add_unwind_protect ((Function
*)NULL
, tag
);
128 /* Discard the unwind protects back to TAG. */
130 discard_unwind_frame (tag
)
133 if (unwind_protect_list
)
134 without_interrupts (unwind_frame_discard_internal
, tag
, (char *)NULL
);
137 /* Run the unwind protects back to TAG. */
139 run_unwind_frame (tag
)
142 if (unwind_protect_list
)
143 without_interrupts (unwind_frame_run_internal
, tag
, (char *)NULL
);
146 /* Add the function CLEANUP with ARG to the list of unwindable things. */
148 add_unwind_protect (cleanup
, arg
)
152 without_interrupts (add_unwind_protect_internal
, (char *)cleanup
, arg
);
155 /* Remove the top unwind protect from the list. */
157 remove_unwind_protect ()
159 if (unwind_protect_list
)
161 (remove_unwind_protect_internal
, (char *)NULL
, (char *)NULL
);
164 /* Run the list of cleanup functions in unwind_protect_list. */
166 run_unwind_protects ()
168 if (unwind_protect_list
)
170 (run_unwind_protects_internal
, (char *)NULL
, (char *)NULL
);
173 /* Erase the unwind-protect list. If flags is 1, free the elements. */
175 clear_unwind_protect_list (flags
)
180 if (unwind_protect_list
)
182 flag
= flags
? "" : (char *)NULL
;
184 (clear_unwind_protects_internal
, flag
, (char *)NULL
);
189 have_unwind_protects ()
191 return (unwind_protect_list
!= 0);
195 unwind_protect_tag_on_stack (tag
)
200 elt
= unwind_protect_list
;
203 if (elt
->head
.cleanup
== 0 && STREQ (elt
->arg
.v
, tag
))
205 elt
= elt
->head
.next
;
210 /* **************************************************************** */
212 /* The Actual Functions */
214 /* **************************************************************** */
217 add_unwind_protect_internal (cleanup
, arg
)
224 elt
->head
.next
= unwind_protect_list
;
225 elt
->head
.cleanup
= cleanup
;
227 unwind_protect_list
= elt
;
231 remove_unwind_protect_internal (ignore1
, ignore2
)
232 char *ignore1
, *ignore2
;
236 elt
= unwind_protect_list
;
239 unwind_protect_list
= unwind_protect_list
->head
.next
;
245 run_unwind_protects_internal (ignore1
, ignore2
)
246 char *ignore1
, *ignore2
;
248 unwind_frame_run_internal ((char *) NULL
, (char *) NULL
);
252 clear_unwind_protects_internal (flag
, ignore
)
257 while (unwind_protect_list
)
258 remove_unwind_protect_internal ((char *)NULL
, (char *)NULL
);
260 unwind_protect_list
= (UNWIND_ELT
*)NULL
;
264 unwind_frame_discard_internal (tag
, ignore
)
271 while (elt
= unwind_protect_list
)
273 unwind_protect_list
= unwind_protect_list
->head
.next
;
274 if (elt
->head
.cleanup
== 0 && (STREQ (elt
->arg
.v
, tag
)))
285 internal_warning ("unwind_frame_discard: %s: frame not found", tag
);
288 /* Restore the value of a variable, based on the contents of SV.
289 sv->desired_setting is a block of memory SIZE bytes long holding the
290 value itself. This block of memory is copied back into the variable. */
292 restore_variable (sv
)
295 FASTCOPY (sv
->desired_setting
, sv
->variable
, sv
->size
);
299 unwind_frame_run_internal (tag
, ignore
)
306 while (elt
= unwind_protect_list
)
308 unwind_protect_list
= elt
->head
.next
;
310 /* If tag, then compare. */
311 if (elt
->head
.cleanup
== 0)
313 if (tag
&& STREQ (elt
->arg
.v
, tag
))
322 if (elt
->head
.cleanup
== (Function
*) restore_variable
)
323 restore_variable (&elt
->sv
.v
);
325 (*(elt
->head
.cleanup
)) (elt
->arg
.v
);
330 if (tag
&& found
== 0)
331 internal_warning ("unwind_frame_run: %s: frame not found", tag
);
335 unwind_protect_mem_internal (var
, psize
)
342 size
= *(int *) psize
;
343 allocated
= size
+ offsetof (UNWIND_ELT
, sv
.v
.desired_setting
[0]);
344 if (allocated
< sizeof (UNWIND_ELT
))
345 allocated
= sizeof (UNWIND_ELT
);
346 elt
= (UNWIND_ELT
*)xmalloc (allocated
);
347 elt
->head
.next
= unwind_protect_list
;
348 elt
->head
.cleanup
= (Function
*) restore_variable
;
349 elt
->sv
.v
.variable
= var
;
350 elt
->sv
.v
.size
= size
;
351 FASTCOPY (var
, elt
->sv
.v
.desired_setting
, size
);
352 unwind_protect_list
= elt
;
355 /* Save the value of a variable so it will be restored when unwind-protects
356 are run. VAR is a pointer to the variable. SIZE is the size in
359 unwind_protect_mem (var
, size
)
363 without_interrupts (unwind_protect_mem_internal
, var
, (char *) &size
);
370 print_unwind_protect_tags ()
374 elt
= unwind_protect_list
;
377 if (elt
->head
.cleanup
== 0)
378 fprintf(stderr
, "tag: %s\n", elt
->arg
.v
);
379 elt
= elt
->head
.next
;