start of new file
[IRC.git] / Robust / src / Runtime / DSTM / interface / mlookup.c
index 8bc046dc90428338f64cb35c36de9c7af815a11f..9000b80e69519c6ee4c847501407da2b67d3e099 100644 (file)
@@ -5,8 +5,6 @@ mhashtable_t mlookup;   //Global hash table
 // Creates a machine lookup table with size =" size" 
 unsigned int mhashCreate(unsigned int size, float loadfactor)  {
        mhashlistnode_t *nodes;
-       int i;
-
        // Allocate space for the hash table 
        if((nodes = calloc(size, sizeof(mhashlistnode_t))) == NULL) {
                printf("Calloc error %s %d\n", __FILE__, __LINE__);
@@ -43,11 +41,11 @@ unsigned int mhashInsert(unsigned int key, void *val) {
        ptr = mlookup.table;
        mlookup.numelements++;
        
-       index = mhashFunction(key);
 #ifdef DEBUG
        printf("DEBUG -> index = %d, key = %d, val = %x\n", index, key, val);
 #endif
        pthread_mutex_lock(&mlookup.locktable);
+       index = mhashFunction(key);
        if(ptr[index].next == NULL && ptr[index].key == 0) {    // Insert at the first position in the hashtable
                ptr[index].key = key;
                ptr[index].val = val;
@@ -58,7 +56,7 @@ unsigned int mhashInsert(unsigned int key, void *val) {
                        return 1;
                }
                node->key = key;
-               node->val = val ;
+               node->val = val;
                node->next = ptr[index].next;
                ptr[index].next = node;
        }
@@ -71,10 +69,10 @@ void *mhashSearch(unsigned int key) {
        int index;
        mhashlistnode_t *ptr, *node;
 
+       pthread_mutex_lock(&mlookup.locktable);
        ptr = mlookup.table;    // Address of the beginning of hash table       
        index = mhashFunction(key);
        node = &ptr[index];
-       pthread_mutex_lock(&mlookup.locktable);
        while(node != NULL) {
                if(node->key == key) {
                        pthread_mutex_unlock(&mlookup.locktable);
@@ -92,11 +90,10 @@ unsigned int mhashRemove(unsigned int key) {
        mhashlistnode_t *curr, *prev;
        mhashlistnode_t *ptr, *node;
        
+       pthread_mutex_lock(&mlookup.locktable);
        ptr = mlookup.table;
        index = mhashFunction(key);
        curr = &ptr[index];
-
-       pthread_mutex_lock(&mlookup.locktable);
        for (; curr != NULL; curr = curr->next) {
                if (curr->key == key) {         // Find a match in the hash table
                        mlookup.numelements--;  // Decrement the number of elements in the global hashtable
@@ -187,3 +184,35 @@ unsigned int mhashResize(unsigned int newsize) {
        return 0;
 }
 
+unsigned int *mhashGetKeys(unsigned int *numKeys)
+{
+       unsigned int *keys;
+       int i, keyindex;
+       mhashlistnode_t *curr;
+
+       pthread_mutex_lock(&mlookup.locktable);
+
+       *numKeys = mlookup.numelements;
+       keys = calloc(*numKeys, sizeof(unsigned int));
+
+       keyindex = 0;
+       for (i = 0; i < mlookup.size; i++)
+       {
+               if (mlookup.table[i].key != 0)
+               {
+                       curr = &mlookup.table[i];
+                       while (curr != NULL)
+                       {
+                               keys[keyindex++] = curr->key;
+                               curr = curr->next;
+                       }
+               }
+       }
+
+       if (keyindex != *numKeys)
+               printf("mhashGetKeys(): WARNING: incorrect mlookup.numelements value!\n");
+
+       pthread_mutex_unlock(&mlookup.locktable);
+       return keys;
+}
+