3 #include "GCSharedHash.h"
5 #include "runtime_arch.h"
10 #define GC_SHIFT_BITS 4
12 /* GCSHARED HASH ********************************************************/
14 // params: startaddr -- the start addr of the shared memory
15 // rsize -- remaining size of the available shared memory
16 struct GCSharedHash * noargallocateGCSharedHash() {
17 return allocateGCSharedHash(100);
20 struct GCSharedHash * allocateGCSharedHash(int size) {
21 struct GCSharedHash *thisvar;
26 printf("Negative Hashtable size Exception\n");
30 thisvar=(struct GCSharedHash *)FREEMALLOC_NGC(sizeof(struct GCSharedHash));
36 (struct GCSharedNode **)FREEMALLOC_NGC(sizeof(struct GCSharedNode *)*size);
37 if(thisvar->bucket == NULL) {
41 /* Set allocation blocks*/
42 thisvar->listhead=NULL;
43 thisvar->listtail=NULL;
45 thisvar->numelements = 0;
49 void freeGCSharedHash(struct GCSharedHash *thisvar) {
50 struct GCSharedNode *ptr=thisvar->listhead;
51 FREE_NGC(thisvar->bucket);
53 struct GCSharedNode *next=ptr->lnext;
60 bool GCSharedHashrehash(struct GCSharedHash * thisvar) {
61 int newsize=thisvar->size;
62 struct GCSharedNode ** newbucket = (struct GCSharedNode **)
63 FREEMALLOC_NGC(sizeof(struct GCSharedNode *)*newsize);
64 if(newbucket == NULL) {
68 for(i=thisvar->size-1; i>=0; i--) {
69 struct GCSharedNode *ptr;
70 for(ptr=thisvar->bucket[i]; ptr!=NULL;) {
71 struct GCSharedNode * nextptr=ptr->next;
72 unsigned int newhashkey=(unsigned int)ptr->key % newsize;
73 ptr->next=newbucket[newhashkey];
74 newbucket[newhashkey]=ptr;
78 thisvar->size=newsize;
79 FREE_NGC(thisvar->bucket);
80 thisvar->bucket=newbucket;
84 int GCSharedHashadd(struct GCSharedHash * thisvar,int key, int data) {
87 struct GCSharedNode **ptr;
89 if (thisvar->numelements>=thisvar->size) {
90 int newsize=2*thisvar->size+1;
91 struct GCSharedNode ** newbucket =
92 (struct GCSharedNode **)FREEMALLOC_NGC(
93 sizeof(struct GCSharedNode *)*newsize);
94 if(newbucket == NULL) {
98 for(i=thisvar->size-1; i>=0; i--) {
99 struct GCSharedNode *ptr;
100 for(ptr=thisvar->bucket[i]; ptr!=NULL;) {
101 struct GCSharedNode * nextptr=ptr->next;
102 unsigned int newhashkey=(unsigned int)ptr->key % newsize;
103 ptr->next=newbucket[newhashkey];
104 newbucket[newhashkey]=ptr;
108 thisvar->size=newsize;
109 FREE_NGC(thisvar->bucket);
110 thisvar->bucket=newbucket;
113 hashkey = (unsigned int)key % thisvar->size;
114 ptr = &thisvar->bucket[hashkey];
116 /* check that thisvar key/object pair isn't already here */
117 /* TBD can be optimized for set v. relation */
120 if ((*ptr)->key == key && (*ptr)->data == data) {
123 ptr = &((*ptr)->next);
127 struct GCSharedNode *node=FREEMALLOC_NGC(sizeof(struct GCSharedNode));
135 if (thisvar->listhead==NULL) {
136 thisvar->listhead=node;
137 thisvar->listtail=node;
142 node->lnext=thisvar->listhead;
143 thisvar->listhead->lprev=node;
144 thisvar->listhead=node;
148 thisvar->numelements++;
153 struct GCSharedHash * allocateGCSharedHash_I(int size) {
154 struct GCSharedHash *thisvar;
159 printf("Negative Hashtable size Exception\n");
163 thisvar=(struct GCSharedHash *)FREEMALLOC_NGC_I(sizeof(struct GCSharedHash));
164 if(thisvar == NULL) {
167 thisvar->size = size;
169 (struct GCSharedNode **)FREEMALLOC_NGC_I(
170 sizeof(struct GCSharedNode *)*size);
171 if(thisvar->bucket == NULL) {
175 /* Set allocation blocks*/
176 thisvar->listhead=NULL;
177 thisvar->listtail=NULL;
179 thisvar->numelements = 0;
183 int GCSharedHashadd_I(struct GCSharedHash * thisvar,int key, int data) {
185 unsigned int hashkey;
186 struct GCSharedNode **ptr;
188 if (thisvar->numelements>=thisvar->size) {
189 int newsize=2*thisvar->size+1;
190 struct GCSharedNode ** newbucket =
191 (struct GCSharedNode **)FREEMALLOC_NGC_I(
192 sizeof(struct GCSharedNode *)*newsize);
193 if(newbucket == NULL) {
197 for(i=thisvar->size-1; i>=0; i--) {
198 struct GCSharedNode *ptr;
199 for(ptr=thisvar->bucket[i]; ptr!=NULL;) {
200 struct GCSharedNode * nextptr=ptr->next;
201 unsigned int newhashkey=(unsigned int)ptr->key % newsize;
202 ptr->next=newbucket[newhashkey];
203 newbucket[newhashkey]=ptr;
207 thisvar->size=newsize;
208 FREE_NGC_I(thisvar->bucket);
209 thisvar->bucket=newbucket;
212 hashkey = (unsigned int)key % thisvar->size;
213 ptr = &thisvar->bucket[hashkey];
215 /* check that thisvar key/object pair isn't already here */
216 /* TBD can be optimized for set v. relation */
219 if ((*ptr)->key == key && (*ptr)->data == data) {
222 ptr = &((*ptr)->next);
226 struct GCSharedNode *node=FREEMALLOC_NGC_I(sizeof(struct GCSharedNode));
234 if (thisvar->listhead==NULL) {
235 thisvar->listhead=node;
236 thisvar->listtail=node;
241 node->lnext=thisvar->listhead;
242 thisvar->listhead->lprev=node;
243 thisvar->listhead=node;
247 thisvar->numelements++;
252 int GCSharedHashget(struct GCSharedHash *thisvar, int key, int *data) {
253 unsigned int hashkey = (unsigned int)key % thisvar->size;
255 struct GCSharedNode *ptr = thisvar->bucket[hashkey];
257 if (ptr->key == key) {
259 return 1; /* success */
264 return 0; /* failure */
267 /* MGCSHAREDHASH ********************************************************/
269 mgcsharedhashtbl_t * mgcsharedhashCreate(unsigned int size,
271 mgcsharedhashtbl_t * ctable;
272 mgcsharedhashlistnode_t * nodes;
275 ctable = (mgcsharedhashtbl_t *)FREEMALLOC_NGC(sizeof(mgcsharedhashtbl_t));
281 // Allocate space for the hash table
282 ctable->table = (mgcsharedhashlistnode_t *)FREEMALLOC_NGC(
283 size*sizeof(mgcsharedhashlistnode_t));
284 if(ctable->table == NULL) {
285 BAMBOO_EXIT(); // TODO
289 ctable->loadfactor = loadfactor;
290 ctable->threshold = size*loadfactor;
292 ctable->mask = (size << (GC_SHIFT_BITS))-1;
294 ctable->structs = NULL ;
295 ctable->numelements = 0; // Initial number of elements in the hash
301 mgcsharedhashtbl_t * mgcsharedhashCreate_I(unsigned int size,
303 mgcsharedhashtbl_t * ctable;
304 mgcsharedhashlistnode_t * nodes;
307 ctable = (mgcsharedhashtbl_t *)FREEMALLOC_NGC_I(sizeof(mgcsharedhashtbl_t));
313 // Allocate space for the hash table
314 ctable->table = (mgcsharedhashlistnode_t *)FREEMALLOC_NGC_I(
315 size*sizeof(mgcsharedhashlistnode_t));
316 if(ctable->table == NULL) {
317 BAMBOO_EXIT(); // TODO
321 ctable->loadfactor = loadfactor;
322 ctable->threshold = size*loadfactor;
324 ctable->mask = (size << (GC_SHIFT_BITS))-1;
326 ctable->structs = NULL ;
327 ctable->numelements = 0; // Initial number of elements in the hash
333 void mgcsharedhashReset(mgcsharedhashtbl_t * tbl) {
334 mgcsharedhashlistnode_t * ptr = tbl->table;
336 if ((tbl->numelements) < (tbl->size>>6)) {
337 mgcsharedhashlistnode_t *top = &ptr[tbl->size];
338 mgcsharedhashlistnode_t * list = tbl->list;
339 while(list != NULL) {
340 mgcsharedhashlistnode_t * next = list->next;
341 if ((list >= ptr) && (list < top)) {
349 BAMBOO_MEMSET_WH(tbl->table, '\0',
350 sizeof(mgcsharedhashlistnode_t)*tbl->size);
353 mgcsharedliststruct_t * structs = tbl->structs;
354 while(structs != NULL) {
355 mgcsharedliststruct_t * next = structs->next;
356 BAMBOO_MEMSET_WH(structs->array, '\0',
357 structs->num * sizeof(mgcsharedhashlistnode_t));
361 tbl->numelements = 0;
364 //Store objects and their pointers into hash
365 //Using open addressing
366 int mgcsharedhashInsert(mgcsharedhashtbl_t * tbl, void * key, void * val) {
367 mgcsharedhashlistnode_t * ptr;
369 if(tbl->numelements > (tbl->threshold)) {
370 //Never resize, simply don't insert any more
374 ptr=&tbl->table[(((unsigned INTPTR)key)&tbl->mask)>>(GC_SHIFT_BITS)];
377 // the first time insert a value for the key
380 } else { // Insert to the next empty place
381 mgcsharedhashlistnode_t *top = &tbl->table[tbl->size];
384 } while((ptr < top) && (ptr->key != NULL));
392 ptr->next = tbl->list;
398 int mgcsharedhashInsert_I(mgcsharedhashtbl_t * tbl, void * key, void * val) {
399 mgcsharedhashlistnode_t * ptr;
401 if(tbl->numelements > (tbl->threshold)) {
402 //Never resize, simply don't insert any more
406 ptr=&tbl->table[(((unsigned INTPTR)key)&tbl->mask)>>(GC_SHIFT_BITS)];
409 // the first time insert a value for the key
412 } else { // Insert to the next empty place
413 mgcsharedhashlistnode_t * top = &tbl->table[tbl->size];
414 mgcsharedhashlistnode_t * start = ptr;
428 ptr->next = tbl->list;
434 // Search for an address for a given oid
435 INLINE void * mgcsharedhashSearch(mgcsharedhashtbl_t * tbl, void * key) {
436 //REMOVE HASH FUNCTION CALL TO MAKE SURE IT IS INLINED HERE]
437 mgcsharedhashlistnode_t * node =
438 &tbl->table[(((unsigned INTPTR)key)&tbl->mask)>>(GC_SHIFT_BITS)];
439 mgcsharedhashlistnode_t *top = &tbl->table[tbl->size];
442 if(node->key == key) {