145650Sghelmer/* Hash tables.
245650Sghelmer   Copyright (C) 2000-2020 Free Software Foundation, Inc.
345650Sghelmer
445650SghelmerThis program is free software; you can redistribute it and/or modify it
545650Sghelmerunder the terms of the GNU General Public License as published by the
645650SghelmerFree Software Foundation; either version 3, or (at your option) any
745650Sghelmerlater version.
845650Sghelmer
945650SghelmerThis program is distributed in the hope that it will be useful,
1045650Sghelmerbut WITHOUT ANY WARRANTY; without even the implied warranty of
1145650SghelmerMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1245650SghelmerGNU General Public License for more details.
1345650Sghelmer
1445650SghelmerYou should have received a copy of the GNU General Public License
1545650Sghelmeralong with this program; see the file COPYING3.  If not see
1645650Sghelmer<http://www.gnu.org/licenses/>.  */
1745650Sghelmer
1845650Sghelmer#ifndef LIBCPP_SYMTAB_H
1945650Sghelmer#define LIBCPP_SYMTAB_H
2045650Sghelmer
2145650Sghelmer#include "obstack.h"
2245650Sghelmer
2345650Sghelmer#ifndef GTY
2445650Sghelmer#define GTY(x) /* nothing */
2545650Sghelmer#endif
2650476Speter
2745650Sghelmer/* This is what each hash table entry points to.  It may be embedded
28260906Sbapt   deeply within another object.  */
2945650Sghelmertypedef struct ht_identifier ht_identifier;
3079538Srutypedef struct ht_identifier *ht_identifier_ptr;
3145650Sghelmerstruct GTY(()) ht_identifier {
3245650Sghelmer  const unsigned char *str;
3345650Sghelmer  unsigned int len;
3445650Sghelmer  unsigned int hash_value;
3568716Sru};
3668716Sru
3779727Sschweikh#define HT_LEN(NODE) ((NODE)->len)
3845650Sghelmer#define HT_STR(NODE) ((NODE)->str)
3957676Ssheldonh
4057676Ssheldonhtypedef struct ht cpp_hash_table;
4145650Sghelmertypedef struct ht_identifier *hashnode;
42117011Sru
43117011Sruenum ht_lookup_option {HT_NO_INSERT = 0, HT_ALLOC};
4445650Sghelmer
4545650Sghelmer/* An identifier hash table for cpplib and the front ends.  */
4645650Sghelmerstruct ht
4745650Sghelmer{
4845650Sghelmer  /* Identifiers are allocated from here.  */
49117011Sru  struct obstack stack;
50117011Sru
5145650Sghelmer  hashnode *entries;
5245650Sghelmer  /* Call back, allocate a node.  */
5345650Sghelmer  hashnode (*alloc_node) (cpp_hash_table *);
5445650Sghelmer  /* Call back, allocate something that hangs off a node like a cpp_macro.
5568716Sru     NULL means use the usual allocator.  */
5645650Sghelmer  void * (*alloc_subobject) (size_t);
57107788Sru
5845650Sghelmer  unsigned int nslots;		/* Total slots in the entries array.  */
5945650Sghelmer  unsigned int nelements;	/* Number of live elements.  */
6045650Sghelmer
6145650Sghelmer  /* Link to reader, if any.  For the benefit of cpplib.  */
6245650Sghelmer  struct cpp_reader *pfile;
6345650Sghelmer
6445650Sghelmer  /* Table usage statistics.  */
6545650Sghelmer  unsigned int searches;
6674321Sdd  unsigned int collisions;
6774321Sdd
6845650Sghelmer  /* Should 'entries' be freed when it is no longer needed?  */
6945650Sghelmer  bool entries_owned;
7045650Sghelmer};
7145650Sghelmer
7245650Sghelmer/* Initialize the hashtable with 2 ^ order entries.  */
7345650Sghelmerextern cpp_hash_table *ht_create (unsigned int order);
7445650Sghelmer
7545650Sghelmer/* Frees all memory associated with a hash table.  */
7645650Sghelmerextern void ht_destroy (cpp_hash_table *);
7745650Sghelmer
7879727Sschweikhextern hashnode ht_lookup (cpp_hash_table *, const unsigned char *,
7945650Sghelmer			   size_t, enum ht_lookup_option);
8045650Sghelmerextern hashnode ht_lookup_with_hash (cpp_hash_table *, const unsigned char *,
8145650Sghelmer                                     size_t, unsigned int,
8245650Sghelmer                                     enum ht_lookup_option);
8379727Sschweikh#define HT_HASHSTEP(r, c) ((r) * 67 + ((c) - 113));
8445650Sghelmer#define HT_HASHFINISH(r, len) ((r) + (len))
8545650Sghelmer
8645650Sghelmer/* For all nodes in TABLE, make a callback.  The callback takes
8745650Sghelmer   TABLE->PFILE, the node, and a PTR, and the callback sequence stops
8845650Sghelmer   if the callback returns zero.  */
8945650Sghelmertypedef int (*ht_cb) (struct cpp_reader *, hashnode, const void *);
9045650Sghelmerextern void ht_forall (cpp_hash_table *, ht_cb, const void *);
91121573Skensmith
92121573Skensmith/* For all nodes in TABLE, call the callback.  If the callback returns
93121573Skensmith   a nonzero value, the node is removed from the table.  */
94121573Skensmithextern void ht_purge (cpp_hash_table *, ht_cb, const void *);
9545650Sghelmer
9645650Sghelmer/* Restore the hash table.  */
9745650Sghelmerextern void ht_load (cpp_hash_table *ht, hashnode *entries,
9845650Sghelmer		     unsigned int nslots, unsigned int nelements, bool own);
9945650Sghelmer
10068962Sru/* Dump allocation statistics to stderr.  */
101112608Skeramidaextern void ht_dump_statistics (cpp_hash_table *);
102112608Skeramida
103112608Skeramida#endif /* LIBCPP_SYMTAB_H */
104112608Skeramida