Logo AND Algorithmique Numérique Distribuée

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