3 #define likely(x) __builtin_expect((x),1)
4 #define unlikely(x) __builtin_expect((x),0)
6 //Smallest Object Size with 1 ptr is 32bytes on on 64-bit machine and 24bytes on 32-bit machine
13 __thread dchashlistnode_t *dc_c_table = NULL;
14 __thread dchashlistnode_t *dc_c_list = NULL;
15 __thread dcliststruct_t *dc_c_structs= NULL;
16 __thread unsigned int dc_c_size;
17 __thread unsigned INTPTR dc_c_mask;
18 __thread unsigned int dc_c_numelements;
19 __thread unsigned int dc_c_threshold;
20 __thread double dc_c_loadfactor;
22 void hashRCRCreate(unsigned int size, double loadfactor) {
23 // Allocate space for the hash table
25 dc_c_table = calloc(size, sizeof(dchashlistnode_t));
26 dc_c_loadfactor = loadfactor;
28 dc_c_threshold=size*loadfactor;
29 dc_c_mask = (size << SHIFTBITS)-1;
30 dc_c_structs=calloc(1, sizeof(dcliststruct_t));
31 dc_c_numelements = 0; // Initial number of elements in the hash
36 if(dc_c_table == NULL) {
37 hashRCRCreate(128, 0.75);
40 dchashlistnode_t *ptr = dc_c_table;
42 if (dc_c_numelements<(dc_c_size>>SHIFTBITS)) {
43 dchashlistnode_t *top=&ptr[dc_c_size];
44 dchashlistnode_t *tmpptr=dc_c_list;
46 dchashlistnode_t *next=tmpptr->lnext;
47 if (tmpptr>=ptr&&tmpptr<top) {
55 bzero(dc_c_table, sizeof(dchashlistnode_t)*dc_c_size);
57 while(dc_c_structs->next!=NULL) {
58 dcliststruct_t *next=dc_c_structs->next;
62 dc_c_structs->num = 0;
67 //Store objects and their pointers into hash
69 //0 = object already exists / Add failed
70 int hashRCRInsert(void * key) {
71 dchashlistnode_t *ptr;
73 if (unlikely(key==NULL)) {
77 if(unlikely(dc_c_numelements > dc_c_threshold)) {
79 unsigned int newsize = dc_c_size << 1;
80 hashRCRResize(newsize);
82 ptr = &dc_c_table[(((unsigned INTPTR)key)&dc_c_mask)>>SHIFTBITS];
83 if(likely(ptr->key==0)) {
88 } else { // Insert in the beginning of linked list
89 dchashlistnode_t * node;
90 dchashlistnode_t *search=ptr;
92 //make sure it isn't here
94 if(search->key == key) {
98 } while(search != NULL);
101 if (dc_c_structs->num<NUMDCLIST) {
102 node=&dc_c_structs->array[dc_c_structs->num];
106 dcliststruct_t *tcl=calloc(1,sizeof(dcliststruct_t));
107 tcl->next=dc_c_structs;
113 node->next = ptr->next;
115 node->lnext=dc_c_list;
123 unsigned int hashRCRResize(unsigned int newsize) {
124 dchashlistnode_t *node, *ptr, *curr; // curr and next keep track of the current and the next chashlistnodes in a linked list
125 unsigned int oldsize;
126 int isfirst; // Keeps track of the first element in the chashlistnode_t for each bin in hashtable
127 unsigned int i,index;
134 if((node = calloc(newsize, sizeof(dchashlistnode_t))) == NULL) {
135 printf("Calloc error %s %d\n", __FILE__, __LINE__);
139 dc_c_table = node; //Update the global hashtable upon resize()
141 dc_c_threshold = newsize * dc_c_loadfactor;
142 mask=dc_c_mask = (newsize << SHIFTBITS)-1;
144 for(i = 0; i < oldsize; i++) { //Outer loop for each bin in hash table
147 do { //Inner loop to go through linked lists
149 dchashlistnode_t *tmp,*next;
151 if ((key=curr->key) == 0) { //Exit inner loop if there the first element is 0
152 break; //key = val =0 for element if not present within the hash table
155 index = (((unsigned INTPTR)key) & mask) >>SHIFTBITS;
158 // Insert into the new table
161 tmp->lnext=dc_c_list;
164 NOTE: Add this case if you change this...
165 This case currently never happens because of the way things rehash....
167 chashlistnode_t *newnode= calloc(1, sizeof(chashlistnode_t));
168 newnode->key = curr->key;
169 newnode->val = curr->val;
170 newnode->next = tmp->next;
174 curr->next=tmp->next;
176 curr->lnext=dc_c_list;
185 free(ptr); //Free the memory of the old hash table
189 //Delete the entire hash table
190 void hashRCRDelete() {
191 dcliststruct_t *ptr=dc_c_structs;
193 dcliststruct_t *next=ptr->next;
203 // Search for an address for a given Address
204 INLINE int hashRCRSearch(void * key) {
205 //REMOVE HASH FUNCTION CALL TO MAKE SURE IT IS INLINED HERE
206 dchashlistnode_t *node = &dc_c_table[(((unsigned INTPTR)key) & dc_c_mask)>>SHIFTBITS];
209 if(node->key == key) {
213 } while(node != NULL);