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;
13 void t_chashCreate(unsigned int size, double loadfactor) {
15 chashlistnode_t *nodes;
18 // Allocate space for the hash table
21 c_table = calloc(size, sizeof(chashlistnode_t));
22 c_loadfactor = loadfactor;
24 c_threshold=size*loadfactor;
25 c_mask = (size << 4)-1;
26 c_structs=calloc(1, sizeof(cliststruct_t));
27 c_numelements = 0; // Initial number of elements in the hash
32 chashlistnode_t *ptr = c_table;
35 if (c_numelements<(c_size>>4)) {
36 chashlistnode_t *top=&ptr[c_size];
37 chashlistnode_t *tmpptr=c_list;
39 chashlistnode_t *next=tmpptr->lnext;
40 if (tmpptr>=ptr&&tmpptr<top) {
48 bzero(c_table, sizeof(chashlistnode_t)*c_size);
50 while(c_structs->next!=NULL) {
51 cliststruct_t *next=c_structs->next;
60 //Store objects and their pointers into hash
61 void t_chashInsert(void * key, void *val) {
65 if(c_numelements > (c_threshold)) {
67 unsigned int newsize = c_size << 1;
68 t_chashResize(newsize);
71 ptr = &c_table[(((unsigned INTPTR)key)&c_mask)>>4];
79 } else { // Insert in the beginning of linked list
80 chashlistnode_t * node;
81 if (c_structs->num<NUMCLIST) {
82 node=&c_structs->array[c_structs->num];
86 cliststruct_t *tcl=calloc(1,sizeof(cliststruct_t));
94 node->next = ptr->next;
101 // Search for an address for a given oid
102 INLINE void * t_chashSearch(void * key) {
103 //REMOVE HASH FUNCTION CALL TO MAKE SURE IT IS INLINED HERE
104 chashlistnode_t *node = &c_table[(((unsigned INTPTR)key) & c_mask)>>4];
107 if(node->key == key) {
111 } while(node != NULL);
116 unsigned int t_chashResize(unsigned int newsize) {
117 chashlistnode_t *node, *ptr, *curr; // curr and next keep track of the current and the next chashlistnodes in a linked list
118 unsigned int oldsize;
119 int isfirst; // Keeps track of the first element in the chashlistnode_t for each bin in hashtable
120 unsigned int i,index;
127 if((node = calloc(newsize, sizeof(chashlistnode_t))) == NULL) {
128 printf("Calloc error %s %d\n", __FILE__, __LINE__);
132 c_table = node; //Update the global hashtable upon resize()
134 c_threshold = newsize * c_loadfactor;
135 mask=c_mask = (newsize << 4)-1;
137 for(i = 0; i < oldsize; i++) { //Outer loop for each bin in hash table
140 do { //Inner loop to go through linked lists
142 chashlistnode_t *tmp,*next;
144 if ((key=curr->key) == 0) { //Exit inner loop if there the first element is 0
145 break; //key = val =0 for element if not present within the hash table
147 index = (((unsigned INTPTR)key) & mask) >>4;
150 // Insert into the new table
153 tmp->val = curr->val;
157 NOTE: Add this case if you change this...
158 This case currently never happens because of the way things rehash....
160 chashlistnode_t *newnode= calloc(1, sizeof(chashlistnode_t));
161 newnode->key = curr->key;
162 newnode->val = curr->val;
163 newnode->next = tmp->next;
167 curr->next=tmp->next;
178 free(ptr); //Free the memory of the old hash table
182 //Delete the entire hash table
183 void t_chashDelete() {
185 cliststruct_t *ptr=c_structs;
187 cliststruct_t *next=ptr->next;