8b95b77cc6954fd6cc9c490fe481ab6168a3ed59
[libfirm] / ir / ir / irhooks.c
1 /*
2  * Copyright (C) 1995-2007 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
6  * This file may be distributed and/or modified under the terms of the
7  * GNU General Public License version 2 as published by the Free Software
8  * Foundation and appearing in the file LICENSE.GPL included in the
9  * packaging of this file.
10  *
11  * Licensees holding valid libFirm Professional Edition licenses may use
12  * this file in accordance with the libFirm Commercial License.
13  * Agreement provided with the Software.
14  *
15  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE.
18  */
19
20 /*
21  * Project:     libFIRM
22  * File name:   ir/ir/irhooks.c
23  * Purpose:     Generic hooks for various libFirm functions.
24  * Author:      Michael Beck
25  * Created:
26  * CVS-ID:      $Id$
27  * Copyright:   (C) 1998-2005 Universität Karlsruhe
28  */
29
30 /**
31  * @file irhooks.c
32  *
33  * Generic hooks for various libFirm functions.
34  *
35  * @author Michael Beck
36  */
37 #ifdef HAVE_CONFIG_H
38 #include "config.h"
39 #endif
40
41 #include "irhooks.h"
42
43 #ifdef FIRM_ENABLE_HOOKS
44
45 /* the hooks */
46 hook_entry_t *hooks[hook_last];
47
48 /* register a hook */
49 void register_hook(hook_type_t hook, hook_entry_t *entry) {
50   /* check if a hook function is specified. It's a union, so no matter which one */
51   if (! entry->hook._hook_turn_into_id)
52     return;
53
54   entry->next = hooks[hook];
55   hooks[hook] = entry;
56 }
57
58 /* unregister a hook */
59 void unregister_hook(hook_type_t hook, hook_entry_t *entry) {
60   hook_entry_t *p;
61
62   if (hooks[hook] == entry) {
63     hooks[hook] = entry->next;
64     entry->next = NULL;
65     return;
66   }
67
68   for (p = hooks[hook]; p && p->next != entry; p = p->next);
69
70   if (p) {
71     p->next     = entry->next;
72     entry->next = NULL;
73   }
74 }
75
76
77 #else
78
79 void register_hook(hook_type_t hook, hook_entry_t *entry) {}
80 void unregister_hook(hook_type_t hook, hook_entry_t *entry) {}
81
82 #endif /* FIRM_ENABLE_HOOKS */
83
84 int firm_init_hooks(void) {
85   /* this strange code assures that both functions are available
86      in a shared library even if none of them is called.
87      Meanwhile not needed anymore but ... */
88   return (int)register_hook + (int)unregister_hook;
89 }