Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
3b581c7611c787b09f8798f748b87d3047a4d78d
[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     if (p)
78       mfree(mdp, p);
79   }
80   UNLOCK(mdp);
81
82   return ret;
83 }
84
85 void free(void *p)
86 {
87   void *mdp = __mmalloc_current_heap;
88 #ifdef HAVE_GTNETS
89   if(!mdp) return;
90 #endif
91   LOCK(mdp);
92   mfree(mdp, p);
93   UNLOCK(mdp);
94 }
95 #endif
96
97 /* Make sure it works with md==NULL */
98
99 /* Safety gap from the heap's break address.
100  * Try to increase this first if you experience strange errors under
101  * valgrind. */
102 #define HEAP_OFFSET   (128UL<<20)
103
104 void *mmalloc_get_default_md(void)
105 {
106   xbt_assert(__mmalloc_default_mdp);
107   return __mmalloc_default_mdp;
108 }
109
110 static void mmalloc_fork_prepare(void)
111 {
112   struct mdesc* mdp = NULL;
113   if ((mdp =__mmalloc_default_mdp)){
114     while(mdp){
115       LOCK(mdp);
116       if(mdp->fd >= 0){
117         mdp->refcount++;
118       }
119       mdp = mdp->next_mdesc;
120     }
121   }
122 }
123
124 static void mmalloc_fork_parent(void)
125 {
126   struct mdesc* mdp = NULL;
127   if ((mdp =__mmalloc_default_mdp)){
128     while(mdp){
129       if(mdp->fd < 0)
130         UNLOCK(mdp);
131       mdp = mdp->next_mdesc;
132     }
133   }
134 }
135
136 static void mmalloc_fork_child(void)
137 {
138   struct mdesc* mdp = NULL;
139   if ((mdp =__mmalloc_default_mdp)){
140     while(mdp){
141       UNLOCK(mdp);
142       mdp = mdp->next_mdesc;
143     }
144   }
145 }
146
147 /* Initialize the default malloc descriptor. */
148 void mmalloc_preinit(void)
149 {
150   int res;
151   if (!__mmalloc_default_mdp) {
152     unsigned long mask = ~((unsigned long)getpagesize() - 1);
153     void *addr = (void*)(((unsigned long)sbrk(0) + HEAP_OFFSET) & mask);
154     __mmalloc_default_mdp = mmalloc_attach(-1, addr);
155     /* Fixme? only the default mdp in protected against forks */
156     res = xbt_os_thread_atfork(mmalloc_fork_prepare,
157                                mmalloc_fork_parent, mmalloc_fork_child);
158     if (res != 0)
159       THROWF(system_error,0,"xbt_os_thread_atfork() failed: return value %d",res);
160   }
161   xbt_assert(__mmalloc_default_mdp != NULL);
162 }
163
164 void mmalloc_postexit(void)
165 {
166   /* Do not detach the default mdp or ldl won't be able to free the memory it allocated since we're in memory */
167   //  mmalloc_detach(__mmalloc_default_mdp);
168   mmalloc_pre_detach(__mmalloc_default_mdp);
169 }
170
171 int mmalloc_compare_heap(void *h1, void *h2){
172
173   if(h1 == NULL && h2 == NULL){
174     XBT_DEBUG("Malloc descriptors null");
175     return 0;
176   }
177  
178
179   /* Heapstats */
180
181   struct mstats ms1 = mmstats(h1);
182   struct mstats ms2 = mmstats(h2);
183
184   if(ms1.bytes_total !=  ms2.bytes_total){
185     XBT_DEBUG("Different total size of the heap");
186     return 1;
187   }
188
189   if(ms1.chunks_used !=  ms2.chunks_used){
190     XBT_DEBUG("Different chunks allocated by the user");
191     return 1;
192   }
193
194   if(ms1.bytes_used !=  ms2.bytes_used){
195     XBT_DEBUG("Different byte total of user-allocated chunks");
196     return 1;
197   }
198
199   if(ms1.bytes_free !=  ms2.bytes_free){
200     XBT_DEBUG("Different byte total of chunks in the free list");
201     return 1;
202   }
203
204   if(ms1.chunks_free !=  ms2.chunks_free){
205     XBT_DEBUG("Different chunks in the free list");
206     return 1;
207   }
208
209   struct mdesc *mdp1, *mdp2;
210   mdp1 = MD_TO_MDP(h1);
211   mdp2 = MD_TO_MDP(h2);
212   
213   if(mmalloc_compare_mdesc(mdp1, mdp2))
214     return 1;
215   
216
217   return 0;
218 }
219
220 int mmalloc_compare_mdesc(struct mdesc *mdp1, struct mdesc *mdp2){
221
222    if(mdp1->headersize != mdp2->headersize){
223     XBT_DEBUG("Different size of the file header for the mapped files");
224     return 1;
225   }
226
227   if(mdp1->refcount != mdp2->refcount){
228     XBT_DEBUG("Different number of processes that attached the heap");
229     return 1;
230   }
231  
232   if(strcmp(mdp1->magic, mdp2->magic) != 0){
233     XBT_DEBUG("Different magic number");
234     return 1;
235   }
236
237   if(mdp1->flags != mdp2->flags){
238     XBT_DEBUG("Different flags");
239     return 1;
240   }
241
242   if(mdp1->heapsize != mdp2->heapsize){
243     XBT_DEBUG("Different number of info entries");
244     return 1;
245   }
246
247   //XBT_DEBUG("Heap size : %d", mdp1->heapsize);
248
249   if(mdp1->heapbase != mdp2->heapbase){
250     XBT_DEBUG("Different first block of the heap");
251     return 1;
252   }
253
254   if(mdp1->heapindex != mdp2->heapindex){
255     XBT_DEBUG("Different index for the heap table");
256     return 1;
257   }
258
259   XBT_DEBUG("Heap index : %d", mdp1->heapindex);
260
261   if(mdp1->base != mdp2->base){
262     XBT_DEBUG("Different base address of the memory region");
263     return 1;
264   }
265
266   if(mdp1->breakval != mdp2->breakval){
267     XBT_DEBUG("Different current location in the memory region");
268     return 1;
269   }
270
271   if(mdp1->top != mdp2->top){
272     XBT_DEBUG("Different end of the current location in the memory region");
273     return 1;
274   }
275   
276   if(mdp1->heaplimit != mdp2->heaplimit){
277     XBT_DEBUG("Different limit of valid info table indices");
278     return 1;
279   }
280
281   //XBT_DEBUG("Heap limit : %d", mdp1->heaplimit);
282
283
284   if(mdp1->fd != mdp2->fd){
285     XBT_DEBUG("Different file descriptor for the file to which this malloc heap is mapped");
286     return 1;
287   }
288
289   if(mdp1->saved_errno != mdp2->saved_errno){
290     XBT_DEBUG("Different errno");
291     return 1;
292   }
293
294   if(mdp1->version != mdp2->version){
295     XBT_DEBUG("Different version of the mmalloc package");
296     return 1;
297   }
298
299  
300   size_t block_free1, start1, block_free2 , start2, block_busy1, block_busy2 ;
301   unsigned int i;
302
303   start1 = block_free1 = mdp1->heapindex; 
304   start2 = block_free2 = mdp2->heapindex;
305   block_busy1 = start1 + mdp1->heapinfo[start1].free.size;
306   block_busy2 = start2 + mdp2->heapinfo[start2].free.size;
307
308   //XBT_DEBUG("Block busy : %d - %d", block_busy1, block_busy2);
309
310
311   if(mdp1->heapinfo[start1].free.size != mdp2->heapinfo[start2].free.size){ // <=> check block_busy
312     
313     XBT_DEBUG("Different size (in blocks) of a free cluster");
314     return 1;
315
316   }else{
317
318     if(mdp1->heapinfo[start1].free.next != mdp2->heapinfo[start1].free.next){
319
320       XBT_DEBUG("Different index of next free cluster");
321       return 1;
322
323     }else{
324    
325       for(i=block_busy1 ; i<mdp1->heapinfo[start1].free.next ; i++){
326         if(mdp1->heapinfo[i].busy.type != mdp2->heapinfo[i].busy.type){
327           XBT_DEBUG("Different type of busy block");
328           return 1;
329         }else{
330           switch(mdp1->heapinfo[i].busy.type){
331           case 0 :
332             if(mdp1->heapinfo[i].busy.info.size != mdp2->heapinfo[i].busy.info.size){
333               XBT_DEBUG("Different size of a large cluster");
334               return 1;
335             }
336             break;
337           default :       
338             if(mdp1->heapinfo[i].busy.info.frag.nfree != mdp2->heapinfo[i].busy.info.frag.nfree){
339               XBT_DEBUG("Different free fragments in a fragmented block");
340               return 1;
341             }else{
342               if(mdp1->heapinfo[i].busy.info.frag.first != mdp2->heapinfo[i].busy.info.frag.first){
343                 XBT_DEBUG("Different first free fragments of the block");
344                 return 1; 
345               }
346             }
347             break;
348           }
349         } 
350       }
351     }
352
353     block_free1 = mdp1->heapinfo[start1].free.next;
354     block_free2 = mdp2->heapinfo[start2].free.next;
355
356     //XBT_DEBUG("Index of next free cluster : %d", block_free1);
357
358     while((block_free1 != start1) && (block_free2 != start2)){ 
359
360       block_busy1 = block_free1 + mdp1->heapinfo[block_free1].free.size;
361       block_busy2 = block_free2 + mdp2->heapinfo[block_free2].free.size;
362
363       if(block_busy1 != block_busy2){
364         XBT_DEBUG("Different index of busy block");
365         return 1;
366       }else{
367
368         //XBT_DEBUG("Index of next busy block : %d - %d", block_busy1, block_busy2);
369         //XBT_DEBUG("Index of next free cluster : %d", mdp1->heapinfo[block_free1].free.next);
370         
371         for(i=block_busy1 ; i<mdp1->heapinfo[block_free1].free.next ; i++){
372           if(mdp1->heapinfo[i].busy.type != mdp2->heapinfo[i].busy.type){
373             XBT_DEBUG("Different type of busy block");
374             return 1;
375           }else{
376             switch(mdp1->heapinfo[i].busy.type){
377             case 0 :
378               if(mdp1->heapinfo[i].busy.info.size != mdp2->heapinfo[i].busy.info.size){
379                 XBT_DEBUG("Different size of a large cluster");
380                 return 1;
381               }
382               break;
383             default :     
384               if(mdp1->heapinfo[i].busy.info.frag.nfree != mdp2->heapinfo[i].busy.info.frag.nfree){
385                 XBT_DEBUG("Different free fragments in a fragmented block");
386                 return 1;
387               }else{
388                 if(mdp1->heapinfo[i].busy.info.frag.first != mdp2->heapinfo[i].busy.info.frag.first){
389                   XBT_DEBUG("Different first free fragments of the block");
390                   return 1; 
391                 }
392               }
393               break;
394             }
395           } 
396         }
397       }
398
399       block_free1 = mdp1->heapinfo[block_free1].free.next;
400       block_free2 = mdp2->heapinfo[block_free2].free.next;
401       
402     } 
403     
404   }
405   
406   
407   return 0;   
408   
409   
410   
411 }
412
413  
414   
415   
416