2 * Copyright (c) 2008 , Nicolas François
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. The name of the copyright holders or contributors may not be used to
14 * endorse or promote products derived from this software without
15 * specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
20 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 #include "prototypes.h"
38 * The cleanup_functions stack.
40 #define CLEANUP_FUNCTIONS 10
41 static cleanup_function cleanup_functions[CLEANUP_FUNCTIONS];
42 static void * cleanup_function_args[CLEANUP_FUNCTIONS];
45 * - Cleanup functions shall not fail.
46 * - You should register do_cleanups with atexit.
47 * - You should add cleanup functions to the stack with add_cleanup when
48 * an operation is expected to be executed later, and remove it from the
49 * stack with del_cleanup when it has been executed.
54 * do_cleanups - perform the actions stored in the cleanup_functions stack.
56 * It is intended to be used as:
57 * atexit (do_cleanups);
59 void do_cleanups (void)
63 /* Make sure there were no overflow */
64 assert (NULL == cleanup_functions[CLEANUP_FUNCTIONS-1]);
66 i = CLEANUP_FUNCTIONS;
69 if (cleanup_functions[i] != NULL) {
70 cleanup_functions[i] (cleanup_function_args[i]);
76 * add_cleanup - Add a cleanup_function to the cleanup_functions stack.
78 void add_cleanup (cleanup_function pcf, /*@null@*/void *arg)
83 assert (NULL == cleanup_functions[CLEANUP_FUNCTIONS-2]);
85 /* Add the cleanup_function at the end of the stack */
86 for (i=0; NULL != cleanup_functions[i]; i++);
87 cleanup_functions[i] = pcf;
88 cleanup_function_args[i] = arg;
92 * del_cleanup - Remove a cleanup_function from the cleanup_functions stack.
94 void del_cleanup (cleanup_function pcf)
99 /* Find the pcf cleanup function */
100 for (i=0; i<CLEANUP_FUNCTIONS; i++) {
101 if (cleanup_functions[i] == pcf) {
106 /* Make sure the cleanup function was found */
107 assert (i<CLEANUP_FUNCTIONS);
109 /* Move the rest of the cleanup functions */
110 for (; i<CLEANUP_FUNCTIONS; i++) {
111 /* Make sure the cleanup function was specified only once */
112 assert (cleanup_functions[i+1] != pcf);
114 if (i == (CLEANUP_FUNCTIONS -1)) {
115 cleanup_functions[i] = NULL;
116 cleanup_function_args[i] = NULL;
118 cleanup_functions[i] = cleanup_functions[i+1];
119 cleanup_function_args[i] = cleanup_function_args[i+1];
122 /* A NULL indicates the end of the stack */
123 if (NULL == cleanup_functions[i]) {