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