1/* Callback management.
2   Copyright (C) 2014 Free Software Foundation, Inc.
3
4This file is part of GCC.
5
6GCC is free software; you can redistribute it and/or modify it under
7the terms of the GNU General Public License as published by the Free
8Software Foundation; either version 3, or (at your option) any later
9version.
10
11GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12WARRANTY; without even the implied warranty of MERCHANTABILITY or
13FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14for more details.
15
16You should have received a copy of the GNU General Public License
17along 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.
27struct method
28{
29  const char *name;
30  cc1_plugin::callback_ftype *func;
31};
32
33// Hash function for struct method.
34static hashval_t
35hash_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.
43static int
44eq_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 (ma->name, mb->name) == 0;
50}
51
52cc1_plugin::callbacks::callbacks ()
53  : m_registry (htab_create_alloc (10, hash_method, eq_method,
54				   free, xcalloc, free))
55{
56}
57
58cc1_plugin::callbacks::~callbacks ()
59{
60  htab_delete (m_registry);
61}
62
63void
64cc1_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
78cc1_plugin::callback_ftype *
79cc1_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