Automatic date update in version.in
[binutils-gdb.git] / gas / hash.h
blob5b9e9a0681a10e37b6deb9a0a44c35032ae32b03
1 /* hash.h -- header file for gas hash table routines
2 Copyright (C) 1987-2024 Free Software Foundation, Inc.
4 This file is part of GAS, the GNU Assembler.
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.
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.
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. */
21 #ifndef HASH_H
22 #define HASH_H
24 struct string_tuple
26 const char *key;
27 const void *value;
30 typedef struct string_tuple string_tuple_t;
32 /* Hash function for a string_tuple. */
34 extern hashval_t hash_string_tuple (const void *);
36 /* Equality function for a string_tuple. */
38 extern int eq_string_tuple (const void *, const void *);
40 /* Insert ELEMENT into HTAB. If REPLACE is non-zero existing elements
41 are overwritten. If ELEMENT already exists, a pointer to the slot
42 is returned. Otherwise NULL is returned. */
44 extern void **htab_insert (htab_t, void * /* element */, int /* replace */);
46 /* Print statistics about a hash table. */
48 extern void htab_print_statistics (FILE *f, const char *name, htab_t table);
50 /* Inline string hash table functions. */
52 static inline string_tuple_t *
53 string_tuple_alloc (htab_t table, const char *key, const void *value)
55 string_tuple_t *tuple = table->alloc_f (1, sizeof (*tuple));
56 tuple->key = key;
57 tuple->value = value;
58 return tuple;
61 static inline void *
62 str_hash_find (htab_t table, const char *key)
64 string_tuple_t needle = { key, NULL };
65 string_tuple_t *tuple = htab_find (table, &needle);
66 return tuple != NULL ? (void *) tuple->value : NULL;
69 static inline void *
70 str_hash_find_n (htab_t table, const char *key, size_t n)
72 char *tmp = XNEWVEC (char, n + 1);
73 memcpy (tmp, key, n);
74 tmp[n] = '\0';
75 string_tuple_t needle = { tmp, NULL };
76 string_tuple_t *tuple = htab_find (table, &needle);
77 free (tmp);
78 return tuple != NULL ? (void *) tuple->value : NULL;
81 static inline void
82 str_hash_delete (htab_t table, const char *key)
84 string_tuple_t needle = { key, NULL };
85 htab_remove_elt (table, &needle);
88 static inline void **
89 str_hash_insert (htab_t table, const char *key, const void *value, int replace)
91 string_tuple_t *elt = string_tuple_alloc (table, key, value);
92 void **slot = htab_insert (table, elt, replace);
93 if (slot && !replace && table->free_f)
94 table->free_f (elt);
95 return slot;
98 static inline htab_t
99 str_htab_create (void)
101 return htab_create_alloc (16, hash_string_tuple, eq_string_tuple,
102 NULL, notes_calloc, NULL);
105 #endif /* HASH_H */