PIPS
reset_hooks.c
Go to the documentation of this file.
1 /*
2 
3  $Id: reset_hooks.c 23065 2016-03-02 09:05:50Z coelho $
4 
5  Copyright 1989-2016 MINES ParisTech
6 
7  This file is part of PIPS.
8 
9  PIPS is free software: you can redistribute it and/or modify it
10  under the terms of the GNU General Public License as published by
11  the Free Software Foundation, either version 3 of the License, or
12  any later version.
13 
14  PIPS is distributed in the hope that it will be useful, but WITHOUT ANY
15  WARRANTY; without even the implied warranty of MERCHANTABILITY or
16  FITNESS FOR A PARTICULAR PURPOSE.
17 
18  See the GNU General Public License for more details.
19 
20  You should have received a copy of the GNU General Public License
21  along with PIPS. If not, see <http://www.gnu.org/licenses/>.
22 
23 */
24 #ifdef HAVE_CONFIG_H
25  #include "pips_config.h"
26 #endif
27 
28 #include <stdio.h>
29 #include <genC.h>
30 #include "misc.h"
31 
32 /* reset functions to call on exceptions
33  * to be used by pipsmake?
34  *
35  * should it be a stack?
36  * should there by a stack of stacks for different TRY/CATCH levels?
37  * should it be processed up to a marker?
38  * would an additionnal void* for arguments make sense?
39  */
40 static list reset_hooks = NIL;
41 
42 /* add function to be called for cleanup if an exception is raised.
43  */
45 {
46  pips_assert("reset function not already in list",
47  !gen_in_list_p(function, reset_hooks));
48  reset_hooks = CONS(VOID_STAR, function, reset_hooks);
49 }
50 
51 /* this function is expected to be called when catching an exception.
52  */
53 void reset_hooks_call(void)
54 {
55  // call reset functions
57  (*((reset_func_t) f))();
58 
59  // cleanup
61  reset_hooks = NIL;
62 }
63 
64 /* check that the stack was cleaned.
65  */
67 {
68  pips_assert("no reset functions", reset_hooks == NIL);
69 }
70 
71 /* remove registered cleanup hook.
72  */
74 {
75  // should it really be the first? probably too optimistic.
76  /*
77  pips_assert(reset_hooks &&
78  VOID_STAR(CAR(reset_hooks)) == function,
79  "unregister reset functions in reverse order");
80  */
81  pips_assert("unregister a function already registered",
82  gen_in_list_p(function, reset_hooks));
83  gen_remove_once(&reset_hooks, function);
84 }
85 
86 /* That's all */
#define VOID_STAR(x)
Definition: genC.h:95
void gen_remove_once(list *pl, const void *o)
Remove the first occurence of o in list pl:
Definition: list.c:691
#define NIL
The empty list (nil in Lisp)
Definition: newgen_list.h:47
#define CONS(_t_, _i_, _l_)
List element cell constructor (insert an element at the beginning of a list)
Definition: newgen_list.h:150
void gen_free_list(list l)
free the spine of the list
Definition: list.c:327
bool gen_in_list_p(const void *vo, const list lx)
tell whether vo belongs to lx
Definition: list.c:734
#define FOREACH(_fe_CASTER, _fe_item, _fe_list)
Apply/map an instruction block on all the elements of a list.
Definition: newgen_list.h:179
void(* reset_func_t)(void)
Definition: misc-local.h:222
#define pips_assert(what, predicate)
common macros, two flavors depending on NDEBUG
Definition: misc-local.h:172
int f(int off1, int off2, int n, float r[n], float a[n], float b[n])
Definition: offsets.c:15
static list reset_hooks
reset functions to call on exceptions to be used by pipsmake?
Definition: reset_hooks.c:40
void reset_hooks_is_empty(void)
check that the stack was cleaned.
Definition: reset_hooks.c:66
void reset_hooks_call(void)
this function is expected to be called when catching an exception.
Definition: reset_hooks.c:53
void reset_hooks_register(reset_func_t function)
add function to be called for cleanup if an exception is raised.
Definition: reset_hooks.c:44
void reset_hooks_unregister(reset_func_t function)
remove registered cleanup hook.
Definition: reset_hooks.c:73
The structure used to build lists in NewGen.
Definition: newgen_list.h:41