OSDN Git Service

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