1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2016, Olivier MATZ <zer0@droids-corp.org>
6 * Simple hash table API
8 * This file provides functions to store objects in hash tables, using strings
18 typedef void (*ec_keyval_elt_free_t)(void *);
23 * Create a hash table.
26 * The hash table, or NULL on error (errno is set).
28 struct ec_keyval *ec_keyval(void);
31 * Get a value from the hash table.
38 * The element if it is found, or NULL on error (errno is set).
39 * In case of success but the element is NULL, errno is set to 0.
41 void *ec_keyval_get(const struct ec_keyval *keyval, const char *key);
44 * Check if the hash table contains this key.
51 * true if it contains the key, else false.
53 bool ec_keyval_has_key(const struct ec_keyval *keyval, const char *key);
56 * Delete an object from the hash table.
63 * 0 on success, or -1 on error (errno is set).
65 int ec_keyval_del(struct ec_keyval *keyval, const char *key);
68 * Add/replace an object in the hash table.
75 * The pointer to be saved in the hash table.
77 * An optional pointer to a destructor function called when an
78 * object is destroyed (ec_keyval_del() or ec_keyval_free()).
80 * 0 on success, or -1 on error (errno is set).
81 * On error, the passed value is freed (free_cb(val) is called).
83 int ec_keyval_set(struct ec_keyval *keyval, const char *key, void *val,
84 ec_keyval_elt_free_t free_cb);
87 * Free a hash table an all its objects.
92 void ec_keyval_free(struct ec_keyval *keyval);
95 * Get the length of a hash table.
100 * The length of the hash table.
102 size_t ec_keyval_len(const struct ec_keyval *keyval);
105 * Duplicate a hash table
107 * A reference counter is shared between the clones of
108 * hash tables so that the objects are freed only when
109 * the last reference is destroyed.
114 * The duplicated hash table, or NULL on error (errno is set).
116 struct ec_keyval *ec_keyval_dup(const struct ec_keyval *keyval);
122 * The stream where the dump is sent.
126 void ec_keyval_dump(FILE *out, const struct ec_keyval *keyval);