exip  Alpha 0.5.4
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
memManagement.c
Go to the documentation of this file.
1 /*==================================================================*\
2 | EXIP - Embeddable EXI Processor in C |
3 |--------------------------------------------------------------------|
4 | This work is licensed under BSD 3-Clause License |
5 | The full license terms and conditions are located in LICENSE.txt |
6 \===================================================================*/
7 
17 #include "memManagement.h"
18 #include "hashtable.h"
19 #include "dynamicArray.h"
20 #include "sTables.h"
21 #include "grammars.h"
22 
24 {
25  list->firstBlock = EXIP_MALLOC(sizeof(struct allocBlock));
26  if(list->firstBlock == NULL)
28  list->firstBlock->nextBlock = NULL;
29  list->lastBlock = list->firstBlock;
30  list->currAllocSlot = 0;
31 
32  return EXIP_OK;
33 }
34 
35 void* memManagedAllocate(AllocList* list, size_t size)
36 {
37  void* ptr = EXIP_MALLOC(size);
38  if(ptr != NULL)
39  {
41  {
42  struct allocBlock* newBlock = EXIP_MALLOC(sizeof(struct allocBlock));
43  if(newBlock == NULL)
44  return NULL;
45 
46  newBlock->nextBlock = NULL;
47  list->lastBlock->nextBlock = newBlock;
48  list->lastBlock = newBlock;
49  list->currAllocSlot = 0;
50  }
51 
52  list->lastBlock->allocation[list->currAllocSlot] = ptr;
53  list->currAllocSlot += 1;
54  }
55  return ptr;
56 }
57 
58 void freeAllMem(EXIStream* strm)
59 {
60  Index i;
61 
62  if(strm->schema != NULL) // can be, in case of error during EXIStream initialization
63  {
64 #if BUILD_IN_GRAMMARS_USE
65  {
66  Index g;
67  DynGrammarRule* tmp_rule;
68  // Explicitly free the memory for any build-in grammars
69  for(g = strm->schema->staticGrCount; g < strm->schema->grammarTable.count; g++)
70  {
71  for(i = 0; i < strm->schema->grammarTable.grammar[g].count; i++)
72  {
73  tmp_rule = &((DynGrammarRule*) strm->schema->grammarTable.grammar[g].rule)[i];
74  if(tmp_rule->production != NULL)
75  EXIP_MFREE(tmp_rule->production);
76  }
78  }
79 
81  }
82 #else
84 #endif
85 
86 #if VALUE_CROSSTABLE_USE
87  // Freeing the value cross tables
88  {
89  Index j;
90  for(i = 0; i < strm->schema->uriTable.count; i++)
91  {
92  for(j = 0; j < strm->schema->uriTable.uri[i].lnTable.count; j++)
93  {
94  if(GET_LN_URI_IDS(strm->schema->uriTable, i, j).vxTable != NULL)
95  {
96  assert(GET_LN_URI_IDS(strm->schema->uriTable, i, j).vxTable->vx);
97  destroyDynArray(&GET_LN_URI_IDS(strm->schema->uriTable, i, j).vxTable->dynArray);
98  GET_LN_URI_IDS(strm->schema->uriTable, i, j).vxTable = NULL;
99  }
100  }
101  }
102  }
103 #endif
104 
105  // In case a default schema was used for this stream
107  {
108  // No schema-informed grammars. This is an empty EXIPSchema container that needs to be freed
109  // Freeing the string tables
110 
111  for(i = 0; i < strm->schema->uriTable.count; i++)
112  {
115  }
116 
119  if(strm->schema->simpleTypeTable.sType != NULL)
121  freeAllocList(&strm->schema->memList);
122  }
123  else
124  {
125  // Possible additions of string table entries must be removed
126  for(i = 0; i < strm->schema->uriTable.dynArray.chunkEntries; i++)
127  {
130  }
131 
132  for(i = strm->schema->uriTable.dynArray.chunkEntries; i < strm->schema->uriTable.count; i++)
133  {
136  }
137 
139  }
140  }
141 
142  // Hash tables are freed separately
143  // #DOCUMENT#
144 #if HASH_TABLE_USE
145  if(strm->valueTable.hashTbl != NULL)
147 #endif
148 
149  // Freeing the value table if present
150  if(strm->valueTable.value != NULL)
151  {
152  Index i;
153  for(i = 0; i < strm->valueTable.count; i++)
154  {
156  }
157 
159  }
160 
161  freeAllocList(&(strm->memList));
162 }
163 
165 {
166  struct allocBlock* tmpBlock = list->firstBlock;
167  struct allocBlock* rmBl;
168  unsigned int i = 0;
169  unsigned int allocLimitInBlock;
170 
171  while(tmpBlock != NULL)
172  {
173  if(tmpBlock->nextBlock != NULL)
174  allocLimitInBlock = ALLOCATION_ARRAY_SIZE;
175  else
176  allocLimitInBlock = list->currAllocSlot;
177 
178  for(i = 0; i < allocLimitInBlock; i++)
179  EXIP_MFREE(tmpBlock->allocation[i]);
180 
181  rmBl = tmpBlock;
182  tmpBlock = tmpBlock->nextBlock;
183  EXIP_MFREE(rmBl);
184  }
185 }