callbacks.cc 2.0 KB

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