Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
0d054b46e6f2d70b97ca92c88584a3101d5d4066
[simgrid.git] / src / xbt / mmalloc / mm_legacy.c
1 /* Copyright (c) 2010. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 /* Redefine the classical malloc/free/realloc functions so that they fit well in the mmalloc framework */
8
9 #include "mmprivate.h"
10 #include "gras_config.h"
11
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_mm_legacy, xbt,
13                                 "Logging specific to mm_legacy in mmalloc");
14
15 static void *__mmalloc_current_heap = NULL;     /* The heap we are currently using. */
16
17 #include "xbt_modinter.h"
18
19 void *mmalloc_get_current_heap(void)
20 {
21   return __mmalloc_current_heap;
22 }
23
24 void mmalloc_set_current_heap(void *new_heap)
25 {
26   __mmalloc_current_heap = new_heap;
27 }
28
29 #ifdef MMALLOC_WANT_OVERIDE_LEGACY
30 void *malloc(size_t n)
31 {
32   void *mdp = __mmalloc_current_heap;
33 #ifdef HAVE_MMAP
34   if (!mdp)
35     mmalloc_preinit();
36 #endif
37   LOCK(mdp);
38   void *ret = mmalloc(mdp, n);
39   UNLOCK(mdp);
40
41   return ret;
42 }
43
44 void *calloc(size_t nmemb, size_t size)
45 {
46   size_t total_size = nmemb * size;
47   void *mdp = __mmalloc_current_heap;
48 #ifdef HAVE_MMAP
49   if (!mdp)
50     mmalloc_preinit();
51 #endif
52   LOCK(mdp);
53   void *ret = mmalloc(mdp, total_size);
54   UNLOCK(mdp);
55
56   /* Fill the allocated memory with zeroes to mimic calloc behaviour */
57   memset(ret, '\0', total_size);
58
59   return ret;
60 }
61
62 void *realloc(void *p, size_t s)
63 {
64   void *ret = NULL;
65   void *mdp = __mmalloc_current_heap;
66 #ifdef HAVE_MMAP
67   if (!mdp)
68     mmalloc_preinit();
69 #endif
70   LOCK(mdp);
71   if (s) {
72     if (p)
73       ret = mrealloc(mdp, p, s);
74     else
75       ret = mmalloc(mdp, s);
76   } else {
77     mfree(mdp, p);
78   }
79   UNLOCK(mdp);
80
81   return ret;
82 }
83
84 void free(void *p)
85 {
86   void *mdp = __mmalloc_current_heap;
87 #ifdef HAVE_GTNETS
88   if(!mdp) return;
89 #endif
90   LOCK(mdp);
91   mfree(mdp, p);
92   UNLOCK(mdp);
93 }
94 #endif
95
96 /* Make sure it works with md==NULL */
97
98 /* Safety gap from the heap's break address.
99  * Try to increase this first if you experience strange errors under
100  * valgrind. */
101 #define HEAP_OFFSET   (128UL<<20)
102
103 void *mmalloc_get_default_md(void)
104 {
105   xbt_assert(__mmalloc_default_mdp);
106   return __mmalloc_default_mdp;
107 }
108
109 static void mmalloc_fork_prepare(void)
110 {
111   struct mdesc* mdp = NULL;
112   if ((mdp =__mmalloc_default_mdp)){
113     while(mdp){
114       LOCK(mdp);
115       if(mdp->fd >= 0){
116         mdp->refcount++;
117       }
118       mdp = mdp->next_mdesc;
119     }
120   }
121 }
122
123 static void mmalloc_fork_parent(void)
124 {
125   struct mdesc* mdp = NULL;
126   if ((mdp =__mmalloc_default_mdp)){
127     while(mdp){
128       if(mdp->fd < 0)
129         UNLOCK(mdp);
130       mdp = mdp->next_mdesc;
131     }
132   }
133 }
134
135 static void mmalloc_fork_child(void)
136 {
137   struct mdesc* mdp = NULL;
138   if ((mdp =__mmalloc_default_mdp)){
139     while(mdp){
140       UNLOCK(mdp);
141       mdp = mdp->next_mdesc;
142     }
143   }
144 }
145
146 /* Initialize the default malloc descriptor. */
147 void mmalloc_preinit(void)
148 {
149   int res;
150   if (!__mmalloc_default_mdp) {
151     unsigned long mask = ~((unsigned long)getpagesize() - 1);
152     void *addr = (void*)(((unsigned long)sbrk(0) + HEAP_OFFSET) & mask);
153     __mmalloc_default_mdp = mmalloc_attach(-1, addr);
154     /* Fixme? only the default mdp in protected against forks */
155     res = xbt_os_thread_atfork(mmalloc_fork_prepare,
156                                mmalloc_fork_parent, mmalloc_fork_child);
157     if (res != 0)
158       THROWF(system_error,0,"xbt_os_thread_atfork() failed: return value %d",res);
159   }
160   xbt_assert(__mmalloc_default_mdp != NULL);
161 }
162
163 void mmalloc_postexit(void)
164 {
165   /* Do not detach the default mdp or ldl won't be able to free the memory it allocated since we're in memory */
166   //  mmalloc_detach(__mmalloc_default_mdp);
167   mmalloc_pre_detach(__mmalloc_default_mdp);
168 }
169
170 int mmalloc_compare_heap(void *h1, void *h2){
171
172   if(h1 == NULL && h2 == NULL){
173     XBT_DEBUG("Malloc descriptors null");
174     return 0;
175   }
176  
177
178   /* Heapstats */
179
180   struct mstats ms1 = mmstats(h1);
181   struct mstats ms2 = mmstats(h2);
182
183   if(ms1.bytes_total !=  ms2.bytes_total){
184     XBT_DEBUG("Different total size of the heap");
185     return 1;
186   }
187
188   if(ms1.chunks_used !=  ms2.chunks_used){
189     XBT_DEBUG("Different chunks allocated by the user");
190     return 1;
191   }
192
193   if(ms1.bytes_used !=  ms2.bytes_used){
194     XBT_DEBUG("Different byte total of user-allocated chunks");
195     return 1;
196   }
197
198   if(ms1.bytes_free !=  ms2.bytes_free){
199     XBT_DEBUG("Different byte total of chunks in the free list");
200     return 1;
201   }
202
203   if(ms1.chunks_free !=  ms2.chunks_free){
204     XBT_DEBUG("Different chunks in the free list");
205     return 1;
206   }
207
208   struct mdesc *mdp1, *mdp2;
209   mdp1 = MD_TO_MDP(h1);
210   mdp2 = MD_TO_MDP(h2);
211   
212   if(mmalloc_compare_mdesc(mdp1, mdp2))
213     return 1;
214   
215
216   return 0;
217 }
218
219 int mmalloc_compare_mdesc(struct mdesc *mdp1, struct mdesc *mdp2){
220
221   struct mdesc* mdp;
222   
223
224   if(mdp1->headersize != mdp2->headersize){
225     XBT_DEBUG("Different size of the file header for the mapped files");
226     return 1;
227   }
228
229   if(mdp1->refcount != mdp2->refcount){
230     XBT_DEBUG("Different number of processes that attached the heap");
231     return 1;
232   }
233  
234   if(strcmp(mdp1->magic, mdp2->magic) != 0){
235     XBT_DEBUG("Different magic number");
236     return 1;
237   }
238
239   if(mdp1->flags != mdp2->flags){
240     XBT_DEBUG("Different flags");
241     return 1;
242   }
243
244   if(mdp1->heapsize != mdp2->heapsize){
245     XBT_DEBUG("Different number of info entries");
246     return 1;
247   }
248
249   // XBT_DEBUG("Heap size : %Zu", mdp1->heapsize);
250
251   if(mdp1->heapbase != mdp2->heapbase){
252     XBT_DEBUG("Different first block of the heap");
253     return 1;
254   }
255
256
257   if(mdp1->heapindex != mdp2->heapindex){
258     XBT_DEBUG("Different index for the heap table");
259     return 1;
260   }
261
262   //XBT_DEBUG("Heap index : %Zu", mdp1->heapindex);
263
264   if(mdp1->base != mdp2->base){
265     XBT_DEBUG("Different base address of the memory region");
266     return 1;
267   }
268
269   if(mdp1->breakval != mdp2->breakval){
270     XBT_DEBUG("Different current location in the memory region");
271     return 1;
272   }
273
274   if(mdp1->top != mdp2->top){
275     XBT_DEBUG("Different end of the current location in the memory region");
276     return 1;
277   }
278   
279   if(mdp1->heaplimit != mdp2->heaplimit){
280     XBT_DEBUG("Different limit of valid info table indices");
281     return 1;
282   }
283
284   //XBT_DEBUG("Heap limit : %Zu", mdp1->heaplimit);
285
286
287   if(mdp1->fd != mdp2->fd){
288     XBT_DEBUG("Different file descriptor for the file to which this malloc heap is mapped");
289     return 1;
290   }
291
292   if(mdp1->saved_errno != mdp2->saved_errno){
293     XBT_DEBUG("Different errno");
294     return 1;
295   }
296
297   if(mdp1->version != mdp2->version){
298     XBT_DEBUG("Different version of the mmalloc package");
299     return 1;
300   }
301
302  
303   size_t block_free1, block_free2 , next_block_free, first_block_free ;
304   size_t i, j;
305   void *addr_block1, *addr_block2;
306
307  
308
309   /* Search index of the first free block */
310
311   block_free1 = mdp1->heapindex; 
312   block_free2 = mdp2->heapindex;
313
314   while(mdp1->heapinfo[block_free1].free.prev != 0){
315     block_free1 = mdp1->heapinfo[block_free1].free.prev;
316   }
317
318   while(mdp2->heapinfo[block_free2].free.prev != 0){
319     block_free2 = mdp1->heapinfo[block_free2].free.prev;
320   }
321
322   if(block_free1 !=  block_free2){
323     XBT_DEBUG("Different first free block");
324     return 1;
325   }
326
327   first_block_free = block_free1;
328
329   if(mdp1->heapinfo[block_free1].free.size != mdp2->heapinfo[block_free2].free.size){ 
330     XBT_DEBUG("Different size (in blocks) of the first free cluster");
331     return 1;
332   }
333
334   /* Check busy blocks (circular checking)*/
335
336   i = block_free1 + mdp1->heapinfo[block_free1].free.size;
337   next_block_free = mdp1->heapinfo[block_free1].free.next;
338
339   if(next_block_free == 0)
340     next_block_free = mdp1->heaplimit;
341
342
343   while(i != first_block_free){
344
345     while(i<next_block_free){
346
347       j = i;
348
349       if(mdp1->heapinfo[i].busy.type != mdp2->heapinfo[i].busy.type){
350         XBT_DEBUG("Different type of busy block");
351         return 1;
352       }else{
353         if(i == 0)
354           j++;
355         mdp = mdp1;
356         addr_block1 = ADDRESS(j);
357         mdp = mdp2;
358         addr_block2 = ADDRESS(j); 
359         switch(mdp1->heapinfo[i].busy.type){
360         case 0 :
361           if(mdp1->heapinfo[i].busy.info.size != mdp2->heapinfo[i].busy.info.size){
362             XBT_DEBUG("Different size of a large cluster");
363             return 1;
364           }else{
365             XBT_DEBUG("Block : %Zu", i);
366             if(memcmp(addr_block1, addr_block2, (mdp1->heapinfo[i].busy.info.size * BLOCKSIZE)) != 0){
367               XBT_DEBUG("Different data in block %Zu", i);
368               return 1;
369             } 
370           }
371           i = i+mdp1->heapinfo[i].busy.info.size;
372           break;
373         default :         
374           if(mdp1->heapinfo[i].busy.info.frag.nfree != mdp2->heapinfo[i].busy.info.frag.nfree){
375             XBT_DEBUG("Different free fragments in a fragmented block");
376             return 1;
377           }else{
378             if(mdp1->heapinfo[i].busy.info.frag.first != mdp2->heapinfo[i].busy.info.frag.first){
379               XBT_DEBUG("Different first free fragments of the block");
380               return 1; 
381             }else{
382               if(memcmp(addr_block1, addr_block2, BLOCKSIZE) != 0){
383                 XBT_DEBUG("Different data in fragmented block %Zu", i);
384                 return 1;
385               } 
386             }
387           }
388           i++;
389           break;
390         }
391       }
392     }
393
394      
395     block_free1 = mdp1->heapinfo[block_free1].free.next;
396     next_block_free = mdp1->heapinfo[block_free1].free.next;
397     if(i != first_block_free){
398       if(block_free1 != 0)
399         i = block_free1 + mdp1->heapinfo[block_free1].free.size;
400       else
401         i = 0;
402     }
403       
404   }
405   
406   return 0;   
407   
408   
409 }
410
411  
412   
413   
414