1 /* Copyright (c) 2008, UNINETT AS */
2 /* See LICENSE for licensing information. */
10 /* allocates and initialises hash structure; returns NULL if malloc fails */
11 struct hash *hash_create() {
12 struct hash *h = malloc(sizeof(struct hash));
15 h->hashlist = list_create();
20 pthread_mutex_init(&h->mutex, NULL);
24 /* frees all memory associated with the hash */
25 void hash_destroy(struct hash *h) {
30 for (ln = list_first(h->hashlist); ln; ln = list_next(ln)) {
31 free(((struct hash_entry *)ln->data)->key);
32 free(((struct hash_entry *)ln->data)->data);
34 list_destroy(h->hashlist);
35 pthread_mutex_destroy(&h->mutex);
38 /* insert entry in hash; returns 1 if ok, 0 if malloc fails */
39 int hash_insert(struct hash *h, void *key, uint32_t keylen, void *data) {
44 e = malloc(sizeof(struct hash_entry));
47 memset(e, 0, sizeof(struct hash_entry));
48 e->key = malloc(keylen);
53 memcpy(e->key, key, keylen);
56 pthread_mutex_lock(&h->mutex);
57 if (!list_push(h->hashlist, e)) {
58 pthread_mutex_unlock(&h->mutex);
63 pthread_mutex_unlock(&h->mutex);
67 /* reads entry from hash */
68 void *hash_read(struct hash *h, void *key, uint32_t keylen) {
74 pthread_mutex_lock(&h->mutex);
75 for (ln = list_first(h->hashlist); ln; ln = list_next(ln)) {
76 e = (struct hash_entry *)ln->data;
77 if (e->keylen == keylen && !memcmp(e->key, key, keylen)) {
78 pthread_mutex_unlock(&h->mutex);
82 pthread_mutex_unlock(&h->mutex);
86 /* extracts entry from hash */
87 void *hash_extract(struct hash *h, void *key, uint32_t keylen) {
93 pthread_mutex_lock(&h->mutex);
94 for (ln = list_first(h->hashlist); ln; ln = list_next(ln)) {
95 e = (struct hash_entry *)ln->data;
96 if (e->keylen == keylen && !memcmp(e->key, key, keylen)) {
98 list_removedata(h->hashlist, e);
100 pthread_mutex_unlock(&h->mutex);
104 pthread_mutex_unlock(&h->mutex);
108 /* returns first entry */
109 struct hash_entry *hash_first(struct hash *hash) {
110 struct list_node *ln;
111 struct hash_entry *e;
112 if (!hash || !((ln = list_first(hash->hashlist))))
114 e = (struct hash_entry *)ln->data;
119 /* returns the next node after the argument */
120 struct hash_entry *hash_next(struct hash_entry *entry) {
121 struct hash_entry *e;
122 if (!entry || !entry->next)
124 e = (struct hash_entry *)entry->next->data;
125 e->next = (struct list_node *)entry->next->next;
129 /* Local Variables: */
130 /* c-file-style: "stroustrup" */