1 /************************************************************************************************
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
8 Uses pthreads. compile using -lpthread option
9 ***************************************************************************************************/
14 extern unsigned int *hostIpAddrs;
15 extern unsigned int oidsPerBlock;
17 unsigned int lhashCreate(unsigned int size, float loadfactor)
22 unsigned int lhashInsert(unsigned int oid, unsigned int mid)
27 unsigned int lhashSearch(unsigned int oid)
29 if (oidsPerBlock == 0)
30 return hostIpAddrs[0];
32 return hostIpAddrs[oid / oidsPerBlock];
35 unsigned int lhashRemove(unsigned int oid)
42 lhashtable_t llookup; //Global Hash table
44 // Creates a hash table with size and an array of lhashlistnode_t
45 unsigned int lhashCreate(unsigned int size, float loadfactor) {
46 lhashlistnode_t *nodes;
49 // Allocate space for the hash table
50 if((nodes = calloc(size, sizeof(lhashlistnode_t))) == NULL) {
51 printf("Calloc error %s %d\n", __FILE__, __LINE__);
55 llookup.table = nodes;
57 llookup.numelements = 0; // Initial number of elements in the hash
58 llookup.loadfactor = loadfactor;
59 //Initialize the pthread_mutex variable
60 pthread_mutex_init(&llookup.locktable, NULL);
64 // Assign to oids to bins inside hash table
65 unsigned int lhashFunction(unsigned int oid) {
66 return( oid % (llookup.size));
69 // Insert oid and mid mapping into the hash table
70 unsigned int lhashInsert(unsigned int oid, unsigned int mid) {
73 lhashlistnode_t *ptr, *node;
75 if (llookup.numelements > (llookup.loadfactor * llookup.size)) {
77 newsize = 2 * llookup.size + 1;
78 pthread_mutex_lock(&llookup.locktable);
80 pthread_mutex_unlock(&llookup.locktable);
84 llookup.numelements++;
86 index = lhashFunction(oid);
88 printf("DEBUG(insert) oid = %d, mid =%d, index =%d\n",oid,mid, index);
90 pthread_mutex_lock(&llookup.locktable);
91 if(ptr[index].next == NULL && ptr[index].oid == 0) { // Insert at the first position in the hashtable
94 } else { // Insert in the linked list
95 if ((node = calloc(1, sizeof(lhashlistnode_t))) == NULL) {
96 printf("Calloc error %s, %d\n", __FILE__, __LINE__);
97 pthread_mutex_unlock(&llookup.locktable);
102 node->next = ptr[index].next;
103 ptr[index].next = node;
106 pthread_mutex_unlock(&llookup.locktable);
110 // Return mid for a given oid in the hash table
111 unsigned int lhashSearch(unsigned int oid) {
113 lhashlistnode_t *ptr, *node;
115 ptr = llookup.table; // Address of the beginning of hash table
116 index = lhashFunction(oid);
118 pthread_mutex_lock(&llookup.locktable);
119 while(node != NULL) {
120 if(node->oid == oid) {
121 pthread_mutex_unlock(&llookup.locktable);
126 pthread_mutex_unlock(&llookup.locktable);
130 // Remove an entry from the hash table
131 unsigned int lhashRemove(unsigned int oid) {
133 lhashlistnode_t *curr, *prev;
134 lhashlistnode_t *ptr, *node;
137 index = lhashFunction(oid);
140 pthread_mutex_lock(&llookup.locktable);
141 for (; curr != NULL; curr = curr->next) {
142 if (curr->oid == oid) { // Find a match in the hash table
143 llookup.numelements--; // Decrement the number of elements in the global hashtable
144 if ((curr == &ptr[index]) && (curr->next == NULL)) { // Delete the first item inside the hashtable with no linked list of lhashlistnode_t
147 } else if ((curr == &ptr[index]) && (curr->next != NULL)) { //Delete the first item with a linked list of lhashlistnode_t connected
148 curr->oid = curr->next->oid;
149 curr->mid = curr->next->mid;
151 curr->next = curr->next->next;
153 } else { // Regular delete from linked listed
154 prev->next = curr->next;
157 pthread_mutex_unlock(&llookup.locktable);
162 pthread_mutex_unlock(&llookup.locktable);
167 unsigned int lhashResize(unsigned int newsize) {
168 lhashlistnode_t *node, *ptr, *curr, *next; // curr and next keep track of the current and the next lhashlistnodes in a linked list
169 unsigned int oldsize;
170 int isfirst; // Keeps track of the first element in the lhashlistnode_t for each bin in hashtable
172 lhashlistnode_t *newnode;
175 oldsize = llookup.size;
177 if((node = calloc(newsize, sizeof(lhashlistnode_t))) == NULL) {
178 printf("Calloc error %s %d\n", __FILE__, __LINE__);
182 llookup.table = node; //Update the global hashtable upon resize()
183 llookup.size = newsize;
184 llookup.numelements = 0;
186 for(i = 0; i < oldsize; i++) { //Outer loop for each bin in hash table
189 while (curr != NULL) { //Inner loop to go through linked lists
190 if (curr->oid == 0) { //Exit inner loop if there the first element for a given bin/index is NULL
191 break; //oid = mid =0 for element if not present within the hash table
194 index = lhashFunction(curr->oid);
195 // Insert into the new table
196 if(llookup.table[index].next == NULL && llookup.table[index].oid == 0) {
197 llookup.table[index].oid = curr->oid;
198 llookup.table[index].mid = curr->mid;
199 llookup.numelements++;
201 if((newnode = calloc(1, sizeof(lhashlistnode_t))) == NULL) {
202 printf("Calloc error %s, %d\n", __FILE__, __LINE__);
205 newnode->oid = curr->oid;
206 newnode->mid = curr->mid;
207 newnode->next = llookup.table[index].next;
208 llookup.table[index].next = newnode;
209 llookup.numelements++;
212 //free the linked list of lhashlistnode_t if not the first element in the hash table
223 free(ptr); //Free the memory of the old hash table