aboutsummaryrefslogtreecommitdiff
path: root/libiberty/hashtab.c
diff options
context:
space:
mode:
authorGeoffrey Keating <geoffk@redhat.com>2002-06-04 07:11:05 +0000
committerGeoffrey Keating <geoffk@gcc.gnu.org>2002-06-04 07:11:05 +0000
commite2500fedef1a1c5b9e818fd1e2c281adff80df4a (patch)
tree720630adca0f6b357e05c4feb8cbe33d556925ce /libiberty/hashtab.c
parentc2ae66169b8326bbf9b1dfa63083d2560fea7ddf (diff)
downloadgcc-e2500fedef1a1c5b9e818fd1e2c281adff80df4a.zip
gcc-e2500fedef1a1c5b9e818fd1e2c281adff80df4a.tar.gz
gcc-e2500fedef1a1c5b9e818fd1e2c281adff80df4a.tar.bz2
Merge from pch-branch up to tag pch-commit-20020603.
From-SVN: r54232
Diffstat (limited to 'libiberty/hashtab.c')
-rw-r--r--libiberty/hashtab.c67
1 files changed, 22 insertions, 45 deletions
diff --git a/libiberty/hashtab.c b/libiberty/hashtab.c
index 7477c35..06e41ac 100644
--- a/libiberty/hashtab.c
+++ b/libiberty/hashtab.c
@@ -158,59 +158,36 @@ eq_pointer (p1, p2)
/* This function creates table with length slightly longer than given
source length. Created hash table is initiated as empty (all the
hash table entries are EMPTY_ENTRY). The function returns the
- created hash table. Memory allocation must not fail. */
+ created hash table, or NULL if memory allocation fails. */
htab_t
-htab_create (size, hash_f, eq_f, del_f)
+htab_create_alloc (size, hash_f, eq_f, del_f, alloc_f, free_f)
size_t size;
htab_hash hash_f;
htab_eq eq_f;
htab_del del_f;
+ htab_alloc alloc_f;
+ htab_free free_f;
{
htab_t result;
size = higher_prime_number (size);
- result = (htab_t) xcalloc (1, sizeof (struct htab));
- result->entries = (PTR *) xcalloc (size, sizeof (PTR));
- result->size = size;
- result->hash_f = hash_f;
- result->eq_f = eq_f;
- result->del_f = del_f;
- result->return_allocation_failure = 0;
- return result;
-}
-
-/* This function creates table with length slightly longer than given
- source length. The created hash table is initiated as empty (all the
- hash table entries are EMPTY_ENTRY). The function returns the created
- hash table. Memory allocation may fail; it may return NULL. */
-
-htab_t
-htab_try_create (size, hash_f, eq_f, del_f)
- size_t size;
- htab_hash hash_f;
- htab_eq eq_f;
- htab_del del_f;
-{
- htab_t result;
-
- size = higher_prime_number (size);
- result = (htab_t) calloc (1, sizeof (struct htab));
+ result = (htab_t) (*alloc_f) (1, sizeof (struct htab));
if (result == NULL)
return NULL;
-
- result->entries = (PTR *) calloc (size, sizeof (PTR));
+ result->entries = (PTR *) (*alloc_f) (size, sizeof (PTR));
if (result->entries == NULL)
{
- free (result);
+ if (free_f != NULL)
+ (*free_f) (result);
return NULL;
}
-
result->size = size;
result->hash_f = hash_f;
result->eq_f = eq_f;
result->del_f = del_f;
- result->return_allocation_failure = 1;
+ result->alloc_f = alloc_f;
+ result->free_f = free_f;
return result;
}
@@ -229,8 +206,11 @@ htab_delete (htab)
&& htab->entries[i] != DELETED_ENTRY)
(*htab->del_f) (htab->entries[i]);
- free (htab->entries);
- free (htab);
+ if (htab->free_f != NULL)
+ {
+ (*htab->free_f) (htab->entries);
+ (*htab->free_f) (htab);
+ }
}
/* This function clears all entries in the given hash table. */
@@ -302,21 +282,17 @@ htab_expand (htab)
PTR *oentries;
PTR *olimit;
PTR *p;
+ PTR *nentries;
oentries = htab->entries;
olimit = oentries + htab->size;
htab->size = higher_prime_number (htab->size * 2);
- if (htab->return_allocation_failure)
- {
- PTR *nentries = (PTR *) calloc (htab->size, sizeof (PTR *));
- if (nentries == NULL)
- return 0;
- htab->entries = nentries;
- }
- else
- htab->entries = (PTR *) xcalloc (htab->size, sizeof (PTR *));
+ nentries = (PTR *) (*htab->alloc_f) (htab->size, sizeof (PTR *));
+ if (nentries == NULL)
+ return 0;
+ htab->entries = nentries;
htab->n_elements -= htab->n_deleted;
htab->n_deleted = 0;
@@ -337,7 +313,8 @@ htab_expand (htab)
}
while (p < olimit);
- free (oentries);
+ if (htab->free_f != NULL)
+ (*htab->free_f) (oentries);
return 1;
}