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 #include "xbt_modinter.h"
15 void *mmalloc_get_current_heap(void)
17 return __mmalloc_current_heap;
20 void mmalloc_set_current_heap(void *new_heap)
22 __mmalloc_current_heap = new_heap;
25 #ifdef MMALLOC_WANT_OVERIDE_LEGACY
26 void *malloc(size_t n)
29 if (!__mmalloc_current_heap)
32 void *ret = mmalloc(__mmalloc_current_heap, n);
37 void *calloc(size_t nmemb, size_t size)
39 size_t total_size = nmemb * size;
41 if (!__mmalloc_current_heap)
44 void *ret = mmalloc(__mmalloc_current_heap, total_size);
46 /* Fill the allocated memory with zeroes to mimic calloc behaviour */
47 memset(ret, '\0', total_size);
52 void *realloc(void *p, size_t s)
56 if (!__mmalloc_current_heap)
61 ret = mrealloc(__mmalloc_current_heap, p, s);
63 ret = mmalloc(__mmalloc_current_heap, s);
66 mfree(__mmalloc_current_heap, p);
74 return mfree(__mmalloc_current_heap, p);
78 /* Make sure it works with md==NULL */
80 #define HEAP_OFFSET 40960000 /* Safety gap from the heap's break address */
82 void *mmalloc_get_default_md(void)
84 xbt_assert(__mmalloc_default_mdp);
85 return __mmalloc_default_mdp;
88 /* Initialize the default malloc descriptor. */
89 void mmalloc_preinit(void)
91 if (!__mmalloc_default_mdp)
92 __mmalloc_default_mdp =
93 mmalloc_attach(-1, (char *) sbrk(0) + HEAP_OFFSET);
94 xbt_assert(__mmalloc_default_mdp != NULL);
97 void mmalloc_postexit(void)
99 /* Do not detach the default mdp or ldl won't be able to free the memory it allocated since we're in memory */
100 // mmalloc_detach(__mmalloc_default_mdp);