forked from UnitexGramLab/unitex-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAbstractAllocator.cpp
284 lines (237 loc) · 8.38 KB
/
AbstractAllocator.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/*
* Unitex
*
* Copyright (C) 2001-2017 Université Paris-Est Marne-la-Vallée <unitex@univ-mlv.fr>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
*
*/
/*
* File created and contributed by Gilles Vollant (Ergonotics SAS)
* as part of an UNITEX optimization and reliability effort
*
* additional information: http://www.ergonotics.com/unitex-contribution/
* contact : unitex-contribution@ergonotics.com
*
*/
#include <string.h>
#include <memory.h>
#include "Error.h"
#include "AbstractAllocator.h"
#include "AbstractAllocatorPlugCallback.h"
//#ifndef HAS_UNITEX_NAMESPACE
//#define HAS_UNITEX_NAMESPACE 1
//#endif
//namespace unitex {
struct AllocatorSpace {
t_allocator_func_array func_array;
void* privateAllocatorSpacePtr;
} ;
struct List_AllocatorSpace {
AllocatorSpace aas;
List_AllocatorSpace* next;
} ;
struct List_AllocatorSpace* p_allocator_info_list=NULL;
UNITEX_FUNC int UNITEX_CALL AddAllocatorSpace(const t_allocator_func_array* func_array,void* privateAllocatorSpacePtr)
{
struct List_AllocatorSpace* new_item;
new_item = (struct List_AllocatorSpace*)malloc(sizeof(struct List_AllocatorSpace));
if (new_item == NULL)
return 0;
new_item->aas.func_array = *func_array;
new_item->aas.privateAllocatorSpacePtr = privateAllocatorSpacePtr;
new_item->next = NULL;
if (p_allocator_info_list == NULL)
p_allocator_info_list = new_item;
else {
struct List_AllocatorSpace* tmp = p_allocator_info_list;
while ((tmp->next) != NULL)
tmp = tmp->next;
tmp->next = new_item;
}
if ((new_item->aas.func_array.fnc_Init_AllocatorSpace) != NULL)
(*(new_item->aas.func_array.fnc_Init_AllocatorSpace))(new_item->aas.privateAllocatorSpacePtr);
return 1;
}
UNITEX_FUNC int UNITEX_CALL RemoveAllocatorSpace(const t_allocator_func_array* func_array,void* privateAllocatorSpacePtr)
{
struct List_AllocatorSpace* tmp = p_allocator_info_list;
struct List_AllocatorSpace* tmp_previous = NULL;
while (tmp != NULL)
{
if ((memcmp(&tmp->aas.func_array,func_array,sizeof(t_allocator_func_array))==0) &&
(tmp->aas.privateAllocatorSpacePtr == privateAllocatorSpacePtr))
{
if (tmp_previous == NULL)
p_allocator_info_list = tmp->next;
else
tmp_previous->next = tmp->next;
if ((tmp->aas.func_array.fnc_Uninit_AllocatorSpace) != NULL)
(*(tmp->aas.func_array.fnc_Uninit_AllocatorSpace))(tmp->aas.privateAllocatorSpacePtr);
free(tmp);
return 1;
}
tmp_previous = tmp;
tmp = tmp->next;
}
return 0;
}
UNITEX_FUNC int UNITEX_CALL GetNbAllocatorSpaceInstalled()
{
int count=0;
struct List_AllocatorSpace* tmp = p_allocator_info_list;
while (tmp != NULL)
{
count++;
tmp = tmp->next;
}
return count;
}
const AllocatorSpace * GetAllocatorSpaceForParam(const char*creator,int flagAllocator,size_t expected_size_item,const void* private_create_ptr)
{
const struct List_AllocatorSpace* tmp = p_allocator_info_list;
const AllocatorSpace * best_aas = NULL;
int best_priority = 0;
while (tmp != NULL)
{
const AllocatorSpace * test_aas = &(tmp->aas);
int cur_priority = tmp->aas.func_array.fnc_is_param_allocator_compatible(creator,flagAllocator,expected_size_item,private_create_ptr,tmp->aas.privateAllocatorSpacePtr) ;
if ((cur_priority>0) && (cur_priority>best_priority))
{
best_aas = test_aas;
best_priority = cur_priority;
}
tmp = tmp->next;
}
return best_aas;
}
Abstract_allocator build_Abstract_allocator_from_AllocatorSpace(const t_allocator_func_array *p_func_array,void* privateAllocatorSpacePtr,const char*creator,int creation_flagAllocator,size_t expected_size_item,const void* private_create_ptr)
{
Abstract_allocator aas;
aas=(abstract_allocator*)malloc(sizeof(abstract_allocator));
if (aas == NULL)
return NULL;
memset(&(aas->pub),0,sizeof(abstract_allocator_info_public_with_allocator));
if (p_func_array->fnc_create_abstract_allocator(&(aas->pub),creator,creation_flagAllocator,expected_size_item,private_create_ptr,privateAllocatorSpacePtr) == 0)
{
free(aas);
return NULL;
}
aas->size_abstract_allocator = sizeof(abstract_allocator);
aas->fnc_delete_abstract_allocator = p_func_array->fnc_delete_abstract_allocator;
aas->privateAllocatorSpacePtr = privateAllocatorSpacePtr;
aas->creation_flag = creation_flagAllocator;
aas->expected_creation_size = expected_size_item;
aas->creator = strdup(creator);
return aas;
}
UNITEX_FUNC Abstract_allocator UNITEX_CALL BuildAbstractAllocatorFromSpecificAllocatorSpace(const t_allocator_func_array *p_func_array,
void* privateAllocatorSpacePtr,const char*creator,int flagAllocator,size_t expected_size_item,const void* private_create_ptr)
{
return build_Abstract_allocator_from_AllocatorSpace(p_func_array,privateAllocatorSpacePtr,creator,flagAllocator,expected_size_item,private_create_ptr);
}
Abstract_allocator create_abstract_allocator(const char*creator,int flagAllocator,size_t expected_size_item,const void* private_create_ptr)
{
const AllocatorSpace * paas = GetAllocatorSpaceForParam(creator,flagAllocator,expected_size_item,private_create_ptr) ;
if (paas == NULL)
return NULL;
return build_Abstract_allocator_from_AllocatorSpace(&(paas->func_array),paas->privateAllocatorSpacePtr,creator,flagAllocator,expected_size_item,private_create_ptr);
}
void close_abstract_allocator(Abstract_allocator aa)
{
if (aa != NULL)
{
abstract_allocator* aas = aa;
if (aas->fnc_delete_abstract_allocator != NULL)
{
aas->fnc_delete_abstract_allocator(&(aa->pub),aas->privateAllocatorSpacePtr);
}
if (aas->creator != NULL)
free(aas->creator);
free(aa);
}
}
int get_allocator_flag(Abstract_allocator aa)
{
int ret=0;
if (aa != NULL)
{
if (aa->pub.fnc_get_flag_allocator != NULL)
ret = (aa->pub.fnc_get_flag_allocator)(aa->pub.abstract_allocator_ptr);
}
return ret;
}
int get_allocator_creation_flag(Abstract_allocator aa)
{
int ret=0;
if (aa != NULL)
{
ret = (aa->creation_flag);
}
return ret;
}
size_t get_allocator_expected_creation_size(Abstract_allocator aa)
{
size_t ret=0;
if (aa != NULL)
{
ret = (aa->expected_creation_size);
}
return ret;
}
int get_allocator_statistic_info(Abstract_allocator aa,int iStatNum,size_t*p_value)
{
int ret=0;
if (aa != NULL)
{
if (aa->pub.fnc_get_statistic_allocator_info != NULL)
ret = (aa->pub.fnc_get_statistic_allocator_info)(iStatNum,p_value,aa->pub.abstract_allocator_ptr);
}
return ret;
}
int clean_allocator(Abstract_allocator aa)
{
int flag=0;
if (aa != NULL)
{
if (aa->pub.fnc_get_flag_allocator != NULL)
flag = (aa->pub.fnc_get_flag_allocator)(aa->pub.abstract_allocator_ptr);
if ((flag & AllocatorCleanPresent) != 0)
if (aa->pub.fnc_clean_allocator != NULL) {
(aa->pub.fnc_clean_allocator)(aa->pub.abstract_allocator_ptr);
return 1;
}
}
return 0;
}
const char* get_allocator_creator(Abstract_allocator aa)
{
const char* ret=NULL;
if (aa != NULL)
{
ret = (aa->creator);
}
return ret;
}
abstract_allocator_info_public_with_allocator* get_abstract_allocator_info_public_with_allocator(Abstract_allocator aa)
{
abstract_allocator_info_public_with_allocator* ret=NULL;
if (aa != NULL)
{
ret = &(aa->pub);
}
return ret;
}
//} // namespace unitex