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 */
11 static void *__mmalloc_current_heap=NULL; /* The heap we are currently using. */
13 void* mmalloc_get_current_heap(void) {
14 return __mmalloc_current_heap;
16 void mmalloc_set_current_heap(void *new_heap) {
17 __mmalloc_current_heap=new_heap;
20 #ifdef MMALLOC_WANT_OVERIDE_LEGACY
21 void *malloc(size_t n) {
22 void *ret = mmalloc(__mmalloc_current_heap, n);
27 void *calloc(size_t nmemb, size_t size) {
28 size_t total_size = nmemb * size;
30 void *ret = mmalloc(__mmalloc_current_heap, total_size);
32 /* Fill the allocated memory with zeroes to mimic calloc behaviour */
33 memset(ret,'\0', total_size);
38 void *realloc(void *p, size_t s) {
43 ret = mrealloc(__mmalloc_current_heap, p,s);
45 ret = mmalloc(__mmalloc_current_heap,s);
48 mfree(__mmalloc_current_heap,p);
55 return mfree(__mmalloc_current_heap, p);
59 /* Make sure it works with md==NULL */
61 #define HEAP_OFFSET 40960000 /* Safety gap from the heap's break address */
63 void *mmalloc_get_default_md(void) {
64 xbt_assert(__mmalloc_default_mdp);
65 return __mmalloc_default_mdp;
68 /* Initialize the default malloc descriptor. */
69 #include "xbt_modinter.h"
70 void mmalloc_preinit(void) {
71 __mmalloc_default_mdp = mmalloc_attach(-1, (char *)sbrk(0) + HEAP_OFFSET);
72 xbt_assert(__mmalloc_default_mdp != NULL);
74 void mmalloc_postexit(void) {
75 /* Do not detach the default mdp or ldl won't be able to free the memory it allocated since we're in memory */
76 // mmalloc_detach(__mmalloc_default_mdp);