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