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) = NULL;
44 static void * (*real_realloc) (void*,size_t) = NULL;
45 static void * (*real_free) (void*) = NULL;
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 static int allocated_junk = 0; /* keep track of many blocks of our little area was already given to someone */
60 #define MAX_JUNK_AREAS (3*4096/JUNK_SIZE)
61 static char junkareas[MAX_JUNK_AREAS][JUNK_SIZE];
63 /* This version use mmalloc if there is a current heap, or the legacy implem if not */
64 void *malloc(size_t n) {
65 xbt_mheap_t mdp = __mmalloc_current_heap;
67 #ifdef MM_LEGACY_VERBOSE
68 static int warned_raw = 0;
69 static int warned_mmalloc = 0;
74 ret = mmalloc(mdp, n);
76 #ifdef MM_LEGACY_VERBOSE
77 if (!warned_mmalloc) {
78 fprintf(stderr,"Using mmalloc; enabling the model-checker in cmake may have a bad impact on your simulation performance\n");
84 size_t needed_areas = n / JUNK_SIZE;
85 if(needed_areas * JUNK_SIZE != n) needed_areas++;
86 if (allocated_junk+needed_areas>=MAX_JUNK_AREAS) {
88 "Panic: real malloc symbol not resolved yet, and I already gave my little private memory chunk away.\n");
91 size_t i = allocated_junk;
92 allocated_junk += needed_areas;
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);
135 if (p<=(void*)junkareas || p>(void*)(junkareas[MAX_JUNK_AREAS]) ) {
136 xbt_mheap_t mdp = __mmalloc_current_heap;
145 } else if(allocated_junk && p==junkareas[allocated_junk-1]) {
153 #else /* NO GNU_LD */
154 void *malloc(size_t n)
156 xbt_mheap_t mdp = __mmalloc_current_heap ?: (xbt_mheap_t) mmalloc_preinit();
159 void *ret = mmalloc(mdp, n);
165 void *calloc(size_t nmemb, size_t size)
167 xbt_mheap_t mdp = __mmalloc_current_heap ?: (xbt_mheap_t) mmalloc_preinit();
170 void *ret = mmalloc(mdp, nmemb*size);
172 memset(ret, 0, nmemb * size);
178 void *realloc(void *p, size_t s)
181 xbt_mheap_t mdp = __mmalloc_current_heap ?: (xbt_mheap_t) mmalloc_preinit();
184 ret = mrealloc(mdp, p, s);
193 xbt_mheap_t mdp = __mmalloc_current_heap ?: (xbt_mheap_t) mmalloc_preinit();
200 #endif /* NO GNU_LD */
201 #endif /* WANT_MALLOC_OVERRIDE */