various changes...
[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   objstr_t *tmp;
19   while (store != NULL) {
20     tmp = store->next;
21     free(store);
22     store = tmp;
23   }
24   return;
25 }
26
27 void *objstrAlloc(objstr_t *store, unsigned int size) {
28   void *tmp;
29   while (1) {
30     if (((unsigned int)store->top - (((unsigned int)store) + sizeof(objstr_t)) + size) <= store->size) { //store not full
31       tmp = store->top;
32       store->top += size;
33       return tmp;
34     }
35     //store full
36     if (store->next == NULL) {
37       //end of list, all full
38       if (size > DEFAULT_OBJ_STORE_SIZE) {
39         //in case of large objects
40         if((store->next = (objstr_t *)calloc(1,(sizeof(objstr_t) + size))) == NULL) {
41           printf("%s() Calloc error at line %d, %s\n", __func__, __LINE__, __FILE__);
42           return NULL;
43         }
44         store = store->next;
45         store->size = size;
46       } else {
47         if((store->next = calloc(1,(sizeof(objstr_t) + DEFAULT_OBJ_STORE_SIZE))) == NULL) {
48           printf("%s() Calloc error at line %d, %s\n", __func__, __LINE__, __FILE__);
49           return NULL;
50         }
51         store = store->next;
52         store->size = DEFAULT_OBJ_STORE_SIZE;
53       }
54       store->top = (void *)(((unsigned int)store) + sizeof(objstr_t) + size);
55       return (void *)(((unsigned int)store) + sizeof(objstr_t));
56     } else
57       store = store->next;
58   }
59 }