Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
we don't need any stats about the amount of free chunks and such
[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 #include <math.h>
12
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_mm_legacy, xbt,
14                                 "Logging specific to mm_legacy in mmalloc");
15
16 /* The mmalloc() package can use a single implicit malloc descriptor
17    for mmalloc/mrealloc/mfree operations which do not supply an explicit
18    descriptor.  This allows mmalloc() to provide
19    backwards compatibility with the non-mmap'd version. */
20 struct mdesc *__mmalloc_default_mdp;
21
22
23 static void *__mmalloc_current_heap = NULL;     /* The heap we are currently using. */
24
25 #include "xbt_modinter.h"
26
27 void *mmalloc_get_current_heap(void)
28 {
29   return __mmalloc_current_heap;
30 }
31
32 void mmalloc_set_current_heap(void *new_heap)
33 {
34   __mmalloc_current_heap = new_heap;
35 }
36
37 #ifdef MMALLOC_WANT_OVERIDE_LEGACY
38 void *malloc(size_t n)
39 {
40   void *mdp = __mmalloc_current_heap;
41 #ifdef HAVE_MMAP
42   if (!mdp)
43     mmalloc_preinit();
44 #endif
45   LOCK(mdp);
46   void *ret = mmalloc(mdp, n);
47   UNLOCK(mdp);
48
49   return ret;
50 }
51
52 void *calloc(size_t nmemb, size_t size)
53 {
54   size_t total_size = nmemb * size;
55   void *mdp = __mmalloc_current_heap;
56 #ifdef HAVE_MMAP
57   if (!mdp)
58     mmalloc_preinit();
59 #endif
60   LOCK(mdp);
61   void *ret = mmalloc(mdp, total_size);
62   UNLOCK(mdp);
63
64   /* Fill the allocated memory with zeroes to mimic calloc behaviour */
65   memset(ret, '\0', total_size);
66
67   return ret;
68 }
69
70 void *realloc(void *p, size_t s)
71 {
72   void *ret = NULL;
73   void *mdp = __mmalloc_current_heap;
74 #ifdef HAVE_MMAP
75   if (!mdp)
76     mmalloc_preinit();
77 #endif
78   LOCK(mdp);
79   if (s) {
80     if (p)
81       ret = mrealloc(mdp, p, s);
82     else
83       ret = mmalloc(mdp, s);
84   } else {
85     mfree(mdp, p);
86   }
87   UNLOCK(mdp);
88
89   return ret;
90 }
91
92 void free(void *p)
93 {
94   void *mdp = __mmalloc_current_heap;
95 #ifdef HAVE_GTNETS
96   if(!mdp) return;
97 #endif
98   LOCK(mdp);
99   mfree(mdp, p);
100   UNLOCK(mdp);
101 }
102 #endif
103
104 /* Make sure it works with md==NULL */
105
106 /* Safety gap from the heap's break address.
107  * Try to increase this first if you experience strange errors under
108  * valgrind. */
109 #define HEAP_OFFSET   (128UL<<20)
110
111 void *mmalloc_get_default_md(void)
112 {
113   xbt_assert(__mmalloc_default_mdp);
114   return __mmalloc_default_mdp;
115 }
116
117 static void mmalloc_fork_prepare(void)
118 {
119   struct mdesc* mdp = NULL;
120   if ((mdp =__mmalloc_default_mdp)){
121     while(mdp){
122       LOCK(mdp);
123       if(mdp->fd >= 0){
124         mdp->refcount++;
125       }
126       mdp = mdp->next_mdesc;
127     }
128   }
129 }
130
131 static void mmalloc_fork_parent(void)
132 {
133   struct mdesc* mdp = NULL;
134   if ((mdp =__mmalloc_default_mdp)){
135     while(mdp){
136       if(mdp->fd < 0)
137         UNLOCK(mdp);
138       mdp = mdp->next_mdesc;
139     }
140   }
141 }
142
143 static void mmalloc_fork_child(void)
144 {
145   struct mdesc* mdp = NULL;
146   if ((mdp =__mmalloc_default_mdp)){
147     while(mdp){
148       UNLOCK(mdp);
149       mdp = mdp->next_mdesc;
150     }
151   }
152 }
153
154 /* Initialize the default malloc descriptor. */
155 void mmalloc_preinit(void)
156 {
157   int res;
158   if (!__mmalloc_default_mdp) {
159     unsigned long mask = ~((unsigned long)getpagesize() - 1);
160     void *addr = (void*)(((unsigned long)sbrk(0) + HEAP_OFFSET) & mask);
161     __mmalloc_default_mdp = mmalloc_attach(-1, addr);
162     /* Fixme? only the default mdp in protected against forks */
163     res = xbt_os_thread_atfork(mmalloc_fork_prepare,
164                                mmalloc_fork_parent, mmalloc_fork_child);
165     if (res != 0)
166       THROWF(system_error,0,"xbt_os_thread_atfork() failed: return value %d",res);
167   }
168   xbt_assert(__mmalloc_default_mdp != NULL);
169 }
170
171 void mmalloc_postexit(void)
172 {
173   /* Do not detach the default mdp or ldl won't be able to free the memory it allocated since we're in memory */
174   //  mmalloc_detach(__mmalloc_default_mdp);
175   mmalloc_pre_detach(__mmalloc_default_mdp);
176 }
177
178 int mmalloc_compare_heap(void *h1, void *h2, void *std_heap_addr){
179
180   if(h1 == NULL && h2 == NULL){
181     XBT_DEBUG("Malloc descriptors null");
182     return 0;
183   }
184
185   /* Heapstats */
186
187   struct mdesc *mdp1, *mdp2;
188   mdp1 = MD_TO_MDP(h1);
189   mdp2 = MD_TO_MDP(h2);
190
191   int errors = mmalloc_compare_mdesc(mdp1, mdp2, std_heap_addr);
192
193   return (errors > 0);
194
195 }
196
197 int mmalloc_compare_mdesc(struct mdesc *mdp1, struct mdesc *mdp2, void *std_heap_addr){
198
199   int errors = 0;
200
201   if(mdp1->headersize != mdp2->headersize){
202     if(XBT_LOG_ISENABLED(xbt_mm_legacy, xbt_log_priority_debug)){
203       XBT_DEBUG("Different size of the file header for the mapped files");
204       errors++;
205     }else{
206       return 1;
207     }
208   }
209
210   if(mdp1->refcount != mdp2->refcount){
211     if(XBT_LOG_ISENABLED(xbt_mm_legacy, xbt_log_priority_debug)){
212       XBT_DEBUG("Different number of processes that attached the heap");
213       errors++;
214     }else{
215       return 1;
216     }
217   }
218  
219   if(strcmp(mdp1->magic, mdp2->magic) != 0){
220     if(XBT_LOG_ISENABLED(xbt_mm_legacy, xbt_log_priority_debug)){
221       XBT_DEBUG("Different magic number");
222       errors++;
223     }else{
224       return 1;
225     }
226   }
227
228   if(mdp1->flags != mdp2->flags){
229     if(XBT_LOG_ISENABLED(xbt_mm_legacy, xbt_log_priority_debug)){
230       XBT_DEBUG("Different flags");
231       errors++;
232     }else{
233       return 1;
234     }
235   }
236
237   if(mdp1->heapsize != mdp2->heapsize){
238     if(XBT_LOG_ISENABLED(xbt_mm_legacy, xbt_log_priority_debug)){
239       XBT_DEBUG("Different number of info entries");
240       errors++;
241     }else{
242       return 1;
243     }
244   }
245
246   //XBT_DEBUG("Heap size : %zu", mdp1->heapsize);
247
248   if(mdp1->heapbase != mdp2->heapbase){
249     if(XBT_LOG_ISENABLED(xbt_mm_legacy, xbt_log_priority_debug)){
250       XBT_DEBUG("Different first block of the heap");
251       errors++;
252     }else{
253       return 1;
254     }
255   }
256
257
258   if(mdp1->heapindex != mdp2->heapindex){
259     if(XBT_LOG_ISENABLED(xbt_mm_legacy, xbt_log_priority_debug)){
260       XBT_DEBUG("Different index for the heap table : %zu - %zu", mdp1->heapindex, mdp2->heapindex);
261       errors++;
262     }else{
263       return 1;
264     }
265   }
266
267   //XBT_DEBUG("Heap index : %zu", mdp1->heapindex);
268
269   if(mdp1->base != mdp2->base){
270     if(XBT_LOG_ISENABLED(xbt_mm_legacy, xbt_log_priority_debug)){
271       XBT_DEBUG("Different base address of the memory region");
272       errors++;
273     }else{
274       return 1;
275     }
276   }
277
278   if(mdp1->breakval != mdp2->breakval){
279     if(XBT_LOG_ISENABLED(xbt_mm_legacy, xbt_log_priority_debug)){
280       XBT_DEBUG("Different current location in the memory region");
281       errors++;
282     }else{
283       return 1;
284     }
285   }
286
287   if(mdp1->top != mdp2->top){
288     if(XBT_LOG_ISENABLED(xbt_mm_legacy, xbt_log_priority_debug)){
289       XBT_DEBUG("Different end of the current location in the memory region");
290       errors++;
291     }else{
292       return 1;
293     }
294   }
295   
296   if(mdp1->heaplimit != mdp2->heaplimit){
297     if(XBT_LOG_ISENABLED(xbt_mm_legacy, xbt_log_priority_debug)){
298       XBT_DEBUG("Different limit of valid info table indices");
299       errors++;
300     }else{
301       return 1;
302     }
303   }
304
305   if(mdp1->fd != mdp2->fd){
306     if(XBT_LOG_ISENABLED(xbt_mm_legacy, xbt_log_priority_debug)){
307       XBT_DEBUG("Different file descriptor for the file to which this malloc heap is mapped");
308       errors++;
309     }else{
310       return 1;
311     }
312   }
313
314    if(mdp1->version != mdp2->version){
315     if(XBT_LOG_ISENABLED(xbt_mm_legacy, xbt_log_priority_debug)){
316       XBT_DEBUG("Different version of the mmalloc package");
317       errors++;
318     }else{
319       return 1;
320     }
321   }
322
323  
324   size_t block_free1, block_free2 , next_block_free, first_block_free, block_free ;
325   size_t i, j;
326   void *addr_block1, *addr_block2;
327   size_t frag_size;
328  
329
330   /* Search index of the first free block */
331
332   block_free1 = mdp1->heapindex; 
333   block_free2 = mdp2->heapindex;
334
335   while(mdp1->heapinfo[block_free1].free.prev != 0){
336     block_free1 = mdp1->heapinfo[block_free1].free.prev;
337   }
338
339   while(mdp2->heapinfo[block_free2].free.prev != 0){
340     block_free2 = mdp1->heapinfo[block_free2].free.prev;
341   }
342
343   if(block_free1 !=  block_free2){
344     if(XBT_LOG_ISENABLED(xbt_mm_legacy, xbt_log_priority_debug)){
345       XBT_DEBUG("Different first free block");
346       errors++;
347     }else{
348       return 1;
349     }
350   }
351
352   first_block_free = block_free1;
353
354   if(mdp1->heapinfo[first_block_free].free.size != mdp2->heapinfo[first_block_free].free.size){ 
355     if(XBT_LOG_ISENABLED(xbt_mm_legacy, xbt_log_priority_debug)){
356       XBT_DEBUG("Different size (in blocks) of the first free cluster");
357       errors++;
358     }else{
359       return 1;
360     }
361   }
362
363   /* Check busy blocks (circular checking)*/
364
365   i = first_block_free + mdp1->heapinfo[first_block_free].free.size;
366
367   if(mdp1->heapinfo[first_block_free].free.next != mdp2->heapinfo[first_block_free].free.next){
368     if(XBT_LOG_ISENABLED(xbt_mm_legacy, xbt_log_priority_debug)){
369       XBT_DEBUG("Different next block free");
370       errors++;
371     }else{
372       return 1;
373     }
374   }
375   
376   block_free = first_block_free;
377   next_block_free = mdp1->heapinfo[first_block_free].free.next;
378
379   if(next_block_free == 0)
380     next_block_free = mdp1->heaplimit;
381
382   while(i != first_block_free){
383
384     while(i<next_block_free){
385
386       if(mdp1->heapinfo[i].busy.type != mdp2->heapinfo[i].busy.type){
387         if(XBT_LOG_ISENABLED(xbt_mm_legacy, xbt_log_priority_debug)){
388           XBT_DEBUG("Different type of busy block");
389           errors++;
390         }else{
391           return 1;
392         }
393       }else{
394
395         addr_block1 = (char *)mdp1 + sizeof(struct mdesc) + ((i-1) * BLOCKSIZE); 
396         addr_block2 = (char *)mdp2 + sizeof(struct mdesc) + ((i-1) * BLOCKSIZE); 
397         
398         switch(mdp1->heapinfo[i].busy.type){
399         case 0 :
400           if(mdp1->heapinfo[i].busy.info.block.size != mdp2->heapinfo[i].busy.info.block.size){
401             if(XBT_LOG_ISENABLED(xbt_mm_legacy, xbt_log_priority_debug)){
402               XBT_DEBUG("Different size of a large cluster");
403               errors++;
404             }else{
405               return 1;
406             }
407           }else{
408             if(memcmp(addr_block1, addr_block2, (mdp1->heapinfo[i].busy.info.block.size * BLOCKSIZE)) != 0){
409               if(XBT_LOG_ISENABLED(xbt_mm_legacy, xbt_log_priority_debug)){           
410                 XBT_DEBUG("Different data in block %zu (size = %zu) (addr_block1 = %p (current = %p) - addr_block2 = %p)", i, mdp1->heapinfo[i].busy.info.block.size, addr_block1, (char *)std_heap_addr + sizeof(struct mdesc) + ((i-1) * BLOCKSIZE), addr_block2);
411                 errors++;
412               }else{
413                 return 1;
414               }
415             } 
416           }
417           i = i+mdp1->heapinfo[i].busy.info.block.size;
418
419           break;
420         default :         
421           if(mdp1->heapinfo[i].busy.info.frag.nfree != mdp2->heapinfo[i].busy.info.frag.nfree){
422             if(XBT_LOG_ISENABLED(xbt_mm_legacy, xbt_log_priority_debug)){
423               XBT_DEBUG("Different free fragments in the fragmented block %zu", i);
424               errors++;
425             }else{
426               return 1;
427             }
428           }else{
429             if(mdp1->heapinfo[i].busy.info.frag.first != mdp2->heapinfo[i].busy.info.frag.first){
430               if(XBT_LOG_ISENABLED(xbt_mm_legacy, xbt_log_priority_debug)){
431                 XBT_DEBUG("Different first free fragments in the block %zu", i);
432                 errors++;
433               }else{
434                 return 1;
435               } 
436             }else{
437               frag_size = pow(2,mdp1->heapinfo[i].busy.type);
438               for(j=0 ; j< (BLOCKSIZE/frag_size); j++){
439                 if(memcmp((char *)addr_block1 + (j * frag_size), (char *)addr_block2 + (j * frag_size), frag_size) != 0){
440                   if(XBT_LOG_ISENABLED(xbt_mm_legacy, xbt_log_priority_debug)){
441                     XBT_DEBUG("Different data in fragment %zu (addr_frag1 = %p - addr_frag2 = %p) of block %zu", j + 1, (char *)addr_block1 + (j * frag_size), (char *)addr_block2 + (j * frag_size),  i);
442                     errors++;
443                   }else{
444                     return 1;
445                   }
446                 } 
447               }
448             }
449           }
450
451           i++;
452
453           break;
454         }
455
456       }
457     }
458
459     if( i != first_block_free){
460
461       if(mdp1->heapinfo[block_free].free.next != mdp2->heapinfo[block_free].free.next){
462         if(XBT_LOG_ISENABLED(xbt_mm_legacy, xbt_log_priority_debug)){
463           XBT_DEBUG("Different next block free");
464           errors++;
465         }else{
466           return 1;
467         }
468       }
469      
470       block_free = mdp1->heapinfo[block_free].free.next;
471       next_block_free = mdp1->heapinfo[block_free].free.next;
472
473       i = block_free + mdp1->heapinfo[block_free].free.size;
474
475       if((next_block_free == 0) && (i != mdp1->heaplimit)){
476
477         while(i < mdp1->heaplimit){
478
479           if(mdp1->heapinfo[i].busy.type != mdp2->heapinfo[i].busy.type){
480             if(XBT_LOG_ISENABLED(xbt_mm_legacy, xbt_log_priority_debug)){
481               XBT_DEBUG("Different type of busy block");
482               errors++;
483             }else{
484               return 1;
485             }
486           }else{
487
488             addr_block1 = (char *)mdp1 + sizeof(struct mdesc) + ((i-1) * BLOCKSIZE); 
489             addr_block2 = (char *)mdp2 + sizeof(struct mdesc) + ((i-1) * BLOCKSIZE); 
490         
491             switch(mdp1->heapinfo[i].busy.type){
492             case 0 :
493               if(mdp1->heapinfo[i].busy.info.block.size != mdp2->heapinfo[i].busy.info.block.size){
494                 if(XBT_LOG_ISENABLED(xbt_mm_legacy, xbt_log_priority_debug)){
495                   XBT_DEBUG("Different size of a large cluster");
496                   errors++;
497                 }else{
498                   return 1;
499                 }
500               }else{
501                 if(memcmp(addr_block1, addr_block2, (mdp1->heapinfo[i].busy.info.block.size * BLOCKSIZE)) != 0){
502                   if(XBT_LOG_ISENABLED(xbt_mm_legacy, xbt_log_priority_debug)){       
503                     XBT_DEBUG("Different data in block %zu (addr_block1 = %p (current = %p) - addr_block2 = %p)", i, addr_block1, (char *)std_heap_addr + sizeof(struct mdesc) + ((i-1) * BLOCKSIZE), addr_block2);
504                     errors++;
505                   }else{
506                     return 1;
507                   }
508                 } 
509               }
510             
511               i = i+mdp1->heapinfo[i].busy.info.block.size;
512
513               break;
514             default :     
515               if(mdp1->heapinfo[i].busy.info.frag.nfree != mdp2->heapinfo[i].busy.info.frag.nfree){
516                 if(XBT_LOG_ISENABLED(xbt_mm_legacy, xbt_log_priority_debug)){
517                   XBT_DEBUG("Different free fragments in the fragmented block %zu", i);
518                   errors++;
519                 }else{
520                   return 1;
521                 }
522               }else{
523                 if(mdp1->heapinfo[i].busy.info.frag.first != mdp2->heapinfo[i].busy.info.frag.first){
524                   if(XBT_LOG_ISENABLED(xbt_mm_legacy, xbt_log_priority_debug)){
525                     XBT_DEBUG("Different first free fragments in the block %zu", i);
526                     errors++;
527                   }else{
528                     return 1;
529                   } 
530                 }else{
531                   frag_size = pow(2,mdp1->heapinfo[i].busy.type);
532                   for(j=0 ; j< (BLOCKSIZE/frag_size); j++){
533                     if(memcmp((char *)addr_block1 + (j * frag_size), (char *)addr_block2 + (j * frag_size), frag_size) != 0){
534                       if(XBT_LOG_ISENABLED(xbt_mm_legacy, xbt_log_priority_debug)){
535                         XBT_DEBUG("Different data in fragment %zu (addr_frag1 = %p - addr_frag2 = %p) of block %zu", j + 1, (char *)addr_block1 + (j * frag_size), (char *)addr_block2 + (j * frag_size),  i);
536                         errors++;
537                       }else{
538                         return 1;
539                       }
540                     } 
541                   }
542                 }
543               }
544
545               i++;
546
547               break;
548             }
549           }
550         }
551
552       }
553
554     }
555   }
556   
557   return (errors>0);    
558 }
559
560  
561 void mmalloc_display_info_heap(void *h){
562
563 }  
564   
565