This commit was manufactured by cvs2svn to create tag 'buildscript'.
[IRC.git] /
1 #include "dstm.h"
2
3 objstr_t *objstrCreate(unsigned int size) {
4   objstr_t *tmp;
5   if((tmp = calloc(1, (sizeof(objstr_t) + size))) == NULL) {
6     printf("%s() Calloc error at line %d, %s\n", __func__, __LINE__, __FILE__);
7     return NULL;
8   }
9   tmp->size = size;
10   tmp->next = NULL;
11   tmp->top = tmp + 1; //points to end of objstr_t structure!
12   return tmp;
13 }
14
15 //free entire list, starting at store
16 void objstrDelete(objstr_t *store) {
17   objstr_t *tmp;
18   while (store != NULL) {
19     tmp = store->next;
20     free(store);
21     store = tmp;
22   }
23   return;
24 }
25
26 void *objstrAlloc(objstr_t *store, unsigned int size) {
27   void *tmp;
28   while (1) {
29     if (((unsigned int)store->top - (((unsigned int)store) + sizeof(objstr_t)) + size) <= store->size) { //store not full
30       tmp = store->top;
31       store->top += size;
32       return tmp;
33     }
34     //store full
35     if (store->next == NULL) {
36       //end of list, all full
37       if (size > DEFAULT_OBJ_STORE_SIZE) {
38         //in case of large objects
39         if((store->next = (objstr_t *)calloc(1,(sizeof(objstr_t) + size))) == NULL) {
40           printf("%s() Calloc error at line %d, %s\n", __func__, __LINE__, __FILE__);
41           return NULL;
42         }
43         store = store->next;
44         store->size = size;
45       } else {
46         if((store->next = calloc(1,(sizeof(objstr_t) + DEFAULT_OBJ_STORE_SIZE))) == NULL) {
47           printf("%s() Calloc error at line %d, %s\n", __func__, __LINE__, __FILE__);
48           return NULL;
49         }
50         store = store->next;
51         store->size = DEFAULT_OBJ_STORE_SIZE;
52       }
53       store->top = (void *)(((unsigned int)store) + sizeof(objstr_t) + size);
54       return (void *)(((unsigned int)store) + sizeof(objstr_t));
55     } else
56       store = store->next;
57   }
58 }