1 /* hash.h -- header file for gas hash table routines
2 Copyright (C) 1987-2020 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)
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
24 /* Insert ELEMENT into HTAB. If the element exists, it is overwritten. */
26 extern void htab_insert (htab_t
, void *);
28 /* Print statistics about a hash table. */
30 extern void htab_print_statistics (FILE *f
, const char *name
, htab_t table
);
32 /* String hash table functions. */
40 typedef struct string_tuple string_tuple_t
;
42 /* Hash function for a string_tuple. */
45 hash_string_tuple (const void *e
)
47 string_tuple_t
*tuple
= (string_tuple_t
*) e
;
48 return htab_hash_string (tuple
->key
);
51 /* Equality function for a string_tuple. */
54 eq_string_tuple (const void *a
, const void *b
)
56 const string_tuple_t
*ea
= (const string_tuple_t
*) a
;
57 const string_tuple_t
*eb
= (const string_tuple_t
*) b
;
59 return strcmp (ea
->key
, eb
->key
) == 0;
62 static inline string_tuple_t
*
63 string_tuple_alloc (const char *key
, char *value
)
65 string_tuple_t
*tuple
= XNEW (string_tuple_t
);
72 str_hash_find (htab_t table
, const char *key
)
74 string_tuple_t needle
= { key
, NULL
};
75 string_tuple_t
*tuple
= htab_find (table
, &needle
);
76 return tuple
!= NULL
? tuple
->value
: NULL
;
80 str_hash_find_n (htab_t table
, const char *key
, size_t n
)
82 char *tmp
= XNEWVEC (char, n
+ 1);
85 string_tuple_t needle
= { tmp
, NULL
};
86 string_tuple_t
*tuple
= htab_find (table
, &needle
);
88 return tuple
!= NULL
? tuple
->value
: NULL
;
92 str_hash_delete (htab_t table
, const char *key
)
94 string_tuple_t needle
= { key
, NULL
};
95 htab_remove_elt (table
, &needle
);
99 str_hash_insert (htab_t table
, const char *key
, void *value
)
101 htab_insert (table
, string_tuple_alloc (key
, value
));
105 str_htab_create (void)
107 return htab_create_alloc (16, hash_string_tuple
, eq_string_tuple
,
108 NULL
, xcalloc
, free
);