* @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;
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;
}
/**
* @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