2 #define INLINE inline __attribute__((always_inline))
4 chashtable_t *chashCreate(unsigned int size, float loadfactor) {
6 struct chashentry *nodes;
9 if((ctable = calloc(1, sizeof(chashtable_t))) == NULL) {
10 printf("Calloc error %s %d\n", __FILE__, __LINE__);
14 // Allocate space for the hash table
15 if((nodes = calloc(size, sizeof(struct chashentry))) == NULL) {
16 printf("Calloc error %s %d\n", __FILE__, __LINE__);
21 ctable->table = nodes;
23 ctable->mask = (size << 1)-1;
24 ctable->numelements = 0; // Initial number of elements in the hash
25 ctable->loadfactor = loadfactor;
26 ctable->capacity=ctable->loadfactor*ctable->size;
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
35 //Store objects and their pointers into hash
36 void chashInsert(chashtable_t *table, unsigned int key, void *val) {
39 struct chashentry *node;
41 if(table->numelements > table->capacity) {
43 newsize = table->size << 1;
44 chashResize(table,newsize);
46 index=(key &table->mask)>>1;
48 node = & table->table[index];
49 if (node->ptr==NULL) {
55 if (index==table->size)
60 // Search for an address for a given oid
61 INLINE void * chashSearch(chashtable_t *table, unsigned int key) {
62 //REMOVE HASH FUNCTION CALL TO MAKE SURE IT IS INLINED HERE
63 unsigned int tmp=(key &table->mask)>>1;
64 struct chashentry *node;
67 node = & table->table[tmp];
79 void chashResize(chashtable_t *table, unsigned int newsize) {
80 unsigned int oldsize=table->size;
81 struct chashentry *ptr= table->table;
82 struct chashentry *node= calloc(newsize, sizeof(struct chashentry));
85 struct chashentry *newnode;
88 struct chashentry *curr;
91 printf("Calloc error %s %d\n", __FILE__, __LINE__);
95 table->table = node; //Update the global hashtable upon resize()
96 table->size = newsize;
97 mask=table->mask = (newsize << 1)-1;
98 table->numelements = 0;
100 for(i = 0; i < oldsize; i++) { //Outer loop for each bin in hash table
106 newnode= & table->table[bin];
107 if (newnode->key==0) {
109 newnode->ptr=curr->ptr;
119 free(ptr); //Free the memory of the old hash table
122 //Delete the entire hash table
123 void chashDelete(chashtable_t *ctable) {