4 __thread chashlistnode_t *c_table;
5 __thread chashlistnode_t *c_list;
6 __thread unsigned int c_size;
7 __thread unsigned INTPTR c_mask;
8 __thread unsigned int c_numelements;
9 __thread unsigned int c_threshold;
10 __thread double c_loadfactor;
11 __thread cliststruct_t *c_structs;
14 __thread rdchashlistnode_t *rd_c_table;
15 __thread rdchashlistnode_t *rd_c_list;
16 __thread unsigned int rd_c_size;
17 __thread unsigned INTPTR rd_c_mask;
18 __thread unsigned int rd_c_numelements;
19 __thread unsigned int rd_c_threshold;
20 __thread double rd_c_loadfactor;
21 __thread rdcliststruct_t *rd_c_structs;
23 void rd_t_chashCreate(unsigned int size, double loadfactor) {
25 chashlistnode_t *nodes;
28 // Allocate space for the hash table
29 rd_c_table = calloc(size, sizeof(rdchashlistnode_t));
30 rd_c_loadfactor = loadfactor;
32 rd_c_threshold=size*loadfactor;
33 rd_c_mask = (size << 4)-1;
34 rd_c_structs=calloc(1, sizeof(rdcliststruct_t));
35 rd_c_numelements = 0; // Initial number of elements in the hash
39 void rd_t_chashreset() {
40 rdchashlistnode_t *ptr = rd_c_table;
43 if (rd_c_numelements<(rd_c_size>>4)) {
44 rdchashlistnode_t *top=&ptr[rd_c_size];
45 rdchashlistnode_t *tmpptr=rd_c_list;
47 rdchashlistnode_t *next=tmpptr->lnext;
48 if (tmpptr>=ptr&&tmpptr<top) {
56 bzero(rd_c_table, sizeof(rdchashlistnode_t)*rd_c_size);
58 while(rd_c_structs->next!=NULL) {
59 rdcliststruct_t *next=rd_c_structs->next;
63 rd_c_structs->num = 0;
68 //Store objects and their pointers into hash
69 void rd_t_chashInsertOnce(void * key, unsigned int version) {
70 rdchashlistnode_t *ptr;
75 if(rd_c_numelements > (rd_c_threshold)) {
77 unsigned int newsize = rd_c_size << 1;
78 rd_t_chashResize(newsize);
81 ptr = &rd_c_table[(((unsigned INTPTR)key)&rd_c_mask)>>4];
89 } else { // Insert in the beginning of linked list
90 rdchashlistnode_t * node;
91 rdchashlistnode_t *search=ptr;
93 //make sure it isn't here
95 if(search->key == key) {
99 } while(search != NULL);
102 if (rd_c_structs->num<NUMCLIST) {
103 node=&rd_c_structs->array[rd_c_structs->num];
107 rdcliststruct_t *tcl=calloc(1,sizeof(rdcliststruct_t));
108 tcl->next=rd_c_structs;
114 node->version = version;
115 node->next = ptr->next;
117 node->lnext=rd_c_list;
122 unsigned int rd_t_chashResize(unsigned int newsize) {
123 rdchashlistnode_t *node, *ptr, *curr; // curr and next keep track of the current and the next chashlistnodes in a linked list
124 unsigned int oldsize;
125 int isfirst; // Keeps track of the first element in the chashlistnode_t for each bin in hashtable
126 unsigned int i,index;
133 if((node = calloc(newsize, sizeof(rdchashlistnode_t))) == NULL) {
134 printf("Calloc error %s %d\n", __FILE__, __LINE__);
138 rd_c_table = node; //Update the global hashtable upon resize()
140 rd_c_threshold = newsize * rd_c_loadfactor;
141 mask=rd_c_mask = (newsize << 4)-1;
143 for(i = 0; i < oldsize; i++) { //Outer loop for each bin in hash table
146 do { //Inner loop to go through linked lists
148 rdchashlistnode_t *tmp,*next;
150 if ((key=curr->key) == 0) { //Exit inner loop if there the first element is 0
151 break; //key = val =0 for element if not present within the hash table
153 index = (((unsigned INTPTR)key) & mask) >>4;
156 // Insert into the new table
159 tmp->version = curr->version;
160 tmp->lnext=rd_c_list;
163 NOTE: Add this case if you change this...
164 This case currently never happens because of the way things rehash....
166 chashlistnode_t *newnode= calloc(1, sizeof(chashlistnode_t));
167 newnode->key = curr->key;
168 newnode->val = curr->val;
169 newnode->next = tmp->next;
173 curr->next=tmp->next;
175 curr->lnext=rd_c_list;
184 free(ptr); //Free the memory of the old hash table
188 //Delete the entire hash table
189 void rd_t_chashDelete() {
191 rdcliststruct_t *ptr=rd_c_structs;
193 rdcliststruct_t *next=ptr->next;
205 __thread dchashlistnode_t *dc_c_table;
206 __thread dchashlistnode_t *dc_c_list;
207 __thread unsigned int dc_c_size;
208 __thread unsigned INTPTR dc_c_mask;
209 __thread unsigned int dc_c_numelements;
210 __thread unsigned int dc_c_threshold;
211 __thread double dc_c_loadfactor;
212 __thread dcliststruct_t *dc_c_structs;
214 void dc_t_chashCreate(unsigned int size, double loadfactor) {
215 dchashlistnode_t *nodes;
218 // Allocate space for the hash table
220 dc_c_table = calloc(size, sizeof(dchashlistnode_t));
221 dc_c_loadfactor = loadfactor;
223 dc_c_threshold=size*loadfactor;
224 dc_c_mask = (size << 4)-1;
225 dc_c_structs=calloc(1, sizeof(dcliststruct_t));
226 dc_c_numelements = 0; // Initial number of elements in the hash
230 void dc_t_chashreset() {
231 dchashlistnode_t *ptr = dc_c_table;
234 if (dc_c_numelements<(dc_c_size>>4)) {
235 dchashlistnode_t *top=&ptr[dc_c_size];
236 dchashlistnode_t *tmpptr=dc_c_list;
237 while(tmpptr!=NULL) {
238 dchashlistnode_t *next=tmpptr->lnext;
239 if (tmpptr>=ptr&&tmpptr<top) {
247 bzero(dc_c_table, sizeof(dchashlistnode_t)*dc_c_size);
249 while(dc_c_structs->next!=NULL) {
250 dcliststruct_t *next=dc_c_structs->next;
254 dc_c_structs->num = 0;
255 dc_c_numelements = 0;
259 //Store objects and their pointers into hash
260 void dc_t_chashInsertOnce(void * key, void *val) {
261 dchashlistnode_t *ptr;
266 if(dc_c_numelements > (dc_c_threshold)) {
268 unsigned int newsize = dc_c_size << 1;
269 dc_t_chashResize(newsize);
272 ptr = &dc_c_table[(((unsigned INTPTR)key)&dc_c_mask)>>4];
277 ptr->lnext=dc_c_list;
280 } else { // Insert in the beginning of linked list
281 dchashlistnode_t * node;
282 dchashlistnode_t *search=ptr;
284 //make sure it isn't here
286 if(search->key == key) {
290 } while(search != NULL);
293 if (dc_c_structs->num<NUMCLIST) {
294 node=&dc_c_structs->array[dc_c_structs->num];
298 dcliststruct_t *tcl=calloc(1,sizeof(dcliststruct_t));
299 tcl->next=dc_c_structs;
306 node->next = ptr->next;
308 node->lnext=dc_c_list;
314 //Store objects and their pointers into hash
315 void dc_t_chashInsertOnceArray(void * key, unsigned int intkey, void *val) {
316 dchashlistnode_t *ptr;
321 if(dc_c_numelements > (dc_c_threshold)) {
323 unsigned int newsize = dc_c_size << 1;
324 dc_t_chashResize(newsize);
327 ptr = &dc_c_table[(((unsigned INTPTR)key^intkey)&dc_c_mask)>>4];
333 ptr->lnext=dc_c_list;
336 } else { // Insert in the beginning of linked list
337 dchashlistnode_t * node;
338 dchashlistnode_t *search=ptr;
340 //make sure it isn't here
342 if(search->key == key&&search->intkey==intkey) {
346 } while(search != NULL);
349 if (dc_c_structs->num<NUMCLIST) {
350 node=&dc_c_structs->array[dc_c_structs->num];
354 dcliststruct_t *tcl=calloc(1,sizeof(dcliststruct_t));
355 tcl->next=dc_c_structs;
361 node->intkey = intkey;
363 node->next = ptr->next;
365 node->lnext=dc_c_list;
372 //Store objects and their pointers into hash
373 void dc_t_chashInsertOnce(void * key, unsigned int indexkey, void *val) {
374 chashlistnode_t *ptr;
379 if(dc_c_numelements > (dc_c_threshold)) {
381 unsigned int newsize = dc_c_size << 1;
382 dc_t_chashResize(newsize);
385 ptr = &dc_c_table[(((unsigned INTPTR)key)&dc_c_mask)>>4];
390 ptr->lnext=dc_c_list;
393 } else { // Insert in the beginning of linked list
394 chashlistnode_t * node;
395 chashlistnode_t *search=ptr;
397 //make sure it isn't here
399 if(search->key == key) {
403 } while(search != NULL);
406 if (dc_c_structs->num<NUMCLIST) {
407 node=&dc_c_structs->array[dc_c_structs->num];
411 cliststruct_t *tcl=calloc(1,sizeof(cliststruct_t));
412 tcl->next=dc_c_structs;
419 node->next = ptr->next;
421 node->lnext=dc_c_list;
427 unsigned int dc_t_chashResize(unsigned int newsize) {
428 dchashlistnode_t *node, *ptr, *curr; // curr and next keep track of the current and the next chashlistnodes in a linked list
429 unsigned int oldsize;
430 int isfirst; // Keeps track of the first element in the chashlistnode_t for each bin in hashtable
431 unsigned int i,index;
438 if((node = calloc(newsize, sizeof(dchashlistnode_t))) == NULL) {
439 printf("Calloc error %s %d\n", __FILE__, __LINE__);
443 dc_c_table = node; //Update the global hashtable upon resize()
445 dc_c_threshold = newsize * dc_c_loadfactor;
446 mask=dc_c_mask = (newsize << 4)-1;
448 for(i = 0; i < oldsize; i++) { //Outer loop for each bin in hash table
451 do { //Inner loop to go through linked lists
456 dchashlistnode_t *tmp,*next;
458 if ((key=curr->key) == 0) { //Exit inner loop if there the first element is 0
459 break; //key = val =0 for element if not present within the hash table
463 index = (((unsigned INTPTR)key^intkey) & mask) >>4;
465 index = (((unsigned INTPTR)key) & mask) >>4;
469 // Insert into the new table
473 tmp->intkey = intkey;
475 tmp->val = curr->val;
476 tmp->lnext=dc_c_list;
479 NOTE: Add this case if you change this...
480 This case currently never happens because of the way things rehash....
482 chashlistnode_t *newnode= calloc(1, sizeof(chashlistnode_t));
483 newnode->key = curr->key;
484 newnode->val = curr->val;
485 newnode->next = tmp->next;
489 curr->next=tmp->next;
491 curr->lnext=dc_c_list;
500 free(ptr); //Free the memory of the old hash table
504 //Delete the entire hash table
505 void dc_t_chashDelete() {
507 dcliststruct_t *ptr=dc_c_structs;
509 dcliststruct_t *next=ptr->next;
519 // Search for an address for a given oid
520 INLINE void * dc_t_chashSearch(void * key) {
521 //REMOVE HASH FUNCTION CALL TO MAKE SURE IT IS INLINED HERE
522 dchashlistnode_t *node = &dc_c_table[(((unsigned INTPTR)key) & dc_c_mask)>>4];
525 if(node->key == key) {
529 } while(node != NULL);
535 // Search for an address for a given oid
536 INLINE void * dc_t_chashSearchArray(void * key, unsigned int intkey) {
537 //REMOVE HASH FUNCTION CALL TO MAKE SURE IT IS INLINED HERE
538 dchashlistnode_t *node = &dc_c_table[(((unsigned INTPTR)key^intkey) & dc_c_mask)>>4];
541 if(node->key == key && node->intkey==intkey) {
545 } while(node != NULL);
553 void t_chashCreate(unsigned int size, double loadfactor) {
554 chashtable_t *ctable;
555 chashlistnode_t *nodes;
558 // Allocate space for the hash table
561 c_table = calloc(size, sizeof(chashlistnode_t));
562 c_loadfactor = loadfactor;
564 c_threshold=size*loadfactor;
565 c_mask = (size << 4)-1;
566 c_structs=calloc(1, sizeof(cliststruct_t));
567 c_numelements = 0; // Initial number of elements in the hash
571 void t_chashreset() {
572 chashlistnode_t *ptr = c_table;
575 if (c_numelements<(c_size>>4)) {
576 chashlistnode_t *top=&ptr[c_size];
577 chashlistnode_t *tmpptr=c_list;
578 while(tmpptr!=NULL) {
579 chashlistnode_t *next=tmpptr->lnext;
580 if (tmpptr>=ptr&&tmpptr<top) {
588 bzero(c_table, sizeof(chashlistnode_t)*c_size);
590 while(c_structs->next!=NULL) {
591 cliststruct_t *next=c_structs->next;
600 //Store objects and their pointers into hash
601 void t_chashInsert(void * key, void *val) {
602 chashlistnode_t *ptr;
605 if(c_numelements > (c_threshold)) {
607 unsigned int newsize = c_size << 1;
608 t_chashResize(newsize);
611 ptr = &c_table[(((unsigned INTPTR)key)&c_mask)>>4];
619 } else { // Insert in the beginning of linked list
620 chashlistnode_t * node;
621 if (c_structs->num<NUMCLIST) {
622 node=&c_structs->array[c_structs->num];
626 cliststruct_t *tcl=calloc(1,sizeof(cliststruct_t));
634 node->next = ptr->next;
641 // Search for an address for a given oid
642 INLINE void * t_chashSearch(void * key) {
643 //REMOVE HASH FUNCTION CALL TO MAKE SURE IT IS INLINED HERE
644 chashlistnode_t *node = &c_table[(((unsigned INTPTR)key) & c_mask)>>4];
647 if(node->key == key) {
651 } while(node != NULL);
656 unsigned int t_chashResize(unsigned int newsize) {
657 chashlistnode_t *node, *ptr, *curr; // curr and next keep track of the current and the next chashlistnodes in a linked list
658 unsigned int oldsize;
659 int isfirst; // Keeps track of the first element in the chashlistnode_t for each bin in hashtable
660 unsigned int i,index;
667 if((node = calloc(newsize, sizeof(chashlistnode_t))) == NULL) {
668 printf("Calloc error %s %d\n", __FILE__, __LINE__);
672 c_table = node; //Update the global hashtable upon resize()
674 c_threshold = newsize * c_loadfactor;
675 mask=c_mask = (newsize << 4)-1;
677 for(i = 0; i < oldsize; i++) { //Outer loop for each bin in hash table
680 do { //Inner loop to go through linked lists
682 chashlistnode_t *tmp,*next;
684 if ((key=curr->key) == 0) { //Exit inner loop if there the first element is 0
685 break; //key = val =0 for element if not present within the hash table
687 index = (((unsigned INTPTR)key) & mask) >>4;
690 // Insert into the new table
693 tmp->val = curr->val;
697 NOTE: Add this case if you change this...
698 This case currently never happens because of the way things rehash....
700 chashlistnode_t *newnode= calloc(1, sizeof(chashlistnode_t));
701 newnode->key = curr->key;
702 newnode->val = curr->val;
703 newnode->next = tmp->next;
707 curr->next=tmp->next;
718 free(ptr); //Free the memory of the old hash table
722 //Delete the entire hash table
723 void t_chashDelete() {
725 cliststruct_t *ptr=c_structs;
727 cliststruct_t *next=ptr->next;