start of new file
[IRC.git] / Robust / src / Runtime / DSTM / interface / clookup.c
1 #include "clookup.h"
2 #define INLINE    inline __attribute__((always_inline))
3
4 chashtable_t *chashCreate(unsigned int size, float loadfactor) {
5   chashtable_t *ctable;
6   chashlistnode_t *nodes; 
7   int i; 
8   
9   if((ctable = calloc(1, sizeof(chashtable_t))) == NULL) {
10     printf("Calloc error %s %d\n", __FILE__, __LINE__);
11     return NULL;
12   }     
13   
14   // Allocate space for the hash table 
15   if((nodes = calloc(size, sizeof(chashlistnode_t))) == NULL) { 
16     printf("Calloc error %s %d\n", __FILE__, __LINE__);
17     free(ctable);
18     return NULL;
19   }       
20   
21   ctable->table = nodes;
22   ctable->size = size; 
23   ctable->mask = (size << 1)-1;
24   ctable->numelements = 0; // Initial number of elements in the hash
25   ctable->loadfactor = loadfactor;
26   
27   return ctable;
28 }
29
30 //Finds the right bin in the hash table
31 static INLINE unsigned int chashFunction(chashtable_t *table, unsigned int key) {
32   return ( key & (table->mask))>>1;//throw away low order bit
33 }
34
35 //Store objects and their pointers into hash
36 unsigned int chashInsert(chashtable_t *table, unsigned int key, void *val) {
37   unsigned int newsize;
38   int index;
39   chashlistnode_t *ptr, *node;
40   
41   if(table->numelements > (table->loadfactor * table->size)) {
42     //Resize
43     newsize = table->size << 1;
44     chashResize(table,newsize);
45   }
46
47   ptr = table->table;
48   table->numelements++;
49   index = chashFunction(table, key);
50 #ifdef DEBUG
51   printf("chashInsert(): DEBUG -> index = %d, key = %d, val = %x\n", index, key, val);
52 #endif
53   if(ptr[index].next == NULL && ptr[index].key == 0) {  // Insert at the first position in the hashtable
54     ptr[index].key = key;
55     ptr[index].val = val;
56   } else { // Insert in the beginning of linked list
57     if ((node = calloc(1, sizeof(chashlistnode_t))) == NULL) {
58       printf("Calloc error %s, %d\n", __FILE__, __LINE__);
59       return 1;
60     }
61     node->key = key;
62     node->val = val;
63     node->next = ptr[index].next;
64     ptr[index].next = node;
65   }
66   return 0;
67 }
68
69 // Search for an address for a given oid
70 INLINE void * chashSearch(chashtable_t *table, unsigned int key) {
71   //REMOVE HASH FUNCTION CALL TO MAKE SURE IT IS INLINED HERE
72   chashlistnode_t *node = &table->table[(key & table->mask)>>1];
73   
74   while(node != NULL) {
75     if(node->key == key) {
76       return node->val;
77     }
78     node = node->next;
79   }
80   return NULL;
81 }
82
83 unsigned int chashRemove(chashtable_t *table, unsigned int key) {
84   int index;
85   chashlistnode_t *curr, *prev;
86   chashlistnode_t *ptr, *node;
87   
88   ptr = table->table;
89   index = chashFunction(table,key);
90   curr = &ptr[index];
91
92   for (; curr != NULL; curr = curr->next) {
93     if (curr->key == key) {         // Find a match in the hash table
94       table->numelements--;  // Decrement the number of elements in the global hashtable
95       if ((curr == &ptr[index]) && (curr->next == NULL))  { // Delete the first item inside the hashtable with no linked list of chashlistnode_t 
96         curr->key = 0;
97         curr->val = NULL;
98       } else if ((curr == &ptr[index]) && (curr->next != NULL)) { //Delete the first item with a linked list of chashlistnode_t  connected 
99         curr->key = curr->next->key;
100         curr->val = curr->next->val;
101         node = curr->next;
102         curr->next = curr->next->next;
103         free(node);
104       } else {                                          // Regular delete from linked listed    
105         prev->next = curr->next;
106         free(curr);
107       }
108       return 0;
109     }       
110     prev = curr; 
111   }
112   return 1;
113 }
114
115 unsigned int chashResize(chashtable_t *table, unsigned int newsize) {
116   chashlistnode_t *node, *ptr, *curr, *next;    // curr and next keep track of the current and the next chashlistnodes in a linked list
117   unsigned int oldsize;
118   int isfirst;    // Keeps track of the first element in the chashlistnode_t for each bin in hashtable
119   int i,index;          
120   chashlistnode_t *newnode;             
121   
122   ptr = table->table;
123   oldsize = table->size;
124   
125   if((node = calloc(newsize, sizeof(chashlistnode_t))) == NULL) {
126     printf("Calloc error %s %d\n", __FILE__, __LINE__);
127     return 1;
128   }
129   
130   table->table = node;          //Update the global hashtable upon resize()
131   table->size = newsize;
132   table->mask = (newsize << 1)-1;
133   table->numelements = 0;
134   
135   for(i = 0; i < oldsize; i++) {                        //Outer loop for each bin in hash table
136     curr = &ptr[i];
137     isfirst = 1;                        
138     while (curr != NULL) {                      //Inner loop to go through linked lists
139       if (curr->key == 0) {             //Exit inner loop if there the first element for a given bin/index is NULL
140         break;                  //key = val =0 for element if not present within the hash table
141       }
142       next = curr->next;
143       
144       index = chashFunction(table, curr->key);
145 #ifdef DEBUG
146       printf("DEBUG(resize) -> index = %d, key = %d, val = %x\n", index, curr->key, curr->val);
147 #endif
148       // Insert into the new table
149       if(table->table[index].next == NULL && table->table[index].key == 0) { 
150         table->table[index].key = curr->key;
151         table->table[index].val = curr->val;
152         table->numelements++;
153       }else { 
154         if((newnode = calloc(1, sizeof(chashlistnode_t))) == NULL) { 
155           printf("Calloc error %s, %d\n", __FILE__, __LINE__);
156           return 1;
157         }       
158         newnode->key = curr->key;
159         newnode->val = curr->val;
160         newnode->next = table->table[index].next;
161         table->table[index].next = newnode;    
162         table->numelements++;
163       }       
164       
165       //free the linked list of chashlistnode_t if not the first element in the hash table
166       if (isfirst != 1) {
167         free(curr);
168       } 
169       
170       isfirst = 0;
171       curr = next;
172     }
173   }
174   
175   free(ptr);            //Free the memory of the old hash table 
176   return 0;
177 }
178
179 //Delete the entire hash table
180 void chashDelete(chashtable_t *ctable) {
181   int i, isFirst;
182   chashlistnode_t *ptr, *curr, *next;
183   ptr = ctable->table;
184   
185   for(i=0 ; i<ctable->size ; i++) {
186     curr = &ptr[i];
187     isFirst = 1 ;
188     while(curr  != NULL) {
189       next = curr->next;
190       if(isFirst != 1) {
191         free(curr);
192       }
193       isFirst = 0;
194       curr = next;
195     }
196   }
197   
198   free(ptr);
199   ptr = NULL;
200   free(ctable);
201   ctable = NULL;
202 }