b8279487fd79de813cea049559a7e5049a5ae816
[IRC.git] / Robust / src / Runtime / DSTM / interface / objstr.c
1 #include "dstm.h"
2 extern objstr_t *prefetchcache;
3
4 objstr_t *objstrCreate(unsigned int size) {
5   objstr_t *tmp;
6   if((tmp = calloc(1, (sizeof(objstr_t) + size))) == NULL) {
7     printf("%s() Calloc error at line %d, %s\n", __func__, __LINE__, __FILE__);
8     return NULL;
9   }
10   tmp->size = size;
11   tmp->next = NULL;
12   tmp->top = tmp + 1; //points to end of objstr_t structure!
13   return tmp;
14 }
15
16 //free entire list, starting at store
17 void objstrDelete(objstr_t *store)
18 {
19         objstr_t *tmp;
20         while (store != NULL)
21         {
22                 tmp = store->next;
23                 free(store);
24                 store = tmp;
25         }
26         return;
27 }
28
29 void *objstrAlloc(objstr_t *store, unsigned int size)
30 {
31         void *tmp;
32         while (1)
33         {
34                 if (((unsigned int)store->top - (unsigned int)store - sizeof(objstr_t) + size) <= store->size)
35                 {  //store not full
36                         tmp = store->top;
37                         store->top += size;
38                         return tmp;
39                 }
40                 //store full
41                 if (store->next == NULL)
42                 {  //end of list, all full
43                         if (size > DEFAULT_OBJ_STORE_SIZE) //in case of large objects
44                         {
45                                 if((store->next = (objstr_t *)calloc(1,(sizeof(objstr_t) + size))) == NULL) {
46                   printf("%s() Calloc error at line %d, %s\n", __func__, __LINE__, __FILE__);
47                   return NULL;
48                 }
49                                 if (store->next == NULL)
50                                         return NULL;
51                                 store = store->next;
52                                 store->size = size;
53                         }
54                         else
55                         {
56                                 if((store->next = calloc(1,(sizeof(objstr_t) + DEFAULT_OBJ_STORE_SIZE))) == NULL) {
57                   printf("%s() Calloc error at line %d, %s\n", __func__, __LINE__, __FILE__);
58                   return NULL;
59                 }
60                                 if (store->next == NULL)
61                                         return NULL;
62                                 store = store->next;
63                                 store->next = NULL;
64                                 store->size = DEFAULT_OBJ_STORE_SIZE;
65                         }
66                         store->top = (void *)((unsigned int)store + sizeof(objstr_t) + size);
67                         return (void *)((unsigned int)store + sizeof(objstr_t));
68                 }
69                 else  //try the next one
70                         store = store->next;
71         }
72 }