1 /* Copyright (c) 2010. The SimGrid Team.
2 * All rights reserved. */
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. */
7 /* Redefine the classical malloc/free/realloc functions so that they fit well in the mmalloc framework */
10 #include "gras_config.h"
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_mm_legacy, xbt,
13 "Logging specific to mm_legacy in mmalloc");
15 static void *__mmalloc_current_heap = NULL; /* The heap we are currently using. */
17 #include "xbt_modinter.h"
19 void *mmalloc_get_current_heap(void)
21 return __mmalloc_current_heap;
24 void mmalloc_set_current_heap(void *new_heap)
26 __mmalloc_current_heap = new_heap;
29 #ifdef MMALLOC_WANT_OVERIDE_LEGACY
30 void *malloc(size_t n)
32 void *mdp = __mmalloc_current_heap;
38 void *ret = mmalloc(mdp, n);
44 void *calloc(size_t nmemb, size_t size)
46 size_t total_size = nmemb * size;
47 void *mdp = __mmalloc_current_heap;
53 void *ret = mmalloc(mdp, total_size);
56 /* Fill the allocated memory with zeroes to mimic calloc behaviour */
57 memset(ret, '\0', total_size);
62 void *realloc(void *p, size_t s)
65 void *mdp = __mmalloc_current_heap;
73 ret = mrealloc(mdp, p, s);
75 ret = mmalloc(mdp, s);
87 void *mdp = __mmalloc_current_heap;
97 /* Make sure it works with md==NULL */
99 /* Safety gap from the heap's break address.
100 * Try to increase this first if you experience strange errors under
102 #define HEAP_OFFSET (128UL<<20)
104 void *mmalloc_get_default_md(void)
106 xbt_assert(__mmalloc_default_mdp);
107 return __mmalloc_default_mdp;
110 static void mmalloc_fork_prepare(void)
112 struct mdesc* mdp = NULL;
113 if ((mdp =__mmalloc_default_mdp)){
119 mdp = mdp->next_mdesc;
124 static void mmalloc_fork_parent(void)
126 struct mdesc* mdp = NULL;
127 if ((mdp =__mmalloc_default_mdp)){
131 mdp = mdp->next_mdesc;
136 static void mmalloc_fork_child(void)
138 struct mdesc* mdp = NULL;
139 if ((mdp =__mmalloc_default_mdp)){
142 mdp = mdp->next_mdesc;
147 /* Initialize the default malloc descriptor. */
148 void mmalloc_preinit(void)
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);
159 THROWF(system_error,0,"xbt_os_thread_atfork() failed: return value %d",res);
161 xbt_assert(__mmalloc_default_mdp != NULL);
164 void mmalloc_postexit(void)
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);
171 int mmalloc_compare_heap(void *h1, void *h2){
173 if(h1 == NULL && h2 == NULL){
174 XBT_DEBUG("Malloc descriptors null");
181 struct mstats ms1 = mmstats(h1);
182 struct mstats ms2 = mmstats(h2);
184 if(ms1.bytes_total != ms2.bytes_total){
185 XBT_DEBUG("Different total size of the heap");
189 if(ms1.chunks_used != ms2.chunks_used){
190 XBT_DEBUG("Different chunks allocated by the user");
194 if(ms1.bytes_used != ms2.bytes_used){
195 XBT_DEBUG("Different byte total of user-allocated chunks");
199 if(ms1.bytes_free != ms2.bytes_free){
200 XBT_DEBUG("Different byte total of chunks in the free list");
204 if(ms1.chunks_free != ms2.chunks_free){
205 XBT_DEBUG("Different chunks in the free list");
209 struct mdesc *mdp1, *mdp2;
210 mdp1 = MD_TO_MDP(h1);
211 mdp2 = MD_TO_MDP(h2);
213 if(mmalloc_compare_mdesc(mdp1, mdp2))
220 int mmalloc_compare_mdesc(struct mdesc *mdp1, struct mdesc *mdp2){
222 if(mdp1->headersize != mdp2->headersize){
223 XBT_DEBUG("Different size of the file header for the mapped files");
227 if(mdp1->refcount != mdp2->refcount){
228 XBT_DEBUG("Different number of processes that attached the heap");
232 if(strcmp(mdp1->magic, mdp2->magic) != 0){
233 XBT_DEBUG("Different magic number");
237 if(mdp1->flags != mdp2->flags){
238 XBT_DEBUG("Different flags");
242 if(mdp1->heapsize != mdp2->heapsize){
243 XBT_DEBUG("Different number of info entries");
247 //XBT_DEBUG("Heap size : %d", mdp1->heapsize);
249 if(mdp1->heapbase != mdp2->heapbase){
250 XBT_DEBUG("Different first block of the heap");
254 if(mdp1->heapindex != mdp2->heapindex){
255 XBT_DEBUG("Different index for the heap table");
259 //XBT_DEBUG("Heap index : %d", mdp1->heapindex);
261 if(mdp1->base != mdp2->base){
262 XBT_DEBUG("Different base address of the memory region");
266 if(mdp1->breakval != mdp2->breakval){
267 XBT_DEBUG("Different current location in the memory region");
271 if(mdp1->top != mdp2->top){
272 XBT_DEBUG("Different end of the current location in the memory region");
276 if(mdp1->heaplimit != mdp2->heaplimit){
277 XBT_DEBUG("Different limit of valid info table indices");
281 //XBT_DEBUG("Heap limit : %d", mdp1->heaplimit);
284 if(mdp1->fd != mdp2->fd){
285 XBT_DEBUG("Different file descriptor for the file to which this malloc heap is mapped");
289 if(mdp1->saved_errno != mdp2->saved_errno){
290 XBT_DEBUG("Different errno");
294 if(mdp1->version != mdp2->version){
295 XBT_DEBUG("Different version of the mmalloc package");
300 size_t block_free1, start1, block_free2 , start2, block_busy1, block_busy2 ;
302 void *addr_block1, *addr_block2;
305 start1 = block_free1 = mdp1->heapindex;
306 start2 = block_free2 = mdp2->heapindex;
307 block_busy1 = start1 + mdp1->heapinfo[start1].free.size;
308 block_busy2 = start2 + mdp2->heapinfo[start2].free.size;
310 //XBT_DEBUG("Block busy : %Zu - %Zu", block_busy1, block_busy2);
313 if(mdp1->heapinfo[start1].free.size != mdp2->heapinfo[start2].free.size){ // <=> check block_busy
315 XBT_DEBUG("Different size (in blocks) of a free cluster");
320 if(mdp1->heapinfo[start1].free.next != mdp2->heapinfo[start1].free.next){
322 XBT_DEBUG("Different index of next free cluster");
329 //XBT_DEBUG("Next free : %Zu", mdp1->heapinfo[start1].free.next);
332 while(i<mdp1->heapinfo[start1].free.next){
334 //XBT_DEBUG("i (block busy) : %Zu", i);
336 if(mdp1->heapinfo[i].busy.type != mdp2->heapinfo[i].busy.type){
337 XBT_DEBUG("Different type of busy block");
341 addr_block1 = ADDRESS(i);
343 addr_block2 = ADDRESS(i);
344 switch(mdp1->heapinfo[i].busy.type){
346 if(mdp1->heapinfo[i].busy.info.size != mdp2->heapinfo[i].busy.info.size){
347 XBT_DEBUG("Different size of a large cluster");
350 //XBT_DEBUG("Blocks %Zu : %p - %p / Data size : %Zu (%Zu blocks)", i, addr_block1, addr_block2, (mdp->heapinfo[i].busy.info.size * BLOCKSIZE),mdp->heapinfo[i].busy.info.size );
351 if(memcmp(addr_block1, addr_block2, mdp1->heapinfo[i].busy.info.size * BLOCKSIZE) != 0){
352 XBT_DEBUG("Different data in block %Zu", i);
356 i = i + mdp1->heapinfo[i].busy.info.size;
359 if(mdp1->heapinfo[i].busy.info.frag.nfree != mdp2->heapinfo[i].busy.info.frag.nfree){
360 XBT_DEBUG("Different free fragments in a fragmented block");
363 if(mdp1->heapinfo[i].busy.info.frag.first != mdp2->heapinfo[i].busy.info.frag.first){
364 XBT_DEBUG("Different first free fragments of the block");
367 if(memcmp(addr_block1, addr_block2, BLOCKSIZE) != 0){
368 XBT_DEBUG("Different data in block %Zu", i);
380 block_free1 = mdp1->heapinfo[start1].free.next;
381 block_free2 = mdp2->heapinfo[start2].free.next;
383 //XBT_DEBUG("Index of next free cluster : %d", block_free1);
385 while((block_free1 != start1) && (block_free2 != start2)){
387 block_busy1 = block_free1 + mdp1->heapinfo[block_free1].free.size;
388 block_busy2 = block_free2 + mdp2->heapinfo[block_free2].free.size;
390 if(block_busy1 != block_busy2){
391 XBT_DEBUG("Different index of busy block");
395 //XBT_DEBUG("Index of next busy block : %Zu - %Zu", block_busy1, block_busy2);
396 //XBT_DEBUG("Index of next free cluster : %Zu", mdp1->heapinfo[block_free1].free.next);
400 while(i<mdp1->heapinfo[block_free1].free.next){
402 //XBT_DEBUG("i (block busy) : %Zu", i);
407 if(mdp1->heapinfo[i].busy.type != mdp2->heapinfo[i].busy.type){
408 XBT_DEBUG("Different type of busy block");
412 addr_block1 = ADDRESS(i);
414 addr_block2 = ADDRESS(i);
415 switch(mdp1->heapinfo[i].busy.type){
417 if(mdp1->heapinfo[i].busy.info.size != mdp2->heapinfo[i].busy.info.size){
418 XBT_DEBUG("Different size of a large cluster");
421 //XBT_DEBUG("Blocks %Zu : %p - %p / Data size : %Zu", i, addr_block1, addr_block2, (mdp->heapinfo[i].busy.info.size * BLOCKSIZE));
422 //XBT_DEBUG("Size of large cluster %d", mdp->heapinfo[i].busy.info.size);
423 if(memcmp(addr_block1, addr_block2, (mdp1->heapinfo[i].busy.info.size * BLOCKSIZE)) != 0){
424 XBT_DEBUG("Different data in block %Zu", i);
428 i = i+mdp1->heapinfo[i].busy.info.size;
431 if(mdp1->heapinfo[i].busy.info.frag.nfree != mdp2->heapinfo[i].busy.info.frag.nfree){
432 XBT_DEBUG("Different free fragments in a fragmented block");
435 if(mdp1->heapinfo[i].busy.info.frag.first != mdp2->heapinfo[i].busy.info.frag.first){
436 XBT_DEBUG("Different first free fragments of the block");
439 if(memcmp(addr_block1, addr_block2, BLOCKSIZE) != 0){
440 XBT_DEBUG("Different data in fragmented block %Zu", i);
454 block_free1 = mdp1->heapinfo[block_free1].free.next;
455 block_free2 = mdp2->heapinfo[block_free2].free.next;