]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gas/hash.h
i386 gas: don't leak op_hash or reg_hash memory
[thirdparty/binutils-gdb.git] / gas / hash.h
CommitLineData
54d22525 1/* hash.h -- header file for gas hash table routines
a2c58332 2 Copyright (C) 1987-2022 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
8d32ded0
ML
24struct string_tuple
25{
26 const char *key;
fe0e921f 27 const void *value;
8d32ded0
ML
28};
29
30typedef struct string_tuple string_tuple_t;
31
32/* Hash function for a string_tuple. */
33
1309c316 34extern hashval_t hash_string_tuple (const void *);
8d32ded0
ML
35
36/* Equality function for a string_tuple. */
37
1309c316 38extern int eq_string_tuple (const void *, const void *);
8d32ded0 39
1309c316
AM
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. */
43
44extern void **htab_insert (htab_t, void * /* element */, int /* replace */);
45
46/* Print statistics about a hash table. */
47
48extern void htab_print_statistics (FILE *f, const char *name, htab_t table);
49
50/* Inline string hash table functions. */
8d32ded0
ML
51
52static inline string_tuple_t *
1309c316 53string_tuple_alloc (htab_t table, const char *key, const void *value)
8d32ded0 54{
1309c316 55 string_tuple_t *tuple = table->alloc_f (1, sizeof (*tuple));
8d32ded0
ML
56 tuple->key = key;
57 tuple->value = value;
58 return tuple;
59}
60
61static inline void *
62str_hash_find (htab_t table, const char *key)
63{
64 string_tuple_t needle = { key, NULL };
65 string_tuple_t *tuple = htab_find (table, &needle);
fe0e921f 66 return tuple != NULL ? (void *) tuple->value : NULL;
8d32ded0
ML
67}
68
69static inline void *
70str_hash_find_n (htab_t table, const char *key, size_t n)
71{
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);
fe0e921f 78 return tuple != NULL ? (void *) tuple->value : NULL;
8d32ded0
ML
79}
80
81static inline void
82str_hash_delete (htab_t table, const char *key)
83{
84 string_tuple_t needle = { key, NULL };
85 htab_remove_elt (table, &needle);
86}
87
fe0e921f
AM
88static inline void **
89str_hash_insert (htab_t table, const char *key, const void *value, int replace)
8d32ded0 90{
1309c316 91 string_tuple_t *elt = string_tuple_alloc (table, key, value);
fe0e921f 92 void **slot = htab_insert (table, elt, replace);
1309c316
AM
93 if (slot && !replace && table->free_f)
94 table->free_f (elt);
fe0e921f 95 return slot;
8d32ded0
ML
96}
97
98static inline htab_t
99str_htab_create (void)
100{
101 return htab_create_alloc (16, hash_string_tuple, eq_string_tuple,
654d6f31 102 free, xcalloc, free);
8d32ded0
ML
103}
104
54d22525 105#endif /* HASH_H */