Commit | Line | Data |
---|---|---|
54d22525 | 1 | /* hash.h -- header file for gas hash table routines |
b3adc24a | 2 | Copyright (C) 1987-2020 Free Software Foundation, Inc. |
252b5132 RH |
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 | |
ec2655a6 | 8 | the Free Software Foundation; either version 3, or (at your option) |
252b5132 RH |
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 | |
54d22525 | 17 | along with GAS; see the file COPYING. If not, write to the Free |
4b4da160 NC |
18 | Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA |
19 | 02110-1301, USA. */ | |
252b5132 | 20 | |
54d22525 ILT |
21 | #ifndef HASH_H |
22 | #define HASH_H | |
252b5132 | 23 | |
fe0e921f AM |
24 | /* Insert ELEMENT into HTAB. If REPLACE is non-zero existing elements |
25 | are overwritten. If ELEMENT already exists, a pointer to the slot | |
26 | is returned. Otherwise NULL is returned. */ | |
abebb03c | 27 | |
fe0e921f | 28 | extern void **htab_insert (htab_t, void * /* element */, int /* replace */); |
abebb03c ML |
29 | |
30 | /* Print statistics about a hash table. */ | |
31 | ||
32 | extern void htab_print_statistics (FILE *f, const char *name, htab_t table); | |
33 | ||
8d32ded0 ML |
34 | /* String hash table functions. */ |
35 | ||
36 | struct string_tuple | |
37 | { | |
38 | const char *key; | |
fe0e921f | 39 | const void *value; |
8d32ded0 ML |
40 | }; |
41 | ||
42 | typedef struct string_tuple string_tuple_t; | |
43 | ||
44 | /* Hash function for a string_tuple. */ | |
45 | ||
46 | static hashval_t | |
47 | hash_string_tuple (const void *e) | |
48 | { | |
49 | string_tuple_t *tuple = (string_tuple_t *) e; | |
50 | return htab_hash_string (tuple->key); | |
51 | } | |
52 | ||
53 | /* Equality function for a string_tuple. */ | |
54 | ||
55 | static int | |
56 | eq_string_tuple (const void *a, const void *b) | |
57 | { | |
58 | const string_tuple_t *ea = (const string_tuple_t *) a; | |
59 | const string_tuple_t *eb = (const string_tuple_t *) b; | |
60 | ||
61 | return strcmp (ea->key, eb->key) == 0; | |
62 | } | |
63 | ||
64 | static inline string_tuple_t * | |
fe0e921f | 65 | string_tuple_alloc (const char *key, const void *value) |
8d32ded0 ML |
66 | { |
67 | string_tuple_t *tuple = XNEW (string_tuple_t); | |
68 | tuple->key = key; | |
69 | tuple->value = value; | |
70 | return tuple; | |
71 | } | |
72 | ||
73 | static inline void * | |
74 | str_hash_find (htab_t table, const char *key) | |
75 | { | |
76 | string_tuple_t needle = { key, NULL }; | |
77 | string_tuple_t *tuple = htab_find (table, &needle); | |
fe0e921f | 78 | return tuple != NULL ? (void *) tuple->value : NULL; |
8d32ded0 ML |
79 | } |
80 | ||
81 | static inline void * | |
82 | str_hash_find_n (htab_t table, const char *key, size_t n) | |
83 | { | |
84 | char *tmp = XNEWVEC (char, n + 1); | |
85 | memcpy (tmp, key, n); | |
86 | tmp[n] = '\0'; | |
87 | string_tuple_t needle = { tmp, NULL }; | |
88 | string_tuple_t *tuple = htab_find (table, &needle); | |
89 | free (tmp); | |
fe0e921f | 90 | return tuple != NULL ? (void *) tuple->value : NULL; |
8d32ded0 ML |
91 | } |
92 | ||
93 | static inline void | |
94 | str_hash_delete (htab_t table, const char *key) | |
95 | { | |
96 | string_tuple_t needle = { key, NULL }; | |
97 | htab_remove_elt (table, &needle); | |
98 | } | |
99 | ||
fe0e921f AM |
100 | static inline void ** |
101 | str_hash_insert (htab_t table, const char *key, const void *value, int replace) | |
8d32ded0 | 102 | { |
fe0e921f AM |
103 | string_tuple_t *elt = string_tuple_alloc (key, value); |
104 | void **slot = htab_insert (table, elt, replace); | |
105 | if (slot && !replace) | |
106 | free (elt); | |
107 | return slot; | |
8d32ded0 ML |
108 | } |
109 | ||
110 | static inline htab_t | |
111 | str_htab_create (void) | |
112 | { | |
113 | return htab_create_alloc (16, hash_string_tuple, eq_string_tuple, | |
114 | NULL, xcalloc, free); | |
115 | } | |
116 | ||
54d22525 | 117 | #endif /* HASH_H */ |