2 * Copyright (C) 2010 Joel Rosdahl
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the Free
6 * Software Foundation; either version 3 of the License, or (at your option)
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * You should have received a copy of the GNU General Public License along with
15 * this program; if not, write to the Free Software Foundation, Inc., 51
16 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 struct exit_function {
22 void (*function)(void *);
24 struct exit_function *next;
27 struct nullary_exit_function {
28 void (*function)(void);
31 static struct exit_function *exit_functions;
34 call_nullary_exit_function(void *context)
36 struct nullary_exit_function *p = (struct nullary_exit_function*)context;
42 * Initialize exit functions. Must be called once before exitfn_add* are used.
47 if (atexit(exitfn_call) != 0) {
48 fatal("atexit failed: %s", strerror(errno));
53 * Add a nullary function to be called context when ccache exits. Functions are
54 * called in reverse order.
57 exitfn_add_nullary(void (*function)(void))
59 struct nullary_exit_function *p = x_malloc(sizeof(*p));
60 p->function = function;
61 exitfn_add(call_nullary_exit_function, p);
65 * Add a function to be called with a context parameter when ccache exits.
66 * Functions are called in reverse order.
69 exitfn_add(void (*function)(void *), void *context)
71 struct exit_function *p;
73 p = x_malloc(sizeof(*p));
74 p->function = function;
76 p->next = exit_functions;
81 * Call added functions.
86 struct exit_function *p = exit_functions, *q;
88 p->function(p->context);
93 exit_functions = NULL;