2 Copyright (C) 2000, 2001, 2003, 2004 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify it
5 under the terms of the GNU General Public License as published by the
6 Free Software Foundation; either version 2, or (at your option) any
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
18 #ifndef LIBCPP_SYMTAB_H
19 #define LIBCPP_SYMTAB_H
22 #define GTY(x) /* nothing */
24 /* This is what each hash table entry points to. It may be embedded
25 deeply within another object. */
26 typedef struct ht_identifier ht_identifier;
27 struct ht_identifier GTY(())
29 const unsigned char *str;
31 unsigned int hash_value;
34 #define HT_LEN(NODE) ((NODE)->len)
35 #define HT_STR(NODE) ((NODE)->str)
37 typedef struct ht hash_table;
38 typedef struct ht_identifier *hashnode;
40 enum ht_lookup_option {HT_NO_INSERT = 0, HT_ALLOC, HT_ALLOCED};
42 /* An identifier hash table for cpplib and the front ends. */
45 /* Identifiers are allocated from here. */
50 hashnode (*alloc_node) (hash_table *);
52 unsigned int nslots; /* Total slots in the entries array. */
53 unsigned int nelements; /* Number of live elements. */
55 /* Link to reader, if any. For the benefit of cpplib. */
56 struct cpp_reader *pfile;
58 /* Table usage statistics. */
59 unsigned int searches;
60 unsigned int collisions;
62 /* Should 'entries' be freed when it is no longer needed? */
66 /* Initialize the hashtable with 2 ^ order entries. */
67 extern hash_table *ht_create (unsigned int order);
69 /* Frees all memory associated with a hash table. */
70 extern void ht_destroy (hash_table *);
72 extern hashnode ht_lookup (hash_table *, const unsigned char *,
73 size_t, enum ht_lookup_option);
74 extern hashnode ht_lookup_with_hash (hash_table *, const unsigned char *,
76 enum ht_lookup_option);
77 #define HT_HASHSTEP(r, c) ((r) * 67 + ((c) - 113));
78 #define HT_HASHFINISH(r, len) ((r) + (len))
80 /* For all nodes in TABLE, make a callback. The callback takes
81 TABLE->PFILE, the node, and a PTR, and the callback sequence stops
82 if the callback returns zero. */
83 typedef int (*ht_cb) (struct cpp_reader *, hashnode, const void *);
84 extern void ht_forall (hash_table *, ht_cb, const void *);
86 /* Restore the hash table. */
87 extern void ht_load (hash_table *ht, hashnode *entries,
88 unsigned int nslots, unsigned int nelements, bool own);
90 /* Dump allocation statistics to stderr. */
91 extern void ht_dump_statistics (hash_table *);
93 #endif /* LIBCPP_SYMTAB_H */