summaryrefslogtreecommitdiffstats
path: root/libopkg/hash_table.c
diff options
context:
space:
mode:
authorticktock35 <ticktock35@e8e0d7a0-c8d9-11dd-a880-a1081c7ac358>2008-12-15 00:30:29 (EST)
committer ticktock35 <ticktock35@e8e0d7a0-c8d9-11dd-a880-a1081c7ac358>2008-12-15 00:30:29 (EST)
commit733d8409723a397b4ced39ea7aaab8bc8b4af5d9 (patch)
tree7138fbfc11577cf7d0fbae0ae7fa98a02ef63a8e /libopkg/hash_table.c
parent1cefade73444d4670d9ae7c06e8f9cc55492fd79 (diff)
opkg: adding the hash_table_remove API, not using yet.
Just complete the API for future usage. Clean all the entry at initial time. This reduces planty of unnecessary check. In order to prevent this kind of bug, using calloc to replace most malloc git-svn-id: http://opkg.googlecode.com/svn/trunk@160 e8e0d7a0-c8d9-11dd-a880-a1081c7ac358
Diffstat (limited to 'libopkg/hash_table.c')
-rw-r--r--libopkg/hash_table.c33
1 files changed, 32 insertions, 1 deletions
diff --git a/libopkg/hash_table.c b/libopkg/hash_table.c
index e7f5a92..713ecff 100644
--- a/libopkg/hash_table.c
+++ b/libopkg/hash_table.c
@@ -146,7 +146,7 @@ int hash_table_insert(hash_table_t *hash, const char *key, void *value)
return 0;
}
}
- hash_entry->next = (hash_entry_t *)malloc(sizeof(hash_entry_t));
+ hash_entry->next = (hash_entry_t *)calloc(1, sizeof(hash_entry_t));
if (!hash_entry->next) {
return -ENOMEM;
}
@@ -161,6 +161,37 @@ int hash_table_insert(hash_table_t *hash, const char *key, void *value)
return 0;
}
+int hash_table_remove(hash_table_t *hash, const char *key)
+{
+ int ndx= hash_index(hash, key);
+ hash_entry_t *hash_entry = hash->entries + ndx;
+ hash_entry_t *next_entry=NULL, *last_entry=NULL;
+ while (hash_entry)
+ {
+ if (hash_entry->key)
+ {
+ if (strcmp(key, hash_entry->key) == 0) {
+ free(hash_entry->key);
+ if (last_entry) {
+ last_entry->next = hash_entry->next;
+ free(hash_entry);
+ } else {
+ next_entry = hash_entry->next;
+ if (next_entry) {
+ memmove(hash_entry, next_entry, sizeof(hash_entry_t));
+ free(next_entry);
+ } else {
+ memset(hash_entry, 0 , sizeof(hash_entry_t));
+ }
+ }
+ return 1;
+ }
+ }
+ last_entry = hash_entry;
+ hash_entry = hash_entry->next;
+ }
+ return 0;
+}
void hash_table_foreach(hash_table_t *hash, void (*f)(const char *key, void *entry, void *data), void *data)
{