add more comments
[IRC.git] / Robust / src / Runtime / DSTM / interface / llookup.c
1 /************************************************************************************************
2   IMP NOTE:
3    All llookup hash function prototypes returns 0 on sucess and 1 otherwise
4    llookup hash is an array of lhashlistnode_t
5    oid = mid = 0 in a given lhashlistnode_t for each bin in the hash table ONLY if the entry is empty =>
6    the OID's can be any unsigned int except 0
7
8    Uses pthreads. compile using -lpthread option
9 ***************************************************************************************************/
10 #include "llookup.h"
11
12 lhashtable_t llookup;           //Global Hash table
13
14 // Creates a hash table with size and an array of lhashlistnode_t 
15 unsigned int lhashCreate(unsigned int size, float loadfactor) {
16         lhashlistnode_t *nodes;
17         int i;
18
19         // Allocate space for the hash table 
20         if((nodes = calloc(size, sizeof(lhashlistnode_t))) == NULL) {
21                 printf("Calloc error %s %d\n", __FILE__, __LINE__);
22                 return 1;
23         }
24         
25         llookup.table = nodes;
26         llookup.size = size;
27         llookup.numelements = 0; // Initial number of elements in the hash
28         llookup.loadfactor = loadfactor;
29         //Initialize the pthread_mutex variable         
30         pthread_mutex_init(&llookup.locktable, NULL);
31         return 0;
32 }
33
34 // Assign to oids to bins inside hash table
35 unsigned int lhashFunction(unsigned int oid) {
36         return( oid % (llookup.size));
37 }
38
39 // Insert oid and mid mapping into the hash table
40 unsigned int lhashInsert(unsigned int oid, unsigned int mid) {
41         unsigned int newsize;
42         int index;
43         lhashlistnode_t *ptr, *node;
44         
45         if (llookup.numelements > (llookup.loadfactor * llookup.size)) {
46                 //Resize Table
47                 newsize = 2 * llookup.size + 1;         
48                 pthread_mutex_lock(&llookup.locktable);
49                 lhashResize(newsize);
50                 pthread_mutex_unlock(&llookup.locktable);
51         }
52         
53         ptr = llookup.table;
54         llookup.numelements++;
55         
56         index = lhashFunction(oid);
57 #ifdef DEBUG
58         printf("DEBUG(insert) oid = %d, mid =%d, index =%d\n",oid,mid, index);
59 #endif
60         pthread_mutex_lock(&llookup.locktable);
61         if(ptr[index].next == NULL && ptr[index].oid == 0) {    // Insert at the first position in the hashtable
62                 ptr[index].oid = oid;
63                 ptr[index].mid = mid;
64         } else {                        // Insert in the linked list
65                 if ((node = calloc(1, sizeof(lhashlistnode_t))) == NULL) {
66                         printf("Calloc error %s, %d\n", __FILE__, __LINE__);
67                         pthread_mutex_unlock(&llookup.locktable);
68                         return 1;
69                 }
70                 node->oid = oid;
71                 node->mid = mid;
72                 node->next = ptr[index].next;
73                 ptr[index].next = node;
74         }
75         
76         pthread_mutex_unlock(&llookup.locktable);
77         return 0;
78 }
79
80 // Return mid for a given oid in the hash table
81 unsigned int lhashSearch(unsigned int oid) {
82         int index;
83         lhashlistnode_t *ptr, *node;
84
85         ptr = llookup.table;    // Address of the beginning of hash table       
86         index = lhashFunction(oid);
87         node = &ptr[index];
88         pthread_mutex_lock(&llookup.locktable);
89         while(node != NULL) {
90                 if(node->oid == oid) {
91                         pthread_mutex_unlock(&llookup.locktable);
92                         return node->mid;
93                 }
94                 node = node->next;
95         }
96         pthread_mutex_unlock(&llookup.locktable);
97         return 0;
98 }
99
100 // Remove an entry from the hash table
101 unsigned int lhashRemove(unsigned int oid) {
102         int index;
103         lhashlistnode_t *curr, *prev;
104         lhashlistnode_t *ptr, *node;
105         
106         ptr = llookup.table;
107         index = lhashFunction(oid);
108         curr = &ptr[index];
109         
110         pthread_mutex_lock(&llookup.locktable);
111         for (; curr != NULL; curr = curr->next) {
112                 if (curr->oid == oid) {         // Find a match in the hash table
113                         llookup.numelements--;  // Decrement the number of elements in the global hashtable
114                         if ((curr == &ptr[index]) && (curr->next == NULL))  { // Delete the first item inside the hashtable with no linked list of lhashlistnode_t 
115                                 curr->oid = 0;
116                                 curr->mid = 0;
117                         } else if ((curr == &ptr[index]) && (curr->next != NULL)) { //Delete the first item with a linked list of lhashlistnode_t  connected 
118                                 curr->oid = curr->next->oid;
119                                 curr->mid = curr->next->mid;
120                                 node = curr->next;
121                                 curr->next = curr->next->next;
122                                 free(node);
123                         } else {                                                // Regular delete from linked listed    
124                                 prev->next = curr->next;
125                                 free(curr);
126                         }
127                         pthread_mutex_unlock(&llookup.locktable);
128                         return 0;
129                 }       
130                 prev = curr; 
131         }
132         pthread_mutex_unlock(&llookup.locktable);
133         return 1;
134 }
135
136 // Resize table
137 unsigned int lhashResize(unsigned int newsize) {
138         lhashlistnode_t *node, *ptr, *curr, *next;      // curr and next keep track of the current and the next lhashlistnodes in a linked list
139         unsigned int oldsize;
140         int isfirst;    // Keeps track of the first element in the lhashlistnode_t for each bin in hashtable
141         int i,index;    
142         lhashlistnode_t *newnode;               
143         
144         ptr = llookup.table;
145         oldsize = llookup.size;
146         
147         if((node = calloc(newsize, sizeof(lhashlistnode_t))) == NULL) {
148                 printf("Calloc error %s %d\n", __FILE__, __LINE__);
149                 return 1;
150         }
151
152         llookup.table = node;           //Update the global hashtable upon resize()
153         llookup.size = newsize;
154         llookup.numelements = 0;
155
156         for(i = 0; i < oldsize; i++) {                  //Outer loop for each bin in hash table
157                 curr = &ptr[i];
158                 isfirst = 1;                    
159                 while (curr != NULL) {                  //Inner loop to go through linked lists
160                         if (curr->oid == 0) {           //Exit inner loop if there the first element for a given bin/index is NULL
161                                 break;                  //oid = mid =0 for element if not present within the hash table
162                         }
163                         next = curr->next;
164                         index = lhashFunction(curr->oid);
165                         // Insert into the new table
166                         if(llookup.table[index].next == NULL && llookup.table[index].oid == 0) {
167                                 llookup.table[index].oid = curr->oid;
168                                 llookup.table[index].mid = curr->mid;
169                                 llookup.numelements++;
170                         }else {
171                                 if((newnode = calloc(1, sizeof(lhashlistnode_t))) == NULL) {
172                                         printf("Calloc error %s, %d\n", __FILE__, __LINE__);
173                                         return 1;
174                                 }
175                                 newnode->oid = curr->oid;
176                                 newnode->mid = curr->mid;
177                                 newnode->next = llookup.table[index].next;
178                                 llookup.table[index].next = newnode;    
179                                 llookup.numelements++;
180                         }
181                         
182                         //free the linked list of lhashlistnode_t if not the first element in the hash table
183                         if (isfirst != 1) {
184                                 free(curr);
185                         } 
186                         
187                         isfirst = 0;
188                         curr = next;
189
190                 }
191         }
192
193         free(ptr);              //Free the memory of the old hash table 
194         return 0;
195 }