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 "internal_config.h"
13 //#define MM_LEGACY_VERBOSE 1 /* define this to see which version of malloc gets used */
15 /* The mmalloc() package can use a single implicit malloc descriptor
16 for mmalloc/mrealloc/mfree operations which do not supply an explicit
17 descriptor. This allows mmalloc() to provide
18 backwards compatibility with the non-mmap'd version. */
19 xbt_mheap_t __mmalloc_default_mdp = NULL;
22 static xbt_mheap_t __mmalloc_current_heap = NULL; /* The heap we are currently using. */
24 xbt_mheap_t mmalloc_get_current_heap(void)
26 return __mmalloc_current_heap;
29 void mmalloc_set_current_heap(xbt_mheap_t new_heap)
31 __mmalloc_current_heap = new_heap;
35 #ifdef MMALLOC_WANT_OVERRIDE_LEGACY
42 static void * (*real_malloc) (size_t);
43 static void * (*real_realloc) (void*,size_t);
44 static void * (*real_free) (void*);
46 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 */
47 real_malloc = (void * (*) (size_t)) dlsym(RTLD_NEXT, "malloc");
48 real_realloc = (void * (*) (void*,size_t)) dlsym(RTLD_NEXT, "realloc");
49 real_free = (void * (*) (void*)) dlsym(RTLD_NEXT, "free");
50 __mmalloc_current_heap = __mmalloc_default_mdp;
54 * DL needs some memory while resolving the malloc symbol, that is somehow problematic
55 * To that extend, we have a little area here living in .BSS that we return if asked for memory before the malloc is resolved.
57 int allocated_junk=0; /* keep track of whether our little area was already given to someone */
60 /* This version use mmalloc if there is a current heap, or the legacy implem if not */
61 void *malloc(size_t n) {
62 xbt_mheap_t mdp = __mmalloc_current_heap;
64 #ifdef MM_LEGACY_VERBOSE
65 static int warned_raw = 0;
66 static int warned_mmalloc = 0;
71 ret = mmalloc(mdp, n);
73 #ifdef MM_LEGACY_VERBOSE
74 if (!warned_mmalloc) {
75 fprintf(stderr,"Using mmalloc; enabling the model-checker in cmake may have a bad impact on your simulation performance\n");
83 "Panic: real malloc symbol not resolved yet, and I already gave my little private memory chunk away. "
84 "Damn LD, we must extend our code to have several such areas.\n");
86 } else if (n > sizeof junkarea) {
88 "Panic: real malloc symbol not resolved yet, and I need %zu bytes while my little private memory chunk is only %zu bytes wide. "
89 "Damn LD, we must fix our code to extend this area.\n", n, sizeof junkarea);
96 #ifdef MM_LEGACY_VERBOSE
98 fprintf(stderr,"Using system malloc after interception; you seem to be currently model-checking\n");
102 ret = real_malloc(n);
108 void *calloc(size_t nmemb, size_t size)
110 void *ret = malloc(nmemb*size);
111 memset(ret, 0, nmemb * size);
115 void *realloc(void *p, size_t s)
117 xbt_mheap_t mdp = __mmalloc_current_heap;
122 ret = mrealloc(mdp, p, s);
125 ret = real_realloc(p,s);
136 xbt_mheap_t mdp = __mmalloc_current_heap;
151 #else /* NO GNU_LD */
152 void *malloc(size_t n)
154 xbt_mheap_t mdp = __mmalloc_current_heap ?: (xbt_mheap_t) mmalloc_preinit();
157 void *ret = mmalloc(mdp, n);
163 void *calloc(size_t nmemb, size_t size)
165 xbt_mheap_t mdp = __mmalloc_current_heap ?: (xbt_mheap_t) mmalloc_preinit();
168 void *ret = mmalloc(mdp, nmemb*size);
170 memset(ret, 0, nmemb * size);
176 void *realloc(void *p, size_t s)
179 xbt_mheap_t mdp = __mmalloc_current_heap ?: (xbt_mheap_t) mmalloc_preinit();
182 ret = mrealloc(mdp, p, s);
191 xbt_mheap_t mdp = __mmalloc_current_heap ?: (xbt_mheap_t) mmalloc_preinit();
198 #endif /* NO GNU_LD */
199 #endif /* WANT_MALLOC_OVERRIDE */