6 #include <sys/malloc.h>
8 #include "hashtable.h" // XXX fix path later
11 struct new_obj *next; /* Next object in the list */
12 char obj[0]; /* actually bigger */
16 struct new_hash_table {
17 int table_size; /* Size of the table (buckets) */
18 int table_obj; /* number of object in the table */
19 int obj_size; /* size of object (key + value) */
20 /* Hash function for this table */
21 uint32_t (*hash)(const void *key, uint32_t size);
22 int (*cmp)(const void *obj1, const void *obj2);
23 int hash_arg; /* hash function parameter */
24 struct malloc_type *mtype;
25 struct new_obj **table_ptr; /* Pointer to the table */
29 * initialize an hash table
30 * - size: size of table (number of buckets)
31 * - obj_size: size of the object to store in the table (key + value)
32 * - hf: pointer to the hash function for this table
33 * - compare: function to compare two objects
35 * Return value: pointer to the hash table, NULL if error occurs
37 struct new_hash_table *
38 new_table_init (int size, int obj_size,
39 uint32_t (hf)(const void *, uint32_t size),
40 int (compare)(const void *, const void *),
41 struct malloc_type *mtype)
43 struct new_hash_table *h;
45 printf("%s called\n", __FUNCTION__);
47 h = malloc(sizeof(struct new_hash_table), mtype, M_NOWAIT | M_ZERO);
51 h->table_ptr = malloc(size * sizeof(struct new_obj*), mtype,
53 if (h->table_ptr == NULL) { /* no memory */
61 h->obj_size = obj_size;
67 new_table_insert_obj (struct new_hash_table *h, const void *obj)
69 int i; /* array index */
70 struct new_obj *o, *ot;
72 i = h->hash(obj, h->table_size);
74 /* same key not allowed */
75 for (ot = h->table_ptr[i]; ot; ot = ot->next) {
76 if (h->cmp(obj, ot->obj) == 0)
79 /* allocate a single chunk of memory */
80 o = malloc(sizeof(*o) + h->obj_size, h->mtype, M_NOWAIT);
83 bcopy(obj, o->obj, h->obj_size);
86 o->next = h->table_ptr[i];
95 new_table_delete_obj(struct new_hash_table *h, const void *obj)
98 struct new_obj *obj1, *prev;
100 i = h->hash(obj, h->table_size);
102 for (prev = NULL, obj1 = h->table_ptr[i]; obj1; obj1 = obj1->next) {
103 if (h->cmp(obj, (void *)obj1->obj) != 0)
105 /* Object found, delete */
107 prev->next = obj1->next;
109 h->table_ptr[i] = obj1->next;
110 free(obj1, h->mtype);
114 return 1; /* Not found */
118 new_table_extract_obj(struct new_hash_table *h, const void *obj)
122 if (h == NULL || h->table_obj == 0)
125 i = h->hash(obj, h->table_size);
126 for (o = h->table_ptr[i]; o; o = o->next) {
127 if (h->cmp(o->obj, obj) == 0)
134 new_table_destroy(struct new_hash_table *h)
137 struct new_obj *cur, *next;
139 if (!h || !h->table_ptr)
141 for (i = 0; i < h->table_size; i++) {
142 for (cur = h->table_ptr[i]; cur; cur = next) {
147 free (h->table_ptr, h->mtype);
153 /* returns the number of elements in the table */
155 new_table_get_element(const struct new_hash_table *h)
157 return h ? h->table_obj : 0;
161 table_next(struct new_hash_table *h, const void *o)
166 printf("%s called\n", __FUNCTION__);
167 if (h == NULL || h->table_obj == 0)
170 for (i = 0; i < h->table_size; i++)
172 return h->table_ptr[i]->obj;
173 return NULL; /* XXX should not happen */
176 /* here we can optimize if we can map o to the bucket,
177 * otherwise locate o and find the next one.
179 i = h->hash(o, h->table_size);
180 for (obj = h->table_ptr[i]; obj; obj = obj->next) {
181 if (h->cmp(obj->obj, o) == 0)
184 if (obj && obj->next != NULL)
185 return obj->next->obj;
186 /* take the first of the next bucket */
187 for (i++; i < h->table_size; i++) {
189 return h->table_ptr[i]->obj;