]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gas/hash.h
Remove --reduce-memory-overheads and --hash-size arguments.
[thirdparty/binutils-gdb.git] / gas / hash.h
1 /* hash.h -- header file for gas hash table routines
2 Copyright (C) 1987-2020 Free Software Foundation, Inc.
3
4 This file is part of GAS, the GNU Assembler.
5
6 GAS is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3, or (at your option)
9 any later version.
10
11 GAS is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GAS; see the file COPYING. If not, write to the Free
18 Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
19 02110-1301, USA. */
20
21 #ifndef HASH_H
22 #define HASH_H
23
24 /* Insert ELEMENT into HTAB. If the element exists, it is overwritten. */
25
26 extern void htab_insert (htab_t, void *);
27
28 /* Print statistics about a hash table. */
29
30 extern void htab_print_statistics (FILE *f, const char *name, htab_t table);
31
32 /* String hash table functions. */
33
34 struct string_tuple
35 {
36 const char *key;
37 char *value;
38 };
39
40 typedef struct string_tuple string_tuple_t;
41
42 /* Hash function for a string_tuple. */
43
44 static hashval_t
45 hash_string_tuple (const void *e)
46 {
47 string_tuple_t *tuple = (string_tuple_t *) e;
48 return htab_hash_string (tuple->key);
49 }
50
51 /* Equality function for a string_tuple. */
52
53 static int
54 eq_string_tuple (const void *a, const void *b)
55 {
56 const string_tuple_t *ea = (const string_tuple_t *) a;
57 const string_tuple_t *eb = (const string_tuple_t *) b;
58
59 return strcmp (ea->key, eb->key) == 0;
60 }
61
62 static inline string_tuple_t *
63 string_tuple_alloc (const char *key, char *value)
64 {
65 string_tuple_t *tuple = XNEW (string_tuple_t);
66 tuple->key = key;
67 tuple->value = value;
68 return tuple;
69 }
70
71 static inline void *
72 str_hash_find (htab_t table, const char *key)
73 {
74 string_tuple_t needle = { key, NULL };
75 string_tuple_t *tuple = htab_find (table, &needle);
76 return tuple != NULL ? tuple->value : NULL;
77 }
78
79 static inline void *
80 str_hash_find_n (htab_t table, const char *key, size_t n)
81 {
82 char *tmp = XNEWVEC (char, n + 1);
83 memcpy (tmp, key, n);
84 tmp[n] = '\0';
85 string_tuple_t needle = { tmp, NULL };
86 string_tuple_t *tuple = htab_find (table, &needle);
87 free (tmp);
88 return tuple != NULL ? tuple->value : NULL;
89 }
90
91 static inline void
92 str_hash_delete (htab_t table, const char *key)
93 {
94 string_tuple_t needle = { key, NULL };
95 htab_remove_elt (table, &needle);
96 }
97
98 static inline void
99 str_hash_insert (htab_t table, const char *key, void *value)
100 {
101 htab_insert (table, string_tuple_alloc (key, value));
102 }
103
104 static inline htab_t
105 str_htab_create (void)
106 {
107 return htab_create_alloc (16, hash_string_tuple, eq_string_tuple,
108 NULL, xcalloc, free);
109 }
110
111 #endif /* HASH_H */