bug fix
[IRC.git] / Robust / src / Runtime / DSTM / interface / llookup.c
index f75b51befb1112b54ae8836ae02db68aaa0843e6..3076097815c200da32930c69a2b2e5425f75939c 100644 (file)
@@ -1,33 +1,64 @@
 /************************************************************************************************
   IMP NOTE:
-  
-   All llookup hash function prototypes returns 0 or NULL when there is an error or else returns 1
+   All llookup hash function prototypes returns 0 on sucess and 1 otherwise
    llookup hash is an array of lhashlistnode_t
    oid = mid = 0 in a given lhashlistnode_t for each bin in the hash table ONLY if the entry is empty =>
    the OID's can be any unsigned int except 0
+
+   Uses pthreads. compile using -lpthread option
 ***************************************************************************************************/
 #include "llookup.h"
 
-extern  lhashtable_t llookup;          //Global Hash table
+#ifdef SIMPLE_LLOOKUP
+
+extern unsigned int *hostIpAddrs;
+extern unsigned int oidsPerBlock;
+
+unsigned int lhashCreate(unsigned int size, float loadfactor)
+{
+       return 0;
+}
 
-// Creates a hash table with default size HASH_SIZE and an array of lhashlistnode_t 
+unsigned int lhashInsert(unsigned int oid, unsigned int mid)
+{
+       return 0;
+}
+
+unsigned int lhashSearch(unsigned int oid)
+{
+       if (oidsPerBlock == 0)
+               return hostIpAddrs[0];
+       else
+               return hostIpAddrs[oid / oidsPerBlock];
+}
+
+unsigned int lhashRemove(unsigned int oid)
+{
+       return 0;
+}
+
+#else
+
+lhashtable_t llookup;          //Global Hash table
+
+// Creates a hash table with size and an array of lhashlistnode_t 
 unsigned int lhashCreate(unsigned int size, float loadfactor) {
        lhashlistnode_t *nodes;
        int i;
 
        // Allocate space for the hash table 
-       if((nodes = calloc(HASH_SIZE, sizeof(lhashlistnode_t))) == NULL) {
+       if((nodes = calloc(size, sizeof(lhashlistnode_t))) == NULL) {
                printf("Calloc error %s %d\n", __FILE__, __LINE__);
-               return 0;
+               return 1;
        }
-       for (i = 0; i < HASH_SIZE; i++) 
-               nodes[i].next = NULL;
        
        llookup.table = nodes;
        llookup.size = size;
        llookup.numelements = 0; // Initial number of elements in the hash
        llookup.loadfactor = loadfactor;
-       return 1;
+       //Initialize the pthread_mutex variable         
+       pthread_mutex_init(&llookup.locktable, NULL);
+       return 0;
 }
 
 // Assign to oids to bins inside hash table
@@ -39,62 +70,74 @@ unsigned int lhashFunction(unsigned int oid) {
 unsigned int lhashInsert(unsigned int oid, unsigned int mid) {
        unsigned int newsize;
        int index;
-       lhashlistnode_t *ptr;
-       lhashlistnode_t *node;
+       lhashlistnode_t *ptr, *node;
        
        if (llookup.numelements > (llookup.loadfactor * llookup.size)) {
                //Resize Table
                newsize = 2 * llookup.size + 1;         
+               pthread_mutex_lock(&llookup.locktable);
                lhashResize(newsize);
+               pthread_mutex_unlock(&llookup.locktable);
        }
+       
        ptr = llookup.table;
        llookup.numelements++;
        
        index = lhashFunction(oid);
+#ifdef DEBUG
+       printf("DEBUG(insert) oid = %d, mid =%d, index =%d\n",oid,mid, index);
+#endif
+       pthread_mutex_lock(&llookup.locktable);
        if(ptr[index].next == NULL && ptr[index].oid == 0) {    // Insert at the first position in the hashtable
                ptr[index].oid = oid;
                ptr[index].mid = mid;
        } else {                        // Insert in the linked list
                if ((node = calloc(1, sizeof(lhashlistnode_t))) == NULL) {
                        printf("Calloc error %s, %d\n", __FILE__, __LINE__);
-                       return 0;
+                       pthread_mutex_unlock(&llookup.locktable);
+                       return 1;
                }
                node->oid = oid;
                node->mid = mid;
                node->next = ptr[index].next;
                ptr[index].next = node;
        }
-       return 1;
+       
+       pthread_mutex_unlock(&llookup.locktable);
+       return 0;
 }
 
-// Search for a value in the hash table
+// Return mid for a given oid in the hash table
 unsigned int lhashSearch(unsigned int oid) {
        int index;
-       lhashlistnode_t *ptr;
-       lhashlistnode_t *tmp;
+       lhashlistnode_t *ptr, *node;
 
        ptr = llookup.table;    // Address of the beginning of hash table       
        index = lhashFunction(oid);
-       tmp = &ptr[index];
-       while(tmp != NULL) {
-               if(tmp->oid == oid) {
-                       return tmp->mid;
+       node = &ptr[index];
+       pthread_mutex_lock(&llookup.locktable);
+       while(node != NULL) {
+               if(node->oid == oid) {
+                       pthread_mutex_unlock(&llookup.locktable);
+                       return node->mid;
                }
-               tmp = tmp->next;
+               node = node->next;
        }
+       pthread_mutex_unlock(&llookup.locktable);
        return 0;
 }
 
 // Remove an entry from the hash table
 unsigned int lhashRemove(unsigned int oid) {
        int index;
-       lhashlistnode_t *curr, *prev, *tmp;
-       lhashlistnode_t *ptr;
+       lhashlistnode_t *curr, *prev;
+       lhashlistnode_t *ptr, *node;
        
        ptr = llookup.table;
        index = lhashFunction(oid);
-       prev = curr = &ptr[index];
-
+       curr = &ptr[index];
+       
+       pthread_mutex_lock(&llookup.locktable);
        for (; curr != NULL; curr = curr->next) {
                if (curr->oid == oid) {         // Find a match in the hash table
                        llookup.numelements--;  // Decrement the number of elements in the global hashtable
@@ -104,54 +147,82 @@ unsigned int lhashRemove(unsigned int oid) {
                        } else if ((curr == &ptr[index]) && (curr->next != NULL)) { //Delete the first item with a linked list of lhashlistnode_t  connected 
                                curr->oid = curr->next->oid;
                                curr->mid = curr->next->mid;
-                               tmp = curr->next;
+                               node = curr->next;
                                curr->next = curr->next->next;
-                               free(tmp);
+                               free(node);
                        } else {                                                // Regular delete from linked listed    
                                prev->next = curr->next;
                                free(curr);
                        }
-                       return 1;
+                       pthread_mutex_unlock(&llookup.locktable);
+                       return 0;
                }       
                prev = curr; 
        }
-       return 0;
+       pthread_mutex_unlock(&llookup.locktable);
+       return 1;
 }
 
 // Resize table
 unsigned int lhashResize(unsigned int newsize) {
-       lhashlistnode_t *node, *curr, *next;    // curr and next keep track of the current and the next lhashlistnodes in a linked list
-       lhashtable_t oldtable;
-       int i, isfirst;    // Keeps track of the first element in the lhashlistnode_t for each bin in hashtable
-
-       oldtable.table = llookup.table; // copy  orginial lhashtable_t type to a new structure
-       oldtable.size = llookup.size;
-       oldtable.numelements = llookup.numelements;
-       oldtable.loadfactor = llookup.loadfactor;
+       lhashlistnode_t *node, *ptr, *curr, *next;      // curr and next keep track of the current and the next lhashlistnodes in a linked list
+       unsigned int oldsize;
+       int isfirst;    // Keeps track of the first element in the lhashlistnode_t for each bin in hashtable
+       int i,index;    
+       lhashlistnode_t *newnode;               
+       
+       ptr = llookup.table;
+       oldsize = llookup.size;
+       
        if((node = calloc(newsize, sizeof(lhashlistnode_t))) == NULL) {
                printf("Calloc error %s %d\n", __FILE__, __LINE__);
-               return 0;
+               return 1;
        }
+
        llookup.table = node;           //Update the global hashtable upon resize()
        llookup.size = newsize;
        llookup.numelements = 0;
 
-       for(i = 0; i < oldtable.size; i++) {
-               curr = next = &oldtable.table[i];
-               isfirst = 1;                            //isfirst = true by default
-               while (curr != NULL) {
+       for(i = 0; i < oldsize; i++) {                  //Outer loop for each bin in hash table
+               curr = &ptr[i];
+               isfirst = 1;                    
+               while (curr != NULL) {                  //Inner loop to go through linked lists
                        if (curr->oid == 0) {           //Exit inner loop if there the first element for a given bin/index is NULL
                                break;                  //oid = mid =0 for element if not present within the hash table
                        }
                        next = curr->next;
-                       lhashInsert(curr->oid, curr->mid);      //Call hashInsert into the new resized table 
+                       index = lhashFunction(curr->oid);
+                       // Insert into the new table
+                       if(llookup.table[index].next == NULL && llookup.table[index].oid == 0) {
+                               llookup.table[index].oid = curr->oid;
+                               llookup.table[index].mid = curr->mid;
+                               llookup.numelements++;
+                       }else {
+                               if((newnode = calloc(1, sizeof(lhashlistnode_t))) == NULL) {
+                                       printf("Calloc error %s, %d\n", __FILE__, __LINE__);
+                                       return 1;
+                               }
+                               newnode->oid = curr->oid;
+                               newnode->mid = curr->mid;
+                               newnode->next = llookup.table[index].next;
+                               llookup.table[index].next = newnode;    
+                               llookup.numelements++;
+                       }
+                       
+                       //free the linked list of lhashlistnode_t if not the first element in the hash table
                        if (isfirst != 1) {
-                               free(curr);             //free the linked list of lhashlistnode_t if not the first element in the hash table
+                               free(curr);
                        } 
-                       isfirst = 0;                    //set isFirst = false inside inner loop
+                       
+                       isfirst = 0;
                        curr = next;
+
                }
        }
-       free(oldtable.table);                           //free the copied hashtable calloc-ed
-       return 1;
+
+       free(ptr);              //Free the memory of the old hash table 
+       return 0;
 }
+
+#endif
+