1 /* LOCK THE ENTIRE HASH TABLE */
3 extern objstr_t *prefetchcache;
5 prehashtable_t pflookup; //Global prefetch cache table
7 unsigned int prehashCreate(unsigned int size, float loadfactor) {
8 prehashlistnode_t *nodes;
11 // Allocate space for the hash table
12 if((nodes = calloc(size, sizeof(prehashlistnode_t))) == NULL) {
13 printf("Calloc error %s %d\n", __FILE__, __LINE__);
18 pflookup.table = nodes;
20 pflookup.numelements = 0; // Initial number of elements in the hash
21 pflookup.loadfactor = loadfactor;
23 //Intiliaze and set prefetch table mutex attribute
24 pthread_mutexattr_init(&pflookup.prefetchmutexattr);
25 //NOTE:PTHREAD_MUTEX_RECURSIVE is currently inside a #if_def UNIX98 in the pthread.h file
26 //Therefore use PTHREAD_MUTEX_RECURSIVE_NP instead
27 pthread_mutexattr_settype(&pflookup.prefetchmutexattr, PTHREAD_MUTEX_RECURSIVE_NP);
29 //Initialize mutex var
30 pthread_mutex_init(&pflookup.lock, &pflookup.prefetchmutexattr);
31 //pthread_mutex_init(&pflookup.lock, NULL);
32 pthread_cond_init(&pflookup.cond, NULL);
36 //Assign keys to bins inside hash table
37 unsigned int prehashFunction(unsigned int key) {
38 return ( key % (pflookup.size));
41 //Store oids and their pointers into hash
42 unsigned int prehashInsert(unsigned int key, void *val) {
45 prehashlistnode_t *ptr, *node;
47 if(pflookup.numelements > (pflookup.loadfactor * pflookup.size)) {
49 newsize = 2 * pflookup.size + 1;
50 pthread_mutex_lock(&pflookup.lock);
51 prehashResize(newsize);
52 pthread_mutex_unlock(&pflookup.lock);
56 pflookup.numelements++;
58 pthread_mutex_lock(&pflookup.lock);
59 index = prehashFunction(key);
60 if(ptr[index].next == NULL && ptr[index].key == 0) { // Insert at the first position in the hashtable
63 } else { // Insert in the beginning of linked list
64 if ((node = calloc(1, sizeof(prehashlistnode_t))) == NULL) {
65 printf("Calloc error %s, %d\n", __FILE__, __LINE__);
66 pthread_mutex_unlock(&pflookup.lock);
71 node->next = ptr[index].next;
72 ptr[index].next = node;
74 pthread_mutex_unlock(&pflookup.lock);
78 // Search for an address for a given oid
79 void *prehashSearch(unsigned int key) {
81 prehashlistnode_t *ptr, *node;
83 pthread_mutex_lock(&pflookup.lock);
85 index = prehashFunction(key);
88 if(node->key == key) {
89 pthread_mutex_unlock(&pflookup.lock);
94 pthread_mutex_unlock(&pflookup.lock);
98 unsigned int prehashRemove(unsigned int key) {
100 prehashlistnode_t *curr, *prev;
101 prehashlistnode_t *ptr, *node;
103 pthread_mutex_lock(&pflookup.lock);
104 ptr = pflookup.table;
105 index = prehashFunction(key);
108 for (; curr != NULL; curr = curr->next) {
109 if (curr->key == key) { // Find a match in the hash table
110 pflookup.numelements--; // Decrement the number of elements in the global hashtable
111 if ((curr == &ptr[index]) && (curr->next == NULL)) { // Delete the first item inside the hashtable with no linked list of prehashlistnode_t
114 } else if ((curr == &ptr[index]) && (curr->next != NULL)) { //Delete the first item with a linked list of prehashlistnode_t connected
115 curr->key = curr->next->key;
116 curr->val = curr->next->val;
118 curr->next = curr->next->next;
120 } else { // Regular delete from linked listed
121 prev->next = curr->next;
124 pthread_mutex_unlock(&pflookup.lock);
129 pthread_mutex_unlock(&pflookup.lock);
133 unsigned int prehashResize(unsigned int newsize) {
134 prehashlistnode_t *node, *ptr, *curr, *next; // curr and next keep track of the current and the next chashlistnodes in a linked list
135 unsigned int oldsize;
136 int isfirst; // Keeps track of the first element in the prehashlistnode_t for each bin in hashtable
138 prehashlistnode_t *newnode;
140 ptr = pflookup.table;
141 oldsize = pflookup.size;
143 if((node = calloc(newsize, sizeof(prehashlistnode_t))) == NULL) {
144 printf("Calloc error %s %d\n", __FILE__, __LINE__);
148 pflookup.table = node; //Update the global hashtable upon resize()
149 pflookup.size = newsize;
150 pflookup.numelements = 0;
152 for(i = 0; i < oldsize; i++) { //Outer loop for each bin in hash table
155 while (curr != NULL) { //Inner loop to go through linked lists
156 if (curr->key == 0) { //Exit inner loop if there the first element for a given bin/index is NULL
157 break; //key = val =0 for element if not present within the hash table
160 index = prehashFunction(curr->key);
161 // Insert into the new table
162 if(pflookup.table[index].next == NULL && pflookup.table[index].key == 0) {
163 pflookup.table[index].key = curr->key;
164 pflookup.table[index].val = curr->val;
165 pflookup.numelements++;
167 if((newnode = calloc(1, sizeof(prehashlistnode_t))) == NULL) {
168 printf("Calloc error %s, %d\n", __FILE__, __LINE__);
171 newnode->key = curr->key;
172 newnode->val = curr->val;
173 newnode->next = pflookup.table[index].next;
174 pflookup.table[index].next = newnode;
175 pflookup.numelements++;
178 //free the linked list of prehashlistnode_t if not the first element in the hash table
188 free(ptr); //Free the memory of the old hash table
192 //Note: This is based on the implementation of the inserting a key in the first position of the hashtable
193 void prehashClear() {
196 prehashlistnode_t *ptr, *prev, *curr;
198 objstr_t *oldcache=prefetchcache;
199 prefetchcache=objstrCreate(prefetchcache->size);
201 pthread_mutex_lock(&pflookup.lock);
202 ptr = pflookup.table;
203 for(i = 0; i < pflookup.size; i++) {
206 while(prev->next != NULL) {
209 prev->next = curr->next;
212 if(isFirstBin == 1) {
217 pthread_mutex_unlock(&pflookup.lock);
219 if (pflookup.hack2!=NULL) {
220 objstrDelete(pflookup.hack2);
222 pflookup.hack2=pflookup.hack;
223 pflookup.hack=oldcache;