diff options
author | Alan Modra <amodra@gmail.com> | 2022-07-07 13:50:29 +0930 |
---|---|---|
committer | Alan Modra <amodra@gmail.com> | 2022-07-09 21:33:49 +0930 |
commit | 1309c3165cc0751f5c4657e0e0ac4335640f2f03 (patch) | |
tree | 835eb0d8b192f8ff27c4b8ff03c51602faf7ac5d /gas/hash.h | |
parent | ed2917de68fe1ae3f279cef3b48c6464c75e9d72 (diff) | |
download | fsf-binutils-gdb-1309c3165cc0751f5c4657e0e0ac4335640f2f03.zip fsf-binutils-gdb-1309c3165cc0751f5c4657e0e0ac4335640f2f03.tar.gz fsf-binutils-gdb-1309c3165cc0751f5c4657e0e0ac4335640f2f03.tar.bz2 |
gas hash.h tidy
Only inline functions should be defined in hash.h, there's no benefit
in having multiple copies of hash_string_tuple and eq_string_tuple.
Also, use the table alloc_f when allocating tuples to be stored, so
that these functions are usable with different memory allocation
strategies.
* hash.h (struct string_tuple, string_tuple_t): Move earlier.
(string_tuple_alloc): Add table param, allocate using table alloc_f.
(str_hash_insert): Adjust to suit. Call table->free_f when
entry is not used.
(hash_string_tuple, eq_string_tuple): Move to..
* hash.c: ..here.
Diffstat (limited to 'gas/hash.h')
-rw-r--r-- | gas/hash.h | 48 |
1 files changed, 18 insertions, 30 deletions
@@ -21,18 +21,6 @@ #ifndef HASH_H #define HASH_H -/* Insert ELEMENT into HTAB. If REPLACE is non-zero existing elements - are overwritten. If ELEMENT already exists, a pointer to the slot - is returned. Otherwise NULL is returned. */ - -extern void **htab_insert (htab_t, void * /* element */, int /* replace */); - -/* Print statistics about a hash table. */ - -extern void htab_print_statistics (FILE *f, const char *name, htab_t table); - -/* String hash table functions. */ - struct string_tuple { const char *key; @@ -43,28 +31,28 @@ typedef struct string_tuple string_tuple_t; /* Hash function for a string_tuple. */ -static hashval_t -hash_string_tuple (const void *e) -{ - string_tuple_t *tuple = (string_tuple_t *) e; - return htab_hash_string (tuple->key); -} +extern hashval_t hash_string_tuple (const void *); /* Equality function for a string_tuple. */ -static int -eq_string_tuple (const void *a, const void *b) -{ - const string_tuple_t *ea = (const string_tuple_t *) a; - const string_tuple_t *eb = (const string_tuple_t *) b; +extern int eq_string_tuple (const void *, const void *); - return strcmp (ea->key, eb->key) == 0; -} +/* Insert ELEMENT into HTAB. If REPLACE is non-zero existing elements + are overwritten. If ELEMENT already exists, a pointer to the slot + is returned. Otherwise NULL is returned. */ + +extern void **htab_insert (htab_t, void * /* element */, int /* replace */); + +/* Print statistics about a hash table. */ + +extern void htab_print_statistics (FILE *f, const char *name, htab_t table); + +/* Inline string hash table functions. */ static inline string_tuple_t * -string_tuple_alloc (const char *key, const void *value) +string_tuple_alloc (htab_t table, const char *key, const void *value) { - string_tuple_t *tuple = XNEW (string_tuple_t); + string_tuple_t *tuple = table->alloc_f (1, sizeof (*tuple)); tuple->key = key; tuple->value = value; return tuple; @@ -100,10 +88,10 @@ str_hash_delete (htab_t table, const char *key) static inline void ** str_hash_insert (htab_t table, const char *key, const void *value, int replace) { - string_tuple_t *elt = string_tuple_alloc (key, value); + string_tuple_t *elt = string_tuple_alloc (table, key, value); void **slot = htab_insert (table, elt, replace); - if (slot && !replace) - free (elt); + if (slot && !replace && table->free_f) + table->free_f (elt); return slot; } |