1 | /* Callback management. |
2 | Copyright (C) 2014-2024 Free Software Foundation, Inc. |
3 | |
4 | This file is part of GCC. |
5 | |
6 | GCC is free software; you can redistribute it and/or modify it under |
7 | the terms of the GNU General Public License as published by the Free |
8 | Software Foundation; either version 3, or (at your option) any later |
9 | version. |
10 | |
11 | GCC is distributed in the hope that it will be useful, but WITHOUT ANY |
12 | WARRANTY; without even the implied warranty of MERCHANTABILITY or |
13 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
14 | for more details. |
15 | |
16 | You should have received a copy of the GNU General Public License |
17 | along with GCC; see the file COPYING3. If not see |
18 | <http://www.gnu.org/licenses/>. */ |
19 | |
20 | #include <cc1plugin-config.h> |
21 | #include <string.h> |
22 | #include <stdlib.h> |
23 | #include "callbacks.hh" |
24 | #include "libiberty.h" |
25 | |
26 | // An entry in the hash table. |
27 | struct method |
28 | { |
29 | const char *name; |
30 | cc1_plugin::callback_ftype *func; |
31 | }; |
32 | |
33 | // Hash function for struct method. |
34 | static hashval_t |
35 | hash_method (const void *a) |
36 | { |
37 | const struct method *m = (const struct method *) a; |
38 | |
39 | return htab_hash_string (m->name); |
40 | } |
41 | |
42 | // Equality function for struct method. |
43 | static int |
44 | eq_method (const void *a, const void *b) |
45 | { |
46 | const struct method *ma = (const struct method *) a; |
47 | const struct method *mb = (const struct method *) b; |
48 | |
49 | return strcmp (s1: ma->name, s2: mb->name) == 0; |
50 | } |
51 | |
52 | cc1_plugin::callbacks::callbacks () |
53 | : m_registry (htab_create_alloc (10, hash_method, eq_method, |
54 | free, xcalloc, free)) |
55 | { |
56 | } |
57 | |
58 | cc1_plugin::callbacks::~callbacks () |
59 | { |
60 | htab_delete (m_registry); |
61 | } |
62 | |
63 | void |
64 | cc1_plugin::callbacks::add_callback (const char *name, |
65 | cc1_plugin::callback_ftype *func) |
66 | { |
67 | method m; |
68 | method **slot; |
69 | |
70 | m.name = name; |
71 | m.func = func; |
72 | |
73 | slot = (method **) htab_find_slot (m_registry, &m, INSERT); |
74 | *slot = XNEW (method); |
75 | **slot = m; |
76 | } |
77 | |
78 | cc1_plugin::callback_ftype * |
79 | cc1_plugin::callbacks::find_callback (const char *name) |
80 | { |
81 | method m, *found; |
82 | |
83 | m.name = name; |
84 | |
85 | found = (method *) htab_find (m_registry, &m); |
86 | if (found == NULL) |
87 | return NULL; |
88 | |
89 | return found->func; |
90 | } |
91 | |