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;
10 // Allocate space for the hash table
11 if((nodes = calloc(size, sizeof(mhashlistnode_t))) == NULL) {
12 printf("Calloc error %s %d\n", __FILE__, __LINE__);
16 mlookup.table = nodes;
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.size));
30 // Insert value and key mapping into the hash table
31 unsigned int mhashInsert(unsigned int key, void *val) {
34 mhashlistnode_t *ptr, *node;
36 if (mlookup.numelements > (mlookup.loadfactor * mlookup.size)) {
38 newsize = 2 * mlookup.size + 1;
39 pthread_mutex_lock(&mlookup.locktable);
41 pthread_mutex_unlock(&mlookup.locktable);
44 mlookup.numelements++;
46 index = mhashFunction(key);
48 printf("DEBUG -> index = %d, key = %d, val = %x\n", index, key, val);
50 pthread_mutex_lock(&mlookup.locktable);
51 if(ptr[index].next == NULL && ptr[index].key == 0) { // Insert at the first position in the hashtable
54 } else { // Insert in the beginning of linked list
55 if ((node = calloc(1, sizeof(mhashlistnode_t))) == NULL) {
56 printf("Calloc error %s, %d\n", __FILE__, __LINE__);
57 pthread_mutex_unlock(&mlookup.locktable);
62 node->next = ptr[index].next;
63 ptr[index].next = node;
65 pthread_mutex_unlock(&mlookup.locktable);
69 // Return val for a given key in the hash table
70 void *mhashSearch(unsigned int key) {
72 mhashlistnode_t *ptr, *node;
74 ptr = mlookup.table; // Address of the beginning of hash table
75 index = mhashFunction(key);
77 pthread_mutex_lock(&mlookup.locktable);
79 if(node->key == key) {
80 pthread_mutex_unlock(&mlookup.locktable);
85 pthread_mutex_unlock(&mlookup.locktable);
89 // Remove an entry from the hash table
90 unsigned int mhashRemove(unsigned int key) {
92 mhashlistnode_t *curr, *prev;
93 mhashlistnode_t *ptr, *node;
96 index = mhashFunction(key);
99 pthread_mutex_lock(&mlookup.locktable);
100 for (; curr != NULL; curr = curr->next) {
101 if (curr->key == key) { // Find a match in the hash table
102 mlookup.numelements--; // Decrement the number of elements in the global hashtable
103 if ((curr == &ptr[index]) && (curr->next == NULL)) { // Delete the first item inside the hashtable with no linked list of mhashlistnode_t
106 } else if ((curr == &ptr[index]) && (curr->next != NULL)) { //Delete the first item with a linked list of mhashlistnode_t connected
107 curr->key = curr->next->key;
108 curr->val = curr->next->val;
110 curr->next = curr->next->next;
112 } else { // Regular delete from linked listed
113 prev->next = curr->next;
116 pthread_mutex_unlock(&mlookup.locktable);
121 pthread_mutex_unlock(&mlookup.locktable);
126 unsigned int mhashResize(unsigned int newsize) {
127 mhashlistnode_t *node, *ptr, *curr, *next; // curr and next keep track of the current and the next mhashlistnodes in a linked list
128 unsigned int oldsize;
129 int isfirst; // Keeps track of the first element in the mhashlistnode_t for each bin in hashtable
131 mhashlistnode_t *newnode;
134 oldsize = mlookup.size;
136 if((node = calloc(newsize, sizeof(mhashlistnode_t))) == NULL) {
137 printf("Calloc error %s %d\n", __FILE__, __LINE__);
141 mlookup.table = node; //Update the global hashtable upon resize()
142 mlookup.size = newsize;
143 mlookup.numelements = 0;
145 for(i = 0; i < oldsize; i++) { //Outer loop for each bin in hash table
148 while (curr != NULL) { //Inner loop to go through linked lists
149 if (curr->key == 0) { //Exit inner loop if there the first element for a given bin/index is NULL
150 break; //key = val =0 for element if not present within the hash table
154 index = mhashFunction(curr->key);
156 printf("DEBUG(resize) -> index = %d, key = %d, val = %x\n", index, curr->key, curr->val);
158 // Insert into the new table
159 if(mlookup.table[index].next == NULL && mlookup.table[index].key == 0) {
160 mlookup.table[index].key = curr->key;
161 mlookup.table[index].val = curr->val;
162 mlookup.numelements++;
164 if((newnode = calloc(1, sizeof(mhashlistnode_t))) == NULL) {
165 printf("Calloc error %s, %d\n", __FILE__, __LINE__);
168 newnode->key = curr->key;
169 newnode->val = curr->val;
170 newnode->next = mlookup.table[index].next;
171 mlookup.table[index].next = newnode;
172 mlookup.numelements++;
175 //free the linked list of mhashlistnode_t if not the first element in the hash table
186 free(ptr); //Free the memory of the old hash table