#ifndef HASH_H
#define HASH_H
-/* Insert ELEMENT into HTAB. If the element exists, it is overwritten. */
+/* 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 *);
+extern void **htab_insert (htab_t, void * /* element */, int /* replace */);
/* Print statistics about a hash table. */
struct string_tuple
{
const char *key;
- char *value;
+ const void *value;
};
typedef struct string_tuple string_tuple_t;
}
static inline string_tuple_t *
-string_tuple_alloc (const char *key, char *value)
+string_tuple_alloc (const char *key, const void *value)
{
string_tuple_t *tuple = XNEW (string_tuple_t);
tuple->key = key;
{
string_tuple_t needle = { key, NULL };
string_tuple_t *tuple = htab_find (table, &needle);
- return tuple != NULL ? tuple->value : NULL;
+ return tuple != NULL ? (void *) tuple->value : NULL;
}
static inline void *
string_tuple_t needle = { tmp, NULL };
string_tuple_t *tuple = htab_find (table, &needle);
free (tmp);
- return tuple != NULL ? tuple->value : NULL;
+ return tuple != NULL ? (void *) tuple->value : NULL;
}
static inline void
htab_remove_elt (table, &needle);
}
-static inline void
-str_hash_insert (htab_t table, const char *key, void *value)
+static inline void **
+str_hash_insert (htab_t table, const char *key, const void *value, int replace)
{
- htab_insert (table, string_tuple_alloc (key, value));
+ string_tuple_t *elt = string_tuple_alloc (key, value);
+ void **slot = htab_insert (table, elt, replace);
+ if (slot && !replace)
+ free (elt);
+ return slot;
}
static inline htab_t