3 mhashtable_t mlookup; //Global hash table
5 // Creates a machine lookup table with size =" size"
6 unsigned int mhashCreate(unsigned int size, double loadfactor) {
7 mhashlistnode_t *nodes;
8 // Allocate space for the hash table
9 if((nodes = calloc(size, sizeof(mhashlistnode_t))) == NULL) {
10 printf("Calloc error %s %d\n", __FILE__, __LINE__);
14 mlookup.table = nodes;
16 mlookup.threshold=size*loadfactor;
17 mlookup.mask = (size << 1) -1;
18 mlookup.numelements = 0; // Initial number of elements in the hash
19 mlookup.loadfactor = loadfactor;
20 //Initialize the pthread_mutex variable
21 pthread_mutex_init(&mlookup.locktable, NULL);
25 // Assign to keys to bins inside hash table
26 unsigned int mhashFunction(unsigned int key) {
27 return ( key & mlookup.mask) >>1;
30 // Insert value and key mapping into the hash table
31 void mhashInsert(unsigned int key, void *val) {
32 mhashlistnode_t *ptr, *node;
34 pthread_mutex_lock(&mlookup.locktable);
35 if (mlookup.numelements > mlookup.threshold) {
37 unsigned int newsize = mlookup.size << 1;
41 ptr = &mlookup.table[(key & mlookup.mask) >>1];
42 mlookup.numelements++;
48 } else { // Insert in the beginning of linked list
49 node = calloc(1, sizeof(mhashlistnode_t));
52 node->next = ptr->next;
55 pthread_mutex_unlock(&mlookup.locktable);
58 // Return val for a given key in the hash table
59 void *mhashSearch(unsigned int key) {
61 mhashlistnode_t *node;
62 pthread_mutex_lock(&mlookup.locktable);
63 node = &mlookup.table[(key & mlookup.mask)>>1];
65 if(node->key == key) {
67 pthread_mutex_unlock(&mlookup.locktable);
73 pthread_mutex_unlock(&mlookup.locktable);
77 // Remove an entry from the hash table
78 unsigned int mhashRemove(unsigned int key) {
80 mhashlistnode_t *curr, *prev;
81 mhashlistnode_t *ptr, *node;
83 pthread_mutex_lock(&mlookup.locktable);
85 index = mhashFunction(key);
87 for (; curr != NULL; curr = curr->next) {
88 if (curr->key == key) { // Find a match in the hash table
89 mlookup.numelements--; // Decrement the number of elements in the global hashtable
90 if ((curr == &ptr[index]) && (curr->next == NULL)) { // Delete the first item inside the hashtable with no linked list of mhashlistnode_t
93 } else if ((curr == &ptr[index]) && (curr->next != NULL)) { //Delete the first item with a linked list of mhashlistnode_t connected
94 curr->key = curr->next->key;
95 curr->val = curr->next->val;
97 curr->next = curr->next->next;
99 } else { // Regular delete from linked listed
100 prev->next = curr->next;
103 pthread_mutex_unlock(&mlookup.locktable);
108 pthread_mutex_unlock(&mlookup.locktable);
115 unsigned int mhashResize(unsigned int newsize) {
116 mhashlistnode_t *node, *ptr, *curr; // curr and next keep track of the current and the next mhashlistnodes in a linked list
117 unsigned int oldsize;
118 int isfirst; // Keeps track of the first element in the mhashlistnode_t for each bin in hashtable
119 unsigned int i,index;
123 oldsize = mlookup.size;
125 if((node = calloc(newsize, sizeof(mhashlistnode_t))) == NULL) {
126 printf("Calloc error %s %d\n", __FILE__, __LINE__);
130 mlookup.table = node; //Update the global hashtable upon resize()
131 mlookup.size = newsize;
132 mlookup.threshold=newsize*mlookup.loadfactor;
133 mask=mlookup.mask = (newsize << 1)-1;
135 for(i = 0; i < oldsize; i++) { //Outer loop for each bin in hash table
140 mhashlistnode_t *tmp,*next;
142 if ((key=curr->key) == 0) { //Exit inner loop if there the first element for a given bin/index is NULL
143 break; //key = val =0 for element if not present within the hash table
146 index = (key & mask) >>1;
147 tmp=&mlookup.table[index];
149 // Insert into the new table
157 NOTE: Add this case if you change this...
158 This case currently never happens because of the way things rehash....
160 mhashlistnode_t *newnode = calloc(1, sizeof(mhashlistnode_t));
161 newnode->key = curr->key;
162 newnode->val = curr->val;
163 newnode->next = tmp->next;
167 curr->next=tmp->next;
175 free(ptr); //Free the memory of the old hash table
179 unsigned int *mhashGetKeys(unsigned int *numKeys) {
182 mhashlistnode_t *curr;
184 pthread_mutex_lock(&mlookup.locktable);
186 *numKeys = mlookup.numelements;
187 keys = calloc(*numKeys, sizeof(unsigned int));
190 for (i = 0; i < mlookup.size; i++) {
191 if (mlookup.table[i].key != 0) {
192 curr = &mlookup.table[i];
193 while (curr != NULL) {
194 keys[keyindex++] = curr->key;
200 if (keyindex != *numKeys)
201 printf("mhashGetKeys(): WARNING: incorrect mlookup.numelements value!\n");
203 pthread_mutex_unlock(&mlookup.locktable);