[dpdk-dev,2/4] hash: allocation of an existing object should fail
Commit Message
Change the API of rte_hash*_create() functions to return NULL and set
rte_errno to EEXIST when the object name already exists.
These functions were returning a pointer to the existing object in that
case, but it is a problem as the caller did not know if the object had
to be freed or not.
Doing this change also makes the hash API more consistent with the other
APIs (mempool, rings, ...).
Signed-off-by: Olivier Matz <olivier.matz@6wind.com>
---
lib/librte_hash/rte_cuckoo_hash.c | 6 ++++--
lib/librte_hash/rte_fbk_hash.c | 5 ++++-
2 files changed, 8 insertions(+), 3 deletions(-)
@@ -230,8 +230,10 @@ rte_hash_create(const struct rte_hash_parameters *params)
/* Guarantee there's no existing */
h = rte_hash_find_existing(params->name);
- if (h != NULL)
- return h;
+ if (h != NULL) {
+ rte_errno = EEXIST;
+ return NULL;
+ }
te = rte_zmalloc("HASH_TAILQ_ENTRY", sizeof(*te), 0);
if (te == NULL) {
@@ -140,8 +140,11 @@ rte_fbk_hash_create(const struct rte_fbk_hash_params *params)
if (strncmp(params->name, ht->name, RTE_FBK_HASH_NAMESIZE) == 0)
break;
}
- if (te != NULL)
+ if (te != NULL) {
+ ht = NULL;
+ rte_errno = EEXIST;
goto exit;
+ }
te = rte_zmalloc("FBK_HASH_TAILQ_ENTRY", sizeof(*te), 0);
if (te == NULL) {