Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Display any possible format sequence when the user provided an invalid one
[simgrid.git] / tools / tesh2 / src / xalloc.c
1 #include <xalloc.h>\r
2 #include <htable.h>\r
3 \r
4 #include <stdlib.h>\r
5 #include <errno.h>\r
6 #include <string.h>\r
7 \r
8 #include <stdio.h>\r
9 \r
10 #include <portable.h>\r
11 #include <xbt/xbt_os_thread.h>\r
12 \r
13 \r
14 #ifndef __DEFAULT_BLOCK_CAPACITY\r
15 #define __DEFAULT_BLOCK_CAPACITY        ((int)512)\r
16 #endif\r
17 \r
18 #ifndef __DEFAULT_TABLE_SIZE\r
19 #define __DEFAULT_TABLE_SIZE            ((int)256)\r
20 #endif\r
21 \r
22 \r
23 #ifndef __BYTE_T_DEFINED\r
24         typedef unsigned char byte;\r
25         #define __BYTE_T_DEFINED\r
26 #endif\r
27 \r
28 \r
29 static int\r
30 _aborded = 0;\r
31 \r
32 /*\r
33 static htable_t\r
34 _heap = NULL;\r
35 \r
36 static xbt_os_mutex_t\r
37 _mutex = NULL;\r
38 \r
39 \r
40 static unsigned long \r
41 hfunc(const void* key)\r
42 {\r
43         return ((unsigned long)(void*)(unsigned long)key) >> 4;\r
44 }\r
45 \r
46 static int\r
47 cmp_key(const void* k1, const void* k2)\r
48 {\r
49         return (k1 == k2);\r
50 }\r
51 */\r
52 \r
53 int\r
54 xmalloc_mod_init(void)\r
55 {\r
56         /*allocator_node_t cur, next;\r
57         allocator_block_t block;\r
58         int block_capacity, type_size, node_size;\r
59         register int pos;\r
60         \r
61         if(_heap)\r
62                 return EALREADY;\r
63                 \r
64         _mutex = xbt_os_mutex_init();\r
65                 \r
66         \r
67         if(!(_heap = (htable_t)calloc(1, sizeof(s_htable_t))))\r
68                 return -1;\r
69         \r
70         if(!(_heap->content = (hassoc_t*)calloc(__DEFAULT_TABLE_SIZE, sizeof(hassoc_t))))\r
71         {\r
72                 free(_heap);\r
73                 return -1;\r
74         }\r
75         \r
76         if(!(_heap->allocator = (allocator_t)calloc(1,sizeof(s_allocator_t))))\r
77         {\r
78                 free(_heap->content);\r
79                 free(_heap);\r
80                 return -1;\r
81         }\r
82 \r
83         _heap->allocator->block_capacity = __DEFAULT_BLOCK_CAPACITY;\r
84         _heap->allocator->type_size = sizeof(s_hassoc_t);\r
85         \r
86         \r
87         next = NULL;\r
88         block_capacity = __DEFAULT_BLOCK_CAPACITY;\r
89         type_size =  sizeof(s_hassoc_t);\r
90         node_size = type_size + sizeof(s_allocator_node_t);\r
91         \r
92         if(!(block = (allocator_block_t)calloc(1,sizeof(s_allocator_block_t) + (block_capacity * node_size))))\r
93         {\r
94                 free(_heap->content);\r
95                 free(_heap->allocator);\r
96                 free(_heap);\r
97                 return -1;\r
98         }\r
99                 \r
100         block->next = _heap->allocator->head;\r
101         block->allocator =  _heap->allocator;\r
102         _heap->allocator->head = block;\r
103         \r
104         cur = (allocator_node_t)(((byte*)(block + 1)) + ((block_capacity - 1) * node_size));\r
105         \r
106         for(pos = block_capacity - 1; pos >= 0; pos--, cur = (allocator_node_t)(((byte*)next) - node_size))\r
107         {\r
108                 cur->next = next;\r
109                 cur->block = block;\r
110                 next = cur;\r
111         }\r
112         \r
113          _heap->allocator->free = _heap->allocator->first = next;\r
114         \r
115          _heap->allocator->capacity += block_capacity;\r
116          _heap->allocator->fn_finalize = NULL;\r
117    \r
118         \r
119         _heap->size = __DEFAULT_TABLE_SIZE;\r
120         _heap->fn_hfunc = hfunc;\r
121         _heap->fn_cmp_key = cmp_key;\r
122         _heap->fn_finalize = NULL;\r
123         \r
124         */\r
125         return 0;\r
126 }\r
127 \r
128 void\r
129 xabort(void)\r
130 {\r
131         _aborded = 1;\r
132         abort();\r
133 }\r
134 \r
135 int\r
136 xmalloc_mod_exit(void)\r
137 {\r
138         /*hassoc_t* content;\r
139         register hassoc_t hassoc;\r
140         allocator_block_t cur, next;\r
141         register int pos;\r
142         int size;\r
143         void* val;\r
144         \r
145         if(_heap)\r
146         {\r
147                 errno = EPERM;\r
148                 return -1;\r
149         }\r
150         \r
151         if(!htable_is_empty(_heap))\r
152         {\r
153                 if(!_aborded)\r
154                         fprintf(stderr,"WARNING : Memory leak detected - automaticaly release the memory...\n");\r
155                 else\r
156                         fprintf(stderr,"System aborted - Automaticaly release the memory...\n");        \r
157         }\r
158                 \r
159 \r
160         content = _heap->content;\r
161         size = _heap->size;\r
162 \r
163         for(pos = 0; pos < size; pos++)\r
164         {\r
165                 for(hassoc = content[pos]; hassoc; hassoc = hassoc->next)\r
166                 {\r
167                         val = (void*)hassoc->val;\r
168                         if(xfree(&val) < 0)\r
169                                 return -1;\r
170                 }\r
171         }\r
172         \r
173         free(_heap->content);\r
174         \r
175         cur = _heap->allocator->head;\r
176         \r
177         while(cur)\r
178         {\r
179                 next = cur->next;\r
180                 xfree(cur);\r
181                 cur = next;\r
182         }\r
183         \r
184         \r
185         free(_heap->allocator);\r
186         \r
187         free(_heap);\r
188         _heap = NULL;\r
189         \r
190         xbt_os_mutex_destroy(_mutex);\r
191         */\r
192         return 0;\r
193 }\r
194 \r
195 void*\r
196 xmalloc(unsigned int size)\r
197 {\r
198         byte* p;\r
199         \r
200         if(!(p = (byte*)calloc(size + 1, sizeof(byte))))\r
201                 return NULL;\r
202         \r
203         p[0] = XMAGIC;\r
204         \r
205         /*if(htable_set(_heap, p, p))\r
206         {\r
207                 xfree(p);\r
208                 return NULL;\r
209         }*/\r
210         \r
211         return p + 1;\r
212 }\r
213 \r
214 void*\r
215 xcalloc(unsigned int count, unsigned int size)\r
216 {\r
217         byte* p;\r
218         \r
219         if(!(p = (byte*)calloc((size * count) + 1, sizeof(byte))))\r
220                 return NULL;\r
221         \r
222         p[0] = XMAGIC;\r
223         \r
224         /*if(htable_set(_heap, p, p))\r
225         {\r
226                 xfree(p);\r
227                 return NULL;\r
228         }*/\r
229         \r
230         return p + 1;\r
231 }\r
232 \r
233 int\r
234 xfree(void* ptr)\r
235 {\r
236         byte* _ptr;\r
237         \r
238         if(!ptr)\r
239         {\r
240                 errno = EINVAL;\r
241                 return -1;\r
242         }\r
243         \r
244         _ptr = (byte*)ptr - 1;\r
245         \r
246         if(XMAGIC != _ptr[0])\r
247         {\r
248                 errno = EINVAL;\r
249                 return -1;\r
250         }\r
251         \r
252         /*if(!htable_lookup(_heap, _ptr))\r
253                 return -1;\r
254         \r
255         if(!htable_remove(_heap, _ptr))\r
256                 return -1;\r
257         */\r
258         \r
259         free(_ptr);\r
260         \r
261         return 0;\r
262 }\r
263 \r
264 void*\r
265 xrealloc(void *ptr, unsigned int size)\r
266 {\r
267         byte* _ptr,* _ptr_r ;\r
268         \r
269         if(!ptr)\r
270         {\r
271                 /* If ptr is NULL, realloc() is identical to a call\r
272          * to malloc() for size byte\r
273          */\r
274                  _ptr = (byte*)calloc(size + 1, sizeof(byte));\r
275         \r
276                 if(!_ptr)\r
277                         return NULL;\r
278         \r
279                 _ptr[0] = XMAGIC;\r
280                 \r
281                 return _ptr + 1;\r
282         }\r
283         \r
284         if(ptr && !size)\r
285         {\r
286                 /* If size is zero and ptr is not NULL, the allocated \r
287                  * object is freed.\r
288                  */\r
289                 xfree(ptr);\r
290                 return NULL;\r
291         }\r
292         \r
293         _ptr = (byte*)ptr - 1;\r
294         \r
295         if(XMAGIC != _ptr[0])\r
296         {\r
297                 errno = EINVAL;\r
298                 return NULL;\r
299         }\r
300         \r
301         if((_ptr_r = realloc(_ptr, size)))\r
302                 return _ptr_r + 1;\r
303         \r
304         return NULL;\r
305 }\r
306 \r
307 char*\r
308 xstrdup(const char* s1)\r
309 {\r
310         char* d1;\r
311         \r
312         if(!s1)\r
313                 return NULL;\r
314         \r
315         if((d1 = xmalloc((unsigned int)strlen(s1) +1 )))\r
316                 strcpy(d1, s1);\r
317         \r
318         return d1;\r
319 }\r
320 \r