1 /* Copyright (c) 2010-2014. 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 "xbt_modinter.h"
11 #include "internal_config.h"
14 //#define MM_LEGACY_VERBOSE 1 /* define this to see which version of malloc gets used */
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;
23 static xbt_mheap_t __mmalloc_current_heap = NULL; /* The heap we are currently using. */
25 xbt_mheap_t mmalloc_get_current_heap(void)
27 return __mmalloc_current_heap;
30 void mmalloc_set_current_heap(xbt_mheap_t new_heap)
32 __mmalloc_current_heap = new_heap;
36 #ifdef MMALLOC_WANT_OVERRIDE_LEGACY
43 static void * (*real_malloc) (size_t);
44 static void * (*real_realloc) (void*,size_t);
45 static void * (*real_free) (void*);
47 static void mm_gnuld_legacy_init(void) { /* This function is called from mmalloc_preinit(); it works even if it's static because all mm is in mm.c */
48 real_malloc = (void * (*) (size_t)) dlsym(RTLD_NEXT, "malloc");
49 real_realloc = (void * (*) (void*,size_t)) dlsym(RTLD_NEXT, "realloc");
50 real_free = (void * (*) (void*)) dlsym(RTLD_NEXT, "free");
51 __mmalloc_current_heap = __mmalloc_default_mdp;
55 * DL needs some memory while resolving the malloc symbol, that is somehow problematic
56 * To that extend, we have a little area here living in .BSS that we return if asked for memory before the malloc is resolved.
58 int allocated_junk=0; /* keep track of whether our little area was already given to someone */
61 /* This version use mmalloc if there is a current heap, or the legacy implem if not */
62 void *malloc(size_t n) {
63 xbt_mheap_t mdp = __mmalloc_current_heap;
65 #ifdef MM_LEGACY_VERBOSE
66 static int warned_raw = 0;
67 static int warned_mmalloc = 0;
72 ret = mmalloc(mdp, n);
74 #ifdef MM_LEGACY_VERBOSE
75 if (!warned_mmalloc) {
76 fprintf(stderr,"Using mmalloc; enabling the model-checker in cmake may have a bad impact on your simulation performance\n");
84 "Panic: real malloc symbol not resolved yet, and I already gave my little private memory chunk away. "
85 "Damn LD, we must extend our code to have several such areas.\n");
87 } else if (n > sizeof junkarea) {
89 "Panic: real malloc symbol not resolved yet, and I need %zu bytes while my little private memory chunk is only %zu bytes wide. "
90 "Damn LD, we must fix our code to extend this area.\n", n, sizeof junkarea);
97 #ifdef MM_LEGACY_VERBOSE
99 fprintf(stderr,"Using system malloc after interception; you seem to be currently model-checking\n");
103 ret = real_malloc(n);
109 void *calloc(size_t nmemb, size_t size)
111 void *ret = malloc(nmemb*size);
112 memset(ret, 0, nmemb * size);
116 void *realloc(void *p, size_t s)
118 xbt_mheap_t mdp = __mmalloc_current_heap;
123 ret = mrealloc(mdp, p, s);
126 ret = real_realloc(p,s);
137 xbt_mheap_t mdp = __mmalloc_current_heap;
152 #else /* NO GNU_LD */
153 void *malloc(size_t n)
155 xbt_mheap_t mdp = __mmalloc_current_heap ?: (xbt_mheap_t) mmalloc_preinit();
158 void *ret = mmalloc(mdp, n);
164 void *calloc(size_t nmemb, size_t size)
166 xbt_mheap_t mdp = __mmalloc_current_heap ?: (xbt_mheap_t) mmalloc_preinit();
169 void *ret = mmalloc(mdp, nmemb*size);
171 memset(ret, 0, nmemb * size);
177 void *realloc(void *p, size_t s)
180 xbt_mheap_t mdp = __mmalloc_current_heap ?: (xbt_mheap_t) mmalloc_preinit();
183 ret = mrealloc(mdp, p, s);
192 xbt_mheap_t mdp = __mmalloc_current_heap ?: (xbt_mheap_t) mmalloc_preinit();
199 #endif /* NO GNU_LD */
200 #endif /* WANT_MALLOC_OVERRIDE */