3 #include "GCSharedHash.h"
5 #include "runtime_arch.h"
21 #define INLINE inline __attribute__((always_inline))
22 #endif // #ifndef INLINE
24 // TODO check the average collision times
25 //int gc_num_search = 0;
26 //int gc_num_collision = 0;
28 /* GCSHARED HASH ********************************************************/
30 // params: startaddr -- the start addr of the shared memory
31 // rsize -- remaining size of the available shared memory
32 struct GCSharedHash * noargallocateGCSharedHash() {
33 return allocateGCSharedHash(100);
36 struct GCSharedHash * allocateGCSharedHash(int size) {
37 struct GCSharedHash *thisvar;
42 printf("Negative Hashtable size Exception\n");
46 thisvar=(struct GCSharedHash *)FREEMALLOC_NGC(sizeof(struct GCSharedHash));
52 (struct GCSharedNode **)FREEMALLOC_NGC(sizeof(struct GCSharedNode *)*size);
53 if(thisvar->bucket == NULL) {
57 /* Set allocation blocks*/
58 thisvar->listhead=NULL;
59 thisvar->listtail=NULL;
61 thisvar->numelements = 0;
65 void freeGCSharedHash(struct GCSharedHash *thisvar) {
66 struct GCSharedNode *ptr=thisvar->listhead;
67 FREE_NGC(thisvar->bucket);
69 struct GCSharedNode *next=ptr->lnext;
76 bool GCSharedHashrehash(struct GCSharedHash * thisvar) {
77 int newsize=thisvar->size;
78 struct GCSharedNode ** newbucket = (struct GCSharedNode **)
79 FREEMALLOC_NGC(sizeof(struct GCSharedNode *)*newsize);
80 if(newbucket == NULL) {
84 for(i=thisvar->size-1; i>=0; i--) {
85 struct GCSharedNode *ptr;
86 for(ptr=thisvar->bucket[i]; ptr!=NULL;) {
87 struct GCSharedNode * nextptr=ptr->next;
88 unsigned int newhashkey=(unsigned int)ptr->key % newsize;
89 ptr->next=newbucket[newhashkey];
90 newbucket[newhashkey]=ptr;
94 thisvar->size=newsize;
95 FREE_NGC(thisvar->bucket);
96 thisvar->bucket=newbucket;
100 int GCSharedHashadd(struct GCSharedHash * thisvar,int key, int data) {
102 unsigned int hashkey;
103 struct GCSharedNode **ptr;
105 if (thisvar->numelements>=thisvar->size) {
106 int newsize=2*thisvar->size+1;
107 struct GCSharedNode ** newbucket =
108 (struct GCSharedNode **)FREEMALLOC_NGC(
109 sizeof(struct GCSharedNode *)*newsize);
110 if(newbucket == NULL) {
114 for(i=thisvar->size-1; i>=0; i--) {
115 struct GCSharedNode *ptr;
116 for(ptr=thisvar->bucket[i]; ptr!=NULL;) {
117 struct GCSharedNode * nextptr=ptr->next;
118 unsigned int newhashkey=(unsigned int)ptr->key % newsize;
119 ptr->next=newbucket[newhashkey];
120 newbucket[newhashkey]=ptr;
124 thisvar->size=newsize;
125 FREE_NGC(thisvar->bucket);
126 thisvar->bucket=newbucket;
129 hashkey = (unsigned int)key % thisvar->size;
130 ptr = &thisvar->bucket[hashkey];
132 /* check that thisvar key/object pair isn't already here */
133 /* TBD can be optimized for set v. relation */
136 if ((*ptr)->key == key && (*ptr)->data == data) {
139 ptr = &((*ptr)->next);
143 struct GCSharedNode *node=FREEMALLOC_NGC(sizeof(struct GCSharedNode));
151 if (thisvar->listhead==NULL) {
152 thisvar->listhead=node;
153 thisvar->listtail=node;
158 node->lnext=thisvar->listhead;
159 thisvar->listhead->lprev=node;
160 thisvar->listhead=node;
164 thisvar->numelements++;
169 struct GCSharedHash * allocateGCSharedHash_I(int size) {
170 struct GCSharedHash *thisvar;
175 printf("Negative Hashtable size Exception\n");
179 thisvar=(struct GCSharedHash *)FREEMALLOC_NGC_I(sizeof(struct GCSharedHash));
180 if(thisvar == NULL) {
183 thisvar->size = size;
185 (struct GCSharedNode **)FREEMALLOC_NGC_I(
186 sizeof(struct GCSharedNode *)*size);
187 if(thisvar->bucket == NULL) {
191 /* Set allocation blocks*/
192 thisvar->listhead=NULL;
193 thisvar->listtail=NULL;
195 thisvar->numelements = 0;
199 int GCSharedHashadd_I(struct GCSharedHash * thisvar,int key, int data) {
201 unsigned int hashkey;
202 struct GCSharedNode **ptr;
204 if (thisvar->numelements>=thisvar->size) {
205 int newsize=2*thisvar->size+1;
206 struct GCSharedNode ** newbucket =
207 (struct GCSharedNode **)FREEMALLOC_NGC_I(
208 sizeof(struct GCSharedNode *)*newsize);
209 if(newbucket == NULL) {
213 for(i=thisvar->size-1; i>=0; i--) {
214 struct GCSharedNode *ptr;
215 for(ptr=thisvar->bucket[i]; ptr!=NULL;) {
216 struct GCSharedNode * nextptr=ptr->next;
217 unsigned int newhashkey=(unsigned int)ptr->key % newsize;
218 ptr->next=newbucket[newhashkey];
219 newbucket[newhashkey]=ptr;
223 thisvar->size=newsize;
224 FREE_NGC_I(thisvar->bucket);
225 thisvar->bucket=newbucket;
228 hashkey = (unsigned int)key % thisvar->size;
229 ptr = &thisvar->bucket[hashkey];
231 /* check that thisvar key/object pair isn't already here */
232 /* TBD can be optimized for set v. relation */
235 if ((*ptr)->key == key && (*ptr)->data == data) {
238 ptr = &((*ptr)->next);
242 struct GCSharedNode *node=FREEMALLOC_NGC_I(sizeof(struct GCSharedNode));
250 if (thisvar->listhead==NULL) {
251 thisvar->listhead=node;
252 thisvar->listtail=node;
257 node->lnext=thisvar->listhead;
258 thisvar->listhead->lprev=node;
259 thisvar->listhead=node;
263 thisvar->numelements++;
268 int GCSharedHashget(struct GCSharedHash *thisvar, int key, int *data) {
269 unsigned int hashkey = (unsigned int)key % thisvar->size;
271 struct GCSharedNode *ptr = thisvar->bucket[hashkey];
273 if (ptr->key == key) {
275 return 1; /* success */
280 return 0; /* failure */
283 /* MGCSHAREDHASH ********************************************************/
285 mgcsharedhashtbl_t * mgcsharedhashCreate(unsigned int size,
287 mgcsharedhashtbl_t * ctable;
288 mgcsharedhashlistnode_t * nodes;
291 ctable = (mgcsharedhashtbl_t *)FREEMALLOC_NGC(sizeof(mgcsharedhashtbl_t));
297 // Allocate space for the hash table
298 ctable->table = (mgcsharedhashlistnode_t *)FREEMALLOC_NGC(
299 size*sizeof(mgcsharedhashlistnode_t));
300 if(ctable->table == NULL) {
301 BAMBOO_EXIT(0xffff); // TODO
305 ctable->loadfactor = loadfactor;
306 ctable->threshold = size*loadfactor;
308 ctable->mask = (size << 6)-1;
310 ctable->structs = NULL ; //FREEMALLOC_NGC(1*sizeof(mgcliststruct_t));
311 ctable->numelements = 0; // Initial number of elements in the hash
317 mgcsharedhashtbl_t * mgcsharedhashCreate_I(unsigned int size,
319 mgcsharedhashtbl_t * ctable;
320 mgcsharedhashlistnode_t * nodes;
323 ctable = (mgcsharedhashtbl_t *)FREEMALLOC_NGC_I(sizeof(mgcsharedhashtbl_t));
329 // Allocate space for the hash table
330 ctable->table = (mgcsharedhashlistnode_t *)FREEMALLOC_NGC_I(
331 size*sizeof(mgcsharedhashlistnode_t));
332 if(ctable->table == NULL) {
333 BAMBOO_EXIT(0xfffe); // TODO
337 ctable->loadfactor = loadfactor;
338 ctable->threshold = size*loadfactor;
340 ctable->mask = (size << 6)-1;
342 ctable->structs = NULL ; //FREEMALLOC_NGC(1*sizeof(mgcliststruct_t));
343 ctable->numelements = 0; // Initial number of elements in the hash
349 void mgcsharedhashReset(mgcsharedhashtbl_t * tbl) {
350 mgcsharedhashlistnode_t * ptr = tbl->table;
352 if ((tbl->numelements) < (tbl->size>>6)) {
353 mgcsharedhashlistnode_t * list = tbl->list;
354 while(list != NULL) {
355 mgcsharedhashlistnode_t *top = &ptr[tbl->size];
356 mgcsharedhashlistnode_t * next = list->next;
357 if ((list >= ptr) && (list < top)) {
365 BAMBOO_MEMSET_WH(tbl->table, '\0',
366 sizeof(mgcsharedhashlistnode_t)*tbl->size);
369 mgcsharedliststruct_t * structs = tbl->structs;
370 while(structs != NULL) {
371 mgcsharedliststruct_t * next = structs->next;
372 BAMBOO_MEMSET_WH(structs->array, '\0',
373 structs->num * sizeof(mgcsharedhashlistnode_t));
377 tbl->numelements = 0;
380 //Store objects and their pointers into hash
381 //Using open addressing
382 int mgcsharedhashInsert(mgcsharedhashtbl_t * tbl, void * key, void * val) {
383 mgcsharedhashlistnode_t * ptr;
385 if(tbl->numelements > (tbl->threshold)) {
386 //Never resize, simply don't insert any more
390 //int keyto = ((unsigned INTPTR)key) % (tbl->size);
391 //ptr=&tbl->table[keyto];
392 ptr=&tbl->table[(((unsigned INTPTR)key)&tbl->mask)>>6];
393 //printf("%x \n", (((unsigned INTPTR)key)&tbl->mask)>>6); // TODO
396 // the first time insert a value for the key
399 } else { // Insert to the next empty place
400 mgcsharedhashlistnode_t *top = &tbl->table[tbl->size];
403 } while((ptr < top) && (ptr->key != NULL));
411 ptr->next = tbl->list;
417 int mgcsharedhashInsert_I(mgcsharedhashtbl_t * tbl, void * key, void * val) {
418 mgcsharedhashlistnode_t * ptr;
420 if(tbl->numelements > (tbl->threshold)) {
421 //Never resize, simply don't insert any more
425 //int keyto = ((unsigned INTPTR)key) % (tbl->size);
426 //ptr=&tbl->table[keyto];
427 ptr=&tbl->table[(((unsigned INTPTR)key)&tbl->mask)>>6];
428 //printf("%x \n", (((unsigned INTPTR)key)&tbl->mask)>>6); // TODO
431 // the first time insert a value for the key
434 } else { // Insert to the next empty place
435 mgcsharedhashlistnode_t * top = &tbl->table[tbl->size];
436 mgcsharedhashlistnode_t * start = ptr;
450 ptr->next = tbl->list;
456 // Search for an address for a given oid
457 INLINE void * mgcsharedhashSearch(mgcsharedhashtbl_t * tbl, void * key) {
458 //REMOVE HASH FUNCTION CALL TO MAKE SURE IT IS INLINED HERE]
459 //int keyto = ((unsigned INTPTR)key) % (tbl->size);
460 //mgcsharedhashlistnode_t * node=&tbl->table[keyto];
461 mgcsharedhashlistnode_t * node =
462 &tbl->table[(((unsigned INTPTR)key)&tbl->mask)>>6];
463 mgcsharedhashlistnode_t *top = &tbl->table[tbl->size];
469 if(node->key == key) {
471 //printf("%x \n", 0xe000+i);
472 //gc_num_collision += i;