3 #include "GCSharedHash.h"
5 #include "runtime_arch.h"
21 #define INLINE inline __attribute__((always_inline))
22 #endif // #ifndef INLINE
24 #define GC_SHIFT_BITS 4
26 /* GCSHARED HASH ********************************************************/
28 // params: startaddr -- the start addr of the shared memory
29 // rsize -- remaining size of the available shared memory
30 struct GCSharedHash * noargallocateGCSharedHash() {
31 return allocateGCSharedHash(100);
34 struct GCSharedHash * allocateGCSharedHash(int size) {
35 struct GCSharedHash *thisvar;
40 printf("Negative Hashtable size Exception\n");
44 thisvar=(struct GCSharedHash *)FREEMALLOC_NGC(sizeof(struct GCSharedHash));
50 (struct GCSharedNode **)FREEMALLOC_NGC(sizeof(struct GCSharedNode *)*size);
51 if(thisvar->bucket == NULL) {
55 /* Set allocation blocks*/
56 thisvar->listhead=NULL;
57 thisvar->listtail=NULL;
59 thisvar->numelements = 0;
63 void freeGCSharedHash(struct GCSharedHash *thisvar) {
64 struct GCSharedNode *ptr=thisvar->listhead;
65 FREE_NGC(thisvar->bucket);
67 struct GCSharedNode *next=ptr->lnext;
74 bool GCSharedHashrehash(struct GCSharedHash * thisvar) {
75 int newsize=thisvar->size;
76 struct GCSharedNode ** newbucket = (struct GCSharedNode **)
77 FREEMALLOC_NGC(sizeof(struct GCSharedNode *)*newsize);
78 if(newbucket == NULL) {
82 for(i=thisvar->size-1; i>=0; i--) {
83 struct GCSharedNode *ptr;
84 for(ptr=thisvar->bucket[i]; ptr!=NULL;) {
85 struct GCSharedNode * nextptr=ptr->next;
86 unsigned int newhashkey=(unsigned int)ptr->key % newsize;
87 ptr->next=newbucket[newhashkey];
88 newbucket[newhashkey]=ptr;
92 thisvar->size=newsize;
93 FREE_NGC(thisvar->bucket);
94 thisvar->bucket=newbucket;
98 int GCSharedHashadd(struct GCSharedHash * thisvar,int key, int data) {
100 unsigned int hashkey;
101 struct GCSharedNode **ptr;
103 if (thisvar->numelements>=thisvar->size) {
104 int newsize=2*thisvar->size+1;
105 struct GCSharedNode ** newbucket =
106 (struct GCSharedNode **)FREEMALLOC_NGC(
107 sizeof(struct GCSharedNode *)*newsize);
108 if(newbucket == NULL) {
112 for(i=thisvar->size-1; i>=0; i--) {
113 struct GCSharedNode *ptr;
114 for(ptr=thisvar->bucket[i]; ptr!=NULL;) {
115 struct GCSharedNode * nextptr=ptr->next;
116 unsigned int newhashkey=(unsigned int)ptr->key % newsize;
117 ptr->next=newbucket[newhashkey];
118 newbucket[newhashkey]=ptr;
122 thisvar->size=newsize;
123 FREE_NGC(thisvar->bucket);
124 thisvar->bucket=newbucket;
127 hashkey = (unsigned int)key % thisvar->size;
128 ptr = &thisvar->bucket[hashkey];
130 /* check that thisvar key/object pair isn't already here */
131 /* TBD can be optimized for set v. relation */
134 if ((*ptr)->key == key && (*ptr)->data == data) {
137 ptr = &((*ptr)->next);
141 struct GCSharedNode *node=FREEMALLOC_NGC(sizeof(struct GCSharedNode));
149 if (thisvar->listhead==NULL) {
150 thisvar->listhead=node;
151 thisvar->listtail=node;
156 node->lnext=thisvar->listhead;
157 thisvar->listhead->lprev=node;
158 thisvar->listhead=node;
162 thisvar->numelements++;
167 struct GCSharedHash * allocateGCSharedHash_I(int size) {
168 struct GCSharedHash *thisvar;
173 printf("Negative Hashtable size Exception\n");
177 thisvar=(struct GCSharedHash *)FREEMALLOC_NGC_I(sizeof(struct GCSharedHash));
178 if(thisvar == NULL) {
181 thisvar->size = size;
183 (struct GCSharedNode **)FREEMALLOC_NGC_I(
184 sizeof(struct GCSharedNode *)*size);
185 if(thisvar->bucket == NULL) {
189 /* Set allocation blocks*/
190 thisvar->listhead=NULL;
191 thisvar->listtail=NULL;
193 thisvar->numelements = 0;
197 int GCSharedHashadd_I(struct GCSharedHash * thisvar,int key, int data) {
199 unsigned int hashkey;
200 struct GCSharedNode **ptr;
202 if (thisvar->numelements>=thisvar->size) {
203 int newsize=2*thisvar->size+1;
204 struct GCSharedNode ** newbucket =
205 (struct GCSharedNode **)FREEMALLOC_NGC_I(
206 sizeof(struct GCSharedNode *)*newsize);
207 if(newbucket == NULL) {
211 for(i=thisvar->size-1; i>=0; i--) {
212 struct GCSharedNode *ptr;
213 for(ptr=thisvar->bucket[i]; ptr!=NULL;) {
214 struct GCSharedNode * nextptr=ptr->next;
215 unsigned int newhashkey=(unsigned int)ptr->key % newsize;
216 ptr->next=newbucket[newhashkey];
217 newbucket[newhashkey]=ptr;
221 thisvar->size=newsize;
222 FREE_NGC_I(thisvar->bucket);
223 thisvar->bucket=newbucket;
226 hashkey = (unsigned int)key % thisvar->size;
227 ptr = &thisvar->bucket[hashkey];
229 /* check that thisvar key/object pair isn't already here */
230 /* TBD can be optimized for set v. relation */
233 if ((*ptr)->key == key && (*ptr)->data == data) {
236 ptr = &((*ptr)->next);
240 struct GCSharedNode *node=FREEMALLOC_NGC_I(sizeof(struct GCSharedNode));
248 if (thisvar->listhead==NULL) {
249 thisvar->listhead=node;
250 thisvar->listtail=node;
255 node->lnext=thisvar->listhead;
256 thisvar->listhead->lprev=node;
257 thisvar->listhead=node;
261 thisvar->numelements++;
266 int GCSharedHashget(struct GCSharedHash *thisvar, int key, int *data) {
267 unsigned int hashkey = (unsigned int)key % thisvar->size;
269 struct GCSharedNode *ptr = thisvar->bucket[hashkey];
271 if (ptr->key == key) {
273 return 1; /* success */
278 return 0; /* failure */
281 /* MGCSHAREDHASH ********************************************************/
283 mgcsharedhashtbl_t * mgcsharedhashCreate(unsigned int size,
285 mgcsharedhashtbl_t * ctable;
286 mgcsharedhashlistnode_t * nodes;
289 ctable = (mgcsharedhashtbl_t *)FREEMALLOC_NGC(sizeof(mgcsharedhashtbl_t));
295 // Allocate space for the hash table
296 ctable->table = (mgcsharedhashlistnode_t *)FREEMALLOC_NGC(
297 size*sizeof(mgcsharedhashlistnode_t));
298 if(ctable->table == NULL) {
299 BAMBOO_EXIT(0xf204); // TODO
303 ctable->loadfactor = loadfactor;
304 ctable->threshold = size*loadfactor;
306 ctable->mask = (size << (GC_SHIFT_BITS))-1;
308 ctable->structs = NULL ; //FREEMALLOC_NGC(1*sizeof(mgcliststruct_t));
309 ctable->numelements = 0; // Initial number of elements in the hash
315 mgcsharedhashtbl_t * mgcsharedhashCreate_I(unsigned int size,
317 mgcsharedhashtbl_t * ctable;
318 mgcsharedhashlistnode_t * nodes;
321 ctable = (mgcsharedhashtbl_t *)FREEMALLOC_NGC_I(sizeof(mgcsharedhashtbl_t));
327 // Allocate space for the hash table
328 ctable->table = (mgcsharedhashlistnode_t *)FREEMALLOC_NGC_I(
329 size*sizeof(mgcsharedhashlistnode_t));
330 if(ctable->table == NULL) {
331 BAMBOO_EXIT(0xf206); // TODO
335 ctable->loadfactor = loadfactor;
336 ctable->threshold = size*loadfactor;
338 ctable->mask = (size << (GC_SHIFT_BITS))-1;
340 ctable->structs = NULL ; //FREEMALLOC_NGC(1*sizeof(mgcliststruct_t));
341 ctable->numelements = 0; // Initial number of elements in the hash
347 void mgcsharedhashReset(mgcsharedhashtbl_t * tbl) {
348 mgcsharedhashlistnode_t * ptr = tbl->table;
350 if ((tbl->numelements) < (tbl->size>>6)) {
351 mgcsharedhashlistnode_t *top = &ptr[tbl->size];
352 mgcsharedhashlistnode_t * list = tbl->list;
353 while(list != NULL) {
354 mgcsharedhashlistnode_t * next = list->next;
355 if ((list >= ptr) && (list < top)) {
363 BAMBOO_MEMSET_WH(tbl->table, '\0',
364 sizeof(mgcsharedhashlistnode_t)*tbl->size);
367 mgcsharedliststruct_t * structs = tbl->structs;
368 while(structs != NULL) {
369 mgcsharedliststruct_t * next = structs->next;
370 BAMBOO_MEMSET_WH(structs->array, '\0',
371 structs->num * sizeof(mgcsharedhashlistnode_t));
375 tbl->numelements = 0;
378 //Store objects and their pointers into hash
379 //Using open addressing
380 int mgcsharedhashInsert(mgcsharedhashtbl_t * tbl, void * key, void * val) {
381 mgcsharedhashlistnode_t * ptr;
383 if(tbl->numelements > (tbl->threshold)) {
384 //Never resize, simply don't insert any more
388 //int keyto = ((unsigned INTPTR)key) % (tbl->size);
389 //ptr=&tbl->table[keyto];
390 ptr=&tbl->table[(((unsigned INTPTR)key)&tbl->mask)>>(GC_SHIFT_BITS)];
393 // the first time insert a value for the key
396 } else { // Insert to the next empty place
397 mgcsharedhashlistnode_t *top = &tbl->table[tbl->size];
400 } while((ptr < top) && (ptr->key != NULL));
408 ptr->next = tbl->list;
414 int mgcsharedhashInsert_I(mgcsharedhashtbl_t * tbl, void * key, void * val) {
415 mgcsharedhashlistnode_t * ptr;
417 if(tbl->numelements > (tbl->threshold)) {
418 //Never resize, simply don't insert any more
422 //int keyto = ((unsigned INTPTR)key) % (tbl->size);
423 //ptr=&tbl->table[keyto];
424 ptr=&tbl->table[(((unsigned INTPTR)key)&tbl->mask)>>(GC_SHIFT_BITS)];
427 // the first time insert a value for the key
430 } else { // Insert to the next empty place
431 mgcsharedhashlistnode_t * top = &tbl->table[tbl->size];
432 mgcsharedhashlistnode_t * start = ptr;
446 ptr->next = tbl->list;
452 // Search for an address for a given oid
453 INLINE void * mgcsharedhashSearch(mgcsharedhashtbl_t * tbl, void * key) {
454 //REMOVE HASH FUNCTION CALL TO MAKE SURE IT IS INLINED HERE]
455 //int keyto = ((unsigned INTPTR)key) % (tbl->size);
456 //mgcsharedhashlistnode_t * node=&tbl->table[keyto];
457 mgcsharedhashlistnode_t * node =
458 &tbl->table[(((unsigned INTPTR)key)&tbl->mask)>>(GC_SHIFT_BITS)];
459 mgcsharedhashlistnode_t *top = &tbl->table[tbl->size];
463 if(node->key == key) {