symtab.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /* Hash tables.
  2. Copyright (C) 2000-2015 Free Software Foundation, Inc.
  3. This program is free software; you can redistribute it and/or modify it
  4. under the terms of the GNU General Public License as published by the
  5. Free Software Foundation; either version 3, or (at your option) any
  6. later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; see the file COPYING3. If not see
  13. <http://www.gnu.org/licenses/>. */
  14. #ifndef LIBCPP_SYMTAB_H
  15. #define LIBCPP_SYMTAB_H
  16. #include "obstack.h"
  17. #ifndef GTY
  18. #define GTY(x) /* nothing */
  19. #endif
  20. /* This is what each hash table entry points to. It may be embedded
  21. deeply within another object. */
  22. typedef struct ht_identifier ht_identifier;
  23. typedef struct ht_identifier *ht_identifier_ptr;
  24. struct GTY(()) ht_identifier {
  25. const unsigned char *str;
  26. unsigned int len;
  27. unsigned int hash_value;
  28. };
  29. #define HT_LEN(NODE) ((NODE)->len)
  30. #define HT_STR(NODE) ((NODE)->str)
  31. typedef struct ht cpp_hash_table;
  32. typedef struct ht_identifier *hashnode;
  33. enum ht_lookup_option {HT_NO_INSERT = 0, HT_ALLOC};
  34. /* An identifier hash table for cpplib and the front ends. */
  35. struct ht
  36. {
  37. /* Identifiers are allocated from here. */
  38. struct obstack stack;
  39. hashnode *entries;
  40. /* Call back, allocate a node. */
  41. hashnode (*alloc_node) (cpp_hash_table *);
  42. /* Call back, allocate something that hangs off a node like a cpp_macro.
  43. NULL means use the usual allocator. */
  44. void * (*alloc_subobject) (size_t);
  45. unsigned int nslots; /* Total slots in the entries array. */
  46. unsigned int nelements; /* Number of live elements. */
  47. /* Link to reader, if any. For the benefit of cpplib. */
  48. struct cpp_reader *pfile;
  49. /* Table usage statistics. */
  50. unsigned int searches;
  51. unsigned int collisions;
  52. /* Should 'entries' be freed when it is no longer needed? */
  53. bool entries_owned;
  54. };
  55. /* Initialize the hashtable with 2 ^ order entries. */
  56. extern cpp_hash_table *ht_create (unsigned int order);
  57. /* Frees all memory associated with a hash table. */
  58. extern void ht_destroy (cpp_hash_table *);
  59. extern hashnode ht_lookup (cpp_hash_table *, const unsigned char *,
  60. size_t, enum ht_lookup_option);
  61. extern hashnode ht_lookup_with_hash (cpp_hash_table *, const unsigned char *,
  62. size_t, unsigned int,
  63. enum ht_lookup_option);
  64. #define HT_HASHSTEP(r, c) ((r) * 67 + ((c) - 113));
  65. #define HT_HASHFINISH(r, len) ((r) + (len))
  66. /* For all nodes in TABLE, make a callback. The callback takes
  67. TABLE->PFILE, the node, and a PTR, and the callback sequence stops
  68. if the callback returns zero. */
  69. typedef int (*ht_cb) (struct cpp_reader *, hashnode, const void *);
  70. extern void ht_forall (cpp_hash_table *, ht_cb, const void *);
  71. /* For all nodes in TABLE, call the callback. If the callback returns
  72. a nonzero value, the node is removed from the table. */
  73. extern void ht_purge (cpp_hash_table *, ht_cb, const void *);
  74. /* Restore the hash table. */
  75. extern void ht_load (cpp_hash_table *ht, hashnode *entries,
  76. unsigned int nslots, unsigned int nelements, bool own);
  77. /* Dump allocation statistics to stderr. */
  78. extern void ht_dump_statistics (cpp_hash_table *);
  79. #endif /* LIBCPP_SYMTAB_H */