exip
Alpha 0.5.4
Main Page
Related Pages
Modules
Data Structures
Files
File List
Globals
All
Data Structures
Files
Functions
Variables
Typedefs
Enumerations
Enumerator
Macros
Groups
Pages
src
common
src
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
23
errorCode
initAllocList
(
AllocList
* list)
24
{
25
list->
firstBlock
=
EXIP_MALLOC
(
sizeof
(
struct
allocBlock
));
26
if
(list->
firstBlock
==
NULL
)
27
return
EXIP_MEMORY_ALLOCATION_ERROR
;
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
{
40
if
(list->
currAllocSlot
==
ALLOCATION_ARRAY_SIZE
)
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
}
77
EXIP_MFREE
(strm->
schema
->
grammarTable
.
grammar
[g].
rule
);
78
}
79
80
strm->
schema
->
grammarTable
.
count
= strm->
schema
->
staticGrCount
;
81
}
82
#else
83
assert
(strm->
schema
->
grammarTable
.
count
== strm->
schema
->
staticGrCount
);
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
106
if
(strm->
schema
->
staticGrCount
<=
SIMPLE_TYPE_COUNT
)
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
{
113
destroyDynArray
(&strm->
schema
->
uriTable
.
uri
[i].
pfxTable
.
dynArray
);
114
destroyDynArray
(&strm->
schema
->
uriTable
.
uri
[i].
lnTable
.
dynArray
);
115
}
116
117
destroyDynArray
(&strm->
schema
->
uriTable
.
dynArray
);
118
destroyDynArray
(&strm->
schema
->
grammarTable
.
dynArray
);
119
if
(strm->
schema
->
simpleTypeTable
.
sType
!=
NULL
)
120
destroyDynArray
(&strm->
schema
->
simpleTypeTable
.
dynArray
);
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
{
128
strm->
schema
->
uriTable
.
uri
[i].
pfxTable
.
count
= strm->
schema
->
uriTable
.
uri
[i].
pfxTable
.
dynArray
.
chunkEntries
;
129
strm->
schema
->
uriTable
.
uri
[i].
lnTable
.
count
= strm->
schema
->
uriTable
.
uri
[i].
lnTable
.
dynArray
.
chunkEntries
;
130
}
131
132
for
(i = strm->
schema
->
uriTable
.
dynArray
.
chunkEntries
; i < strm->schema->uriTable.count; i++)
133
{
134
destroyDynArray
(&strm->
schema
->
uriTable
.
uri
[i].
pfxTable
.
dynArray
);
135
destroyDynArray
(&strm->
schema
->
uriTable
.
uri
[i].
lnTable
.
dynArray
);
136
}
137
138
strm->
schema
->
uriTable
.
count
= strm->
schema
->
uriTable
.
dynArray
.
chunkEntries
;
139
}
140
}
141
142
// Hash tables are freed separately
143
// #DOCUMENT#
144
#if HASH_TABLE_USE
145
if
(strm->
valueTable
.
hashTbl
!=
NULL
)
146
hashtable_destroy
(strm->
valueTable
.
hashTbl
);
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
{
155
EXIP_MFREE
(strm->
valueTable
.
value
[i].
valueStr
.
str
);
156
}
157
158
destroyDynArray
(&strm->
valueTable
.
dynArray
);
159
}
160
161
freeAllocList
(&(strm->
memList
));
162
}
163
164
void
freeAllocList
(
AllocList
* list)
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
}
Generated on Thu Nov 27 2014 10:56:09 for exip by
1.8.4