[dpdk-dev,2/4] hash: allocation of an existing object should fail

Message ID 1459351827-3346-3-git-send-email-olivier.matz@6wind.com (mailing list archive)
State Changes Requested, archived
Headers

Commit Message

Olivier Matz March 30, 2016, 3:30 p.m. UTC
  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(-)
  

Patch

diff --git a/lib/librte_hash/rte_cuckoo_hash.c b/lib/librte_hash/rte_cuckoo_hash.c
index 71b5b76..ccec2db 100644
--- a/lib/librte_hash/rte_cuckoo_hash.c
+++ b/lib/librte_hash/rte_cuckoo_hash.c
@@ -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) {
diff --git a/lib/librte_hash/rte_fbk_hash.c b/lib/librte_hash/rte_fbk_hash.c
index 8752a47..ba1e475 100644
--- a/lib/librte_hash/rte_fbk_hash.c
+++ b/lib/librte_hash/rte_fbk_hash.c
@@ -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) {