2017-12-01 Thomas Koenig <tkoenig@gcc.gnu.org>
[official-gcc.git] / libcc1 / callbacks.cc
blobe5abd4975568f14c81097502e62131ed7148daa7
1 /* Callback management.
2 Copyright (C) 2014-2017 Free Software Foundation, Inc.
4 This file is part of GCC.
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.
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.
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/>. */
20 #include <cc1plugin-config.h>
21 #include <string.h>
22 #include <stdlib.h>
23 #include "callbacks.hh"
24 #include "libiberty.h"
26 // An entry in the hash table.
27 struct method
29 const char *name;
30 cc1_plugin::callback_ftype *func;
33 // Hash function for struct method.
34 static hashval_t
35 hash_method (const void *a)
37 const struct method *m = (const struct method *) a;
39 return htab_hash_string (m->name);
42 // Equality function for struct method.
43 static int
44 eq_method (const void *a, const void *b)
46 const struct method *ma = (const struct method *) a;
47 const struct method *mb = (const struct method *) b;
49 return strcmp (ma->name, mb->name) == 0;
52 cc1_plugin::callbacks::callbacks ()
53 : m_registry (htab_create_alloc (10, hash_method, eq_method,
54 free, xcalloc, free))
58 cc1_plugin::callbacks::~callbacks ()
60 htab_delete (m_registry);
63 void
64 cc1_plugin::callbacks::add_callback (const char *name,
65 cc1_plugin::callback_ftype *func)
67 method m;
68 method **slot;
70 m.name = name;
71 m.func = func;
73 slot = (method **) htab_find_slot (m_registry, &m, INSERT);
74 *slot = XNEW (method);
75 **slot = m;
78 cc1_plugin::callback_ftype *
79 cc1_plugin::callbacks::find_callback (const char *name)
81 method m, *found;
83 m.name = name;
85 found = (method *) htab_find (m_registry, &m);
86 if (found == NULL)
87 return NULL;
89 return found->func;