3 mhashtable_t mlookup; //Global hash table
5 // Creates a machine lookup table with size =" size"
6 unsigned int mhashCreate(unsigned int size, float 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.numelements = 0; // Initial number of elements in the hash
17 mlookup.loadfactor = loadfactor;
18 //Initialize the pthread_mutex variable
19 pthread_mutex_init(&mlookup.locktable, NULL);
23 // Assign to keys to bins inside hash table
24 unsigned int mhashFunction(unsigned int key) {
25 return( key % (mlookup.size));
28 // Insert value and key mapping into the hash table
29 unsigned int mhashInsert(unsigned int key, void *val) {
32 mhashlistnode_t *ptr, *node;
34 if (mlookup.numelements > (mlookup.loadfactor * mlookup.size)) {
36 newsize = 2 * mlookup.size + 1;
37 pthread_mutex_lock(&mlookup.locktable);
39 pthread_mutex_unlock(&mlookup.locktable);
42 mlookup.numelements++;
45 printf("DEBUG -> index = %d, key = %d, val = %x\n", index, key, val);
47 pthread_mutex_lock(&mlookup.locktable);
48 index = mhashFunction(key);
49 if(ptr[index].next == NULL && ptr[index].key == 0) { // Insert at the first position in the hashtable
52 } else { // Insert in the beginning of linked list
53 if ((node = calloc(1, sizeof(mhashlistnode_t))) == NULL) {
54 printf("Calloc error %s, %d\n", __FILE__, __LINE__);
55 pthread_mutex_unlock(&mlookup.locktable);
60 node->next = ptr[index].next;
61 ptr[index].next = node;
63 pthread_mutex_unlock(&mlookup.locktable);
67 // Return val for a given key in the hash table
68 void *mhashSearch(unsigned int key) {
70 mhashlistnode_t *ptr, *node;
72 pthread_mutex_lock(&mlookup.locktable);
73 ptr = mlookup.table; // Address of the beginning of hash table
74 index = mhashFunction(key);
77 if(node->key == key) {
78 pthread_mutex_unlock(&mlookup.locktable);
83 pthread_mutex_unlock(&mlookup.locktable);
87 // Remove an entry from the hash table
88 unsigned int mhashRemove(unsigned int key) {
90 mhashlistnode_t *curr, *prev;
91 mhashlistnode_t *ptr, *node;
93 pthread_mutex_lock(&mlookup.locktable);
95 index = mhashFunction(key);
97 for (; curr != NULL; curr = curr->next) {
98 if (curr->key == key) { // Find a match in the hash table
99 mlookup.numelements--; // Decrement the number of elements in the global hashtable
100 if ((curr == &ptr[index]) && (curr->next == NULL)) { // Delete the first item inside the hashtable with no linked list of mhashlistnode_t
103 } else if ((curr == &ptr[index]) && (curr->next != NULL)) { //Delete the first item with a linked list of mhashlistnode_t connected
104 curr->key = curr->next->key;
105 curr->val = curr->next->val;
107 curr->next = curr->next->next;
109 } else { // Regular delete from linked listed
110 prev->next = curr->next;
113 pthread_mutex_unlock(&mlookup.locktable);
118 pthread_mutex_unlock(&mlookup.locktable);
123 unsigned int mhashResize(unsigned int newsize) {
124 mhashlistnode_t *node, *ptr, *curr, *next; // curr and next keep track of the current and the next mhashlistnodes in a linked list
125 unsigned int oldsize;
126 int isfirst; // Keeps track of the first element in the mhashlistnode_t for each bin in hashtable
128 mhashlistnode_t *newnode;
131 oldsize = mlookup.size;
133 if((node = calloc(newsize, sizeof(mhashlistnode_t))) == NULL) {
134 printf("Calloc error %s %d\n", __FILE__, __LINE__);
138 mlookup.table = node; //Update the global hashtable upon resize()
139 mlookup.size = newsize;
140 mlookup.numelements = 0;
142 for(i = 0; i < oldsize; i++) { //Outer loop for each bin in hash table
145 while (curr != NULL) { //Inner loop to go through linked lists
146 if (curr->key == 0) { //Exit inner loop if there the first element for a given bin/index is NULL
147 break; //key = val =0 for element if not present within the hash table
151 index = mhashFunction(curr->key);
153 printf("DEBUG(resize) -> index = %d, key = %d, val = %x\n", index, curr->key, curr->val);
155 // Insert into the new table
156 if(mlookup.table[index].next == NULL && mlookup.table[index].key == 0) {
157 mlookup.table[index].key = curr->key;
158 mlookup.table[index].val = curr->val;
159 mlookup.numelements++;
161 if((newnode = calloc(1, sizeof(mhashlistnode_t))) == NULL) {
162 printf("Calloc error %s, %d\n", __FILE__, __LINE__);
165 newnode->key = curr->key;
166 newnode->val = curr->val;
167 newnode->next = mlookup.table[index].next;
168 mlookup.table[index].next = newnode;
169 mlookup.numelements++;
172 //free the linked list of mhashlistnode_t if not the first element in the hash table
183 free(ptr); //Free the memory of the old hash table
187 unsigned int *mhashGetKeys(unsigned int *numKeys) {
190 mhashlistnode_t *curr;
192 pthread_mutex_lock(&mlookup.locktable);
194 *numKeys = mlookup.numelements;
195 keys = calloc(*numKeys, sizeof(unsigned int));
198 for (i = 0; i < mlookup.size; i++)
200 if (mlookup.table[i].key != 0){
201 curr = &mlookup.table[i];
202 while (curr != NULL){
203 keys[keyindex++] = curr->key;
209 if (keyindex != *numKeys)
210 printf("mhashGetKeys(): WARNING: incorrect mlookup.numelements value!\n");
212 pthread_mutex_unlock(&mlookup.locktable);