From: Alan Modra <amodra@gmail.com>
To: binutils@sourceware.org
Subject: gas hash.h tidy
Date: Sat, 9 Jul 2022 22:01:18 +0930 [thread overview]
Message-ID: <Ysl1FulmF+4SlKwR@squeak.grove.modra.org> (raw)
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.
diff --git a/gas/hash.c b/gas/hash.c
index 5ddd88ea0b2..aa6424eec57 100644
--- a/gas/hash.c
+++ b/gas/hash.c
@@ -20,6 +20,26 @@
#include "as.h"
+/* Hash function for a string_tuple. */
+
+hashval_t
+hash_string_tuple (const void *e)
+{
+ string_tuple_t *tuple = (string_tuple_t *) e;
+ return htab_hash_string (tuple->key);
+}
+
+/* Equality function for a string_tuple. */
+
+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;
+
+ 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. */
diff --git a/gas/hash.h b/gas/hash.h
index 2a7c99678f5..21f4a6dc42a 100644
--- a/gas/hash.h
+++ b/gas/hash.h
@@ -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;
}
--
Alan Modra
Australia Development Lab, IBM
reply other threads:[~2022-07-09 12:31 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=Ysl1FulmF+4SlKwR@squeak.grove.modra.org \
--to=amodra@gmail.com \
--cc=binutils@sourceware.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).