__thread unsigned int c_numelements;
__thread unsigned int c_threshold;
__thread double c_loadfactor;
+__thread cliststruct_t *c_structs;
void t_chashCreate(unsigned int size, double loadfactor) {
chashtable_t *ctable;
c_loadfactor = loadfactor;
c_size = size;
c_threshold=size*loadfactor;
- c_mask = (size << 3)-1;
+ c_mask = (size << 4)-1;
+ c_structs=calloc(1, sizeof(cliststruct_t));
c_numelements = 0; // Initial number of elements in the hash
c_list=NULL;
}
void t_chashreset() {
chashlistnode_t *ptr = c_table;
int i;
- if (c_numelements<(c_size>>3)) {
+
+ if (c_numelements<(c_size>>4)) {
chashlistnode_t *top=&ptr[c_size];
chashlistnode_t *tmpptr=c_list;
while(tmpptr!=NULL) {
//zero in list
tmpptr->key=0;
tmpptr->next=NULL;
- } else {
- free(tmpptr);
}
tmpptr=next;
}
} else {
- for(i=0 ; i<c_size ; i++) {
- chashlistnode_t * curr = ptr[i].next;
- while(curr!=NULL) {
- chashlistnode_t * next = curr->next;
- free(curr);
- curr=next;
- }
- }
bzero(c_table, sizeof(chashlistnode_t)*c_size);
}
+ while(c_structs->next!=NULL) {
+ cliststruct_t *next=c_structs->next;
+ free(c_structs);
+ c_structs=next;
+ }
+ c_structs->num = 0;
c_numelements = 0;
c_list=NULL;
}
t_chashResize(newsize);
}
- ptr = &c_table[(((unsigned INTPTR)key)&c_mask)>>3];
+ ptr = &c_table[(((unsigned INTPTR)key)&c_mask)>>4];
c_numelements++;
if(ptr->key==0) {
ptr->lnext=c_list;
c_list=ptr;
} else { // Insert in the beginning of linked list
- chashlistnode_t * node = calloc(1, sizeof(chashlistnode_t));
+ chashlistnode_t * node;
+ if (c_structs->num<NUMCLIST) {
+ node=&c_structs->array[c_structs->num];
+ c_structs->num++;
+ } else {
+ //get new list
+ cliststruct_t *tcl=calloc(1,sizeof(cliststruct_t));
+ tcl->next=c_structs;
+ c_structs=tcl;
+ node=&tcl->array[0];
+ tcl->num=1;
+ }
node->key = key;
node->val = val;
node->next = ptr->next;
// Search for an address for a given oid
INLINE void * t_chashSearch(void * key) {
//REMOVE HASH FUNCTION CALL TO MAKE SURE IT IS INLINED HERE
- chashlistnode_t *node = &c_table[(((unsigned INTPTR)key) & c_mask)>>3];
+ chashlistnode_t *node = &c_table[(((unsigned INTPTR)key) & c_mask)>>4];
do {
if(node->key == key) {
return NULL;
}
+
unsigned int t_chashResize(unsigned int newsize) {
chashlistnode_t *node, *ptr, *curr; // curr and next keep track of the current and the next chashlistnodes in a linked list
unsigned int oldsize;
c_table = node; //Update the global hashtable upon resize()
c_size = newsize;
c_threshold = newsize * c_loadfactor;
- mask=c_mask = (newsize << 3)-1;
+ mask=c_mask = (newsize << 4)-1;
for(i = 0; i < oldsize; i++) { //Outer loop for each bin in hash table
curr = &ptr[i];
if ((key=curr->key) == 0) { //Exit inner loop if there the first element is 0
break; //key = val =0 for element if not present within the hash table
}
- next = curr->next;
- index = (((unsigned INTPTR)key) & mask) >>3;
+ index = (((unsigned INTPTR)key) & mask) >>4;
tmp=&node[index];
+ next = curr->next;
// Insert into the new table
if(tmp->key == 0) {
- tmp->key = curr->key;
+ tmp->key = key;
tmp->val = curr->val;
tmp->lnext=c_list;
c_list=tmp;
- if (!isfirst) {
- free(curr);
- }
}/*
NOTE: Add this case if you change this...
This case currently never happens because of the way things rehash....
//Delete the entire hash table
void t_chashDelete() {
int i;
- chashlistnode_t *ptr = c_table;
-
- for(i=0 ; i<c_size ; i++) {
- chashlistnode_t * curr = ptr[i].next;
- while(curr!=NULL) {
- chashlistnode_t * next = curr->next;
- free(curr);
- curr=next;
- }
+ cliststruct_t *ptr=c_structs;
+ while(ptr!=NULL) {
+ cliststruct_t *next=ptr->next;
+ free(ptr);
+ ptr=next;
}
- free(ptr);
+ free(c_table);
c_table=NULL;
+ c_structs=NULL;
c_list=NULL;
}
}
void fixtable(chashlistnode_t ** tc_table, chashlistnode_t **tc_list, unsigned int tc_size) {
- unsigned int mask=(tc_size<<3)-1;
+ unsigned int mask=(tc_size<<4)-1;
chashlistnode_t *node=calloc(tc_size, sizeof(chashlistnode_t));
chashlistnode_t *ptr=*tc_table;
chashlistnode_t *curr;
}
next = curr->next;
- index = (((unsigned INTPTR)key) & mask) >>3;
+ index = (((unsigned INTPTR)key) & mask) >>4;
curr->key=key;
tmp=&node[index];
tmp->val = curr->val;
tmp->lnext=newlist;
newlist=tmp;
- if (!isfirst) {
- free(curr);
- }
} else if (isfirst) {
- chashlistnode_t *newnode= calloc(1, sizeof(chashlistnode_t));
+ chashlistnode_t *newnode;
+ if (c_structs->num<NUMCLIST) {
+ newnode=&c_structs->array[c_structs->num];
+ c_structs->num++;
+ } else {
+ //get new list
+ cliststruct_t *tcl=calloc(1,sizeof(cliststruct_t));
+ tcl->next=c_structs;
+ c_structs=tcl;
+ newnode=&tcl->array[0];
+ tcl->num=1;
+ }
newnode->key = curr->key;
newnode->val = curr->val;
newnode->next = tmp->next;