cex: avoid gratuitous heap allocations

There's no need to go for the heap when using gnulib's hash module.

* src/state-item.c (hash_pair_lookup, hash_pair_remove,
state_sym_lookup): Use the heap Luke.
That removes a leak from hash_pair_lookup.
(init_prods): Use hash_pair_insert instead of duplicating it.
This commit is contained in:
Akim Demaille
2020-05-17 16:28:08 +02:00
parent 2d8c3edbf8
commit 51fa2ab0a2

View File

@@ -68,13 +68,10 @@ hash_pair_free (hash_pair *hp)
static bitset static bitset
hash_pair_lookup (Hash_table *tab, int key) hash_pair_lookup (Hash_table *tab, int key)
{ {
hash_pair *l = xmalloc (sizeof (hash_pair)); hash_pair probe;
l->key = key; probe.key = key;
hash_pair *hp = (hash_pair *) hash_lookup (tab, l); hash_pair *hp = hash_lookup (tab, &probe);
if (!hp) return hp ? hp->l : NULL;
return NULL;
free (l);
return hp->l;
} }
static void static void
@@ -83,15 +80,18 @@ hash_pair_insert (Hash_table *tab, int key, bitset val)
hash_pair *hp = xmalloc (sizeof (hash_pair)); hash_pair *hp = xmalloc (sizeof (hash_pair));
hp->key = key; hp->key = key;
hp->l = val; hp->l = val;
hash_xinsert (tab, hp); hash_pair *res = hash_xinsert (tab, hp);
// This must be the first insertion.
(void) res;
assert (res == hp);
} }
static void static void
hash_pair_remove (Hash_table *tab, int key) hash_pair_remove (Hash_table *tab, int key)
{ {
hash_pair *hp = xmalloc (sizeof (hash_pair)); hash_pair probe;
hp->key = key; probe.key = key;
hash_delete (tab, hp); hash_delete (tab, &probe);
} }
/* A state_item from a state's id and the offset of the item within /* A state_item from a state's id and the offset of the item within
@@ -193,11 +193,9 @@ state_sym_comparator (const void *s1, const void *s2)
static state * static state *
state_sym_lookup (symbol_number sym, Hash_table *h) state_sym_lookup (symbol_number sym, Hash_table *h)
{ {
state *s = xmalloc (sizeof (state)); state probe;
s->accessing_symbol = sym; probe.accessing_symbol = sym;
state *res = hash_lookup (h, s); return hash_lookup (h, &probe);
free (s);
return res;
} }
static void static void
@@ -298,11 +296,8 @@ init_prods (void)
{ {
bitset copy = bitset_create (nstate_items, BITSET_SPARSE); bitset copy = bitset_create (nstate_items, BITSET_SPARSE);
bitset_copy (copy, lb); bitset_copy (copy, lb);
hash_pair *prod_hp = xmalloc (sizeof (hash_pair));
prod_hp->key = j;
prod_hp->l = copy;
// update prods. // update prods.
hash_xinsert (si_prods, prod_hp); hash_pair_insert (si_prods, j, copy);
// update revs. // update revs.
bitset_iterator biter; bitset_iterator biter;