adding a test case
[IRC.git] / Robust / src / Runtime / DSTM / interface / objstr.c
1 #include "dstm.h"
2 #include "gCollect.h"
3
4 #define OSUSED(x) (((unsigned int)(x)->top)-((unsigned int) (x+1)))
5 #define OSFREE(x) ((x)->size-OSUSED(x))
6
7 objstr_t *objstrCreate(unsigned int size) {
8   objstr_t *tmp;
9   if((tmp = calloc(1, (sizeof(objstr_t) + size))) == NULL) {
10     printf("%s() Calloc error at line %d, %s\n", __func__, __LINE__, __FILE__);
11     return NULL;
12   }
13   tmp->size = size;
14   tmp->next = NULL;
15   tmp->top = tmp + 1; //points to end of objstr_t structure!
16   return tmp;
17 }
18
19 //free entire list, starting at store
20 void objstrDelete(objstr_t *store) {
21   objstr_t *tmp;
22   while (store != NULL) {
23     tmp = store->next;
24     free(store);
25     store = tmp;
26   }
27   return;
28 }
29
30 void *objstrAlloc(objstr_t **osptr, unsigned int size) {
31   void *tmp;
32   int i=0;
33   objstr_t *store=*osptr;
34   if ((size&7)!=0) {
35     size+=(8-(size&7));
36   }
37
38   for(; i<3; i++) {
39     if (OSFREE(store)>=size) {
40       tmp=store->top;
41       store->top +=size;
42       return tmp;
43     }
44     if ((store=store->next)==NULL)
45       break;
46   }
47
48   {
49     unsigned int newsize=size>DEFAULT_OBJ_STORE_SIZE ? size : DEFAULT_OBJ_STORE_SIZE;
50     objstr_t *os=(objstr_t *)calloc(1,(sizeof(objstr_t) + newsize));
51     void *ptr=&os[1];
52     os->next=*osptr;
53     (*osptr)=os;
54     os->size=newsize;
55     os->top=((char *)ptr)+size;
56     return ptr;
57   }
58 }