[Utils] Add a missing destroy function for hash table
authorDongju Chae <dongju.chae@samsung.com>
Thu, 4 Jul 2019 05:46:58 +0000 (14:46 +0900)
committer함명주/On-Device Lab(SR)/Principal Engineer/삼성전자 <myungjoo.ham@samsung.com>
Mon, 8 Jul 2019 10:17:41 +0000 (19:17 +0900)
This commit adds the destroy function for hash table.

Signed-off-by: Dongju Chae <dongju.chae@samsung.com>
core/npu-engine/src/ne-utils.c
core/npu-engine/src/ne-utils.h

index d4aed13..0c78c2a 100644 (file)
@@ -157,8 +157,9 @@ list_del (list *list, list_node *node)
  * @brief initialize hash table
  * @param[in] ht the instance of hash table
  * @param[in] size the size of bucket (should be a power of 2)
+ * @return 0 if ok. otherwise a negative error value
  */
-void
+int
 hash_table_init (hash_table *ht, uint32_t size)
 {
   int i;
@@ -169,8 +170,36 @@ hash_table_init (hash_table *ht, uint32_t size)
   ht->size = size;
   ht->list = (list *) malloc (sizeof (list) * size);
 
+  if (ht->list == NULL)
+    return -ENOMEM;
+
   for (i = 0; i < size; i++) 
     list_init (&ht->list[i]);
+
+  return 0;
+}
+
+/**
+ * @brief destroy hash table
+ * @param[in] ht the instance of hash table
+ * @return 0 if ok. otherwise a negative error value
+ */
+int
+hash_table_destroy (hash_table *ht)
+{
+  if (!(ht && ht->list))
+    return -EINVAL;
+
+  /**
+   * there're hash table nodes which was not allocated yet
+   * the caller should deallocate them before the table destruction
+   */
+  if (ht->num > 0)
+    return -EBUSY;
+
+  free (ht->list);
+
+  return 0;
 }
 
 /**
index ec71f21..f6caca7 100644 (file)
@@ -149,8 +149,16 @@ typedef struct _hash_table {
  * @brief initialize hash table
  * @param[in] ht the instance of hash table
  * @param[in] size the size of bucket (should be a power of 2)
+ * @return 0 if ok. otherwise a negative error value
  */
-void hash_table_init (hash_table *ht, uint32_t size);
+int hash_table_init (hash_table *ht, uint32_t size);
+
+/**
+ * @brief destroy hash table
+ * @param[in] ht the instance of hash table
+ * @return 0 if ok. otherwise a negative error value
+ */
+int hash_table_destroy (hash_table *ht);
 
 /**
  * @brief add the node with key into the hash table