1 /* LOCK THE ENTIRE HASH TABLE */
4 prehashtable_t pflookup; //Global prefetch cache table
6 unsigned int prehashCreate(unsigned int size, float loadfactor) {
7 prehashlistnode_t *nodes;
10 // Allocate space for the hash table
11 if((nodes = calloc(size, sizeof(prehashlistnode_t))) == NULL) {
12 printf("Calloc error %s %d\n", __FILE__, __LINE__);
16 pflookup.table = nodes;
18 pflookup.numelements = 0; // Initial number of elements in the hash
19 pflookup.loadfactor = loadfactor;
21 //Intiliaze and set prefetch table mutex attribute
22 pthread_mutexattr_init(&pflookup.prefetchmutexattr);
23 //NOTE:PTHREAD_MUTEX_RECURSIVE is currently inside a #if_def UNIX98 in the pthread.h file
24 //Therefore use PTHREAD_MUTEX_RECURSIVE_NP instead
25 pthread_mutexattr_settype(&pflookup.prefetchmutexattr, PTHREAD_MUTEX_RECURSIVE_NP);
27 //Initialize mutex var
28 pthread_mutex_init(&pflookup.lock, &pflookup.prefetchmutexattr);
29 //pthread_mutex_init(&pflookup.lock, NULL);
30 pthread_cond_init(&pflookup.cond, NULL);
34 //Assign keys to bins inside hash table
35 unsigned int prehashFunction(unsigned int key) {
36 return ( key % (pflookup.size));
39 //Store oids and their pointers into hash
40 unsigned int prehashInsert(unsigned int key, void *val) {
43 prehashlistnode_t *ptr, *node;
45 if(pflookup.numelements > (pflookup.loadfactor * pflookup.size)) {
47 newsize = 2 * pflookup.size + 1;
48 pthread_mutex_lock(&pflookup.lock);
49 prehashResize(newsize);
50 pthread_mutex_unlock(&pflookup.lock);
54 pflookup.numelements++;
56 pthread_mutex_lock(&pflookup.lock);
57 index = prehashFunction(key);
58 if(ptr[index].next == NULL && ptr[index].key == 0) { // Insert at the first position in the hashtable
61 } else { // Insert in the beginning of linked list
62 if ((node = calloc(1, sizeof(prehashlistnode_t))) == NULL) {
63 printf("Calloc error %s, %d\n", __FILE__, __LINE__);
64 pthread_mutex_unlock(&pflookup.lock);
69 node->next = ptr[index].next;
70 ptr[index].next = node;
72 pthread_mutex_unlock(&pflookup.lock);
76 // Search for an address for a given oid
77 void *prehashSearch(unsigned int key) {
79 prehashlistnode_t *ptr, *node;
81 pthread_mutex_lock(&pflookup.lock);
83 index = prehashFunction(key);
86 if(node->key == key) {
87 pthread_mutex_unlock(&pflookup.lock);
92 pthread_mutex_unlock(&pflookup.lock);
96 unsigned int prehashRemove(unsigned int key) {
98 prehashlistnode_t *curr, *prev;
99 prehashlistnode_t *ptr, *node;
101 pthread_mutex_lock(&pflookup.lock);
102 ptr = pflookup.table;
103 index = prehashFunction(key);
106 for (; curr != NULL; curr = curr->next) {
107 if (curr->key == key) { // Find a match in the hash table
108 pflookup.numelements--; // Decrement the number of elements in the global hashtable
109 if ((curr == &ptr[index]) && (curr->next == NULL)) { // Delete the first item inside the hashtable with no linked list of prehashlistnode_t
112 } else if ((curr == &ptr[index]) && (curr->next != NULL)) { //Delete the first item with a linked list of prehashlistnode_t connected
113 curr->key = curr->next->key;
114 curr->val = curr->next->val;
116 curr->next = curr->next->next;
118 } else { // Regular delete from linked listed
119 prev->next = curr->next;
122 pthread_mutex_unlock(&pflookup.lock);
127 pthread_mutex_unlock(&pflookup.lock);
131 unsigned int prehashResize(unsigned int newsize) {
132 prehashlistnode_t *node, *ptr, *curr, *next; // curr and next keep track of the current and the next chashlistnodes in a linked list
133 unsigned int oldsize;
134 int isfirst; // Keeps track of the first element in the prehashlistnode_t for each bin in hashtable
136 prehashlistnode_t *newnode;
138 ptr = pflookup.table;
139 oldsize = pflookup.size;
141 if((node = calloc(newsize, sizeof(prehashlistnode_t))) == NULL) {
142 printf("Calloc error %s %d\n", __FILE__, __LINE__);
146 pflookup.table = node; //Update the global hashtable upon resize()
147 pflookup.size = newsize;
148 pflookup.numelements = 0;
150 for(i = 0; i < oldsize; i++) { //Outer loop for each bin in hash table
153 while (curr != NULL) { //Inner loop to go through linked lists
154 if (curr->key == 0) { //Exit inner loop if there the first element for a given bin/index is NULL
155 break; //key = val =0 for element if not present within the hash table
158 index = prehashFunction(curr->key);
159 // Insert into the new table
160 if(pflookup.table[index].next == NULL && pflookup.table[index].key == 0) {
161 pflookup.table[index].key = curr->key;
162 pflookup.table[index].val = curr->val;
163 pflookup.numelements++;
165 if((newnode = calloc(1, sizeof(prehashlistnode_t))) == NULL) {
166 printf("Calloc error %s, %d\n", __FILE__, __LINE__);
169 newnode->key = curr->key;
170 newnode->val = curr->val;
171 newnode->next = pflookup.table[index].next;
172 pflookup.table[index].next = newnode;
173 pflookup.numelements++;
176 //free the linked list of prehashlistnode_t if not the first element in the hash table
186 free(ptr); //Free the memory of the old hash table
190 //Note: This is based on the implementation of the inserting a key in the first position of the hashtable
191 void prehashClear() {
193 prehashlistnode_t *ptr, *prev, *curr;
195 pthread_mutex_lock(&pflookup.lock);
196 ptr = pflookup.table;
197 for(i = 0; i < pflookup.size; i++) {
200 while(prev->next != NULL) {
203 prev->next = curr->next;
206 if(isFirstBin == 1) {
211 pthread_mutex_unlock(&pflookup.lock);