X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/blobdiff_plain/ea174fbe37a5a77f04c94335d63f298228505ae7..3fe5947a05c3e9c84b29e4e7f60c111aeab7fe27:/src/xbt/mmalloc/mm_legacy.c diff --git a/src/xbt/mmalloc/mm_legacy.c b/src/xbt/mmalloc/mm_legacy.c index cb12f56d80..4fb087a92c 100644 --- a/src/xbt/mmalloc/mm_legacy.c +++ b/src/xbt/mmalloc/mm_legacy.c @@ -1,4 +1,4 @@ -/* Copyright (c) 2010-2014. The SimGrid Team. +/* Copyright (c) 2010-2015. The SimGrid Team. * All rights reserved. */ /* This program is free software; you can redistribute it and/or modify it @@ -11,14 +11,14 @@ #include -#include "../../mc/mc_base.h" +#include "src/mc/mc_base.h" #include "mmprivate.h" -#include "xbt_modinter.h" -#include "internal_config.h" +#include "src/xbt_modinter.h" +#include "src/internal_config.h" #include -#include "../mc/mc_protocol.h" +#include "src/mc/mc_protocol.h" -/* ***** Whether to use `mmalloc` of the undrlying malloc ***** */ +/* ***** Whether to use `mmalloc` of the underlying malloc ***** */ static int __malloc_use_mmalloc; @@ -50,14 +50,19 @@ xbt_mheap_t mmalloc_set_current_heap(xbt_mheap_t new_heap) return heap; } -#ifdef MMALLOC_WANT_OVERRIDE_LEGACY +/* Override the malloc-like functions if MC is activated at compile time */ +#if HAVE_MC /* ***** Temporary allocator * * This is used before we have found the real malloc implementation with dlsym. */ -#define BUFFER_SIZE 32 +#ifdef __FreeBSD__ /* FreeBSD require more memory, other might */ +# define BUFFER_SIZE 256 +#else /* Valid on: Linux */ +# define BUFFER_SIZE 32 +#endif static size_t fake_alloc_index; static uint64_t buffer[BUFFER_SIZE]; @@ -67,15 +72,17 @@ static uint64_t buffer[BUFFER_SIZE]; */ static void* mm_fake_malloc(size_t n) { + // How many uint64_t do w need? size_t count = n / sizeof(uint64_t); - if (n && 0xff) - count ++; - if (fake_alloc_index + count < BUFFER_SIZE) { - uint64_t* res = buffer + fake_alloc_index; - fake_alloc_index += count; - return res; - } - exit(127); + if (n % sizeof(uint64_t)) + count++; + // Check that we have enough available memory: + if (fake_alloc_index + count >= BUFFER_SIZE) + exit(127); + // Allocate it: + uint64_t* res = buffer + fake_alloc_index; + fake_alloc_index += count; + return res; } static void* mm_fake_calloc(size_t nmemb, size_t size) @@ -120,10 +127,17 @@ static void __attribute__((constructor(101))) mm_legacy_constructor() if (__malloc_use_mmalloc) { __mmalloc_current_heap = mmalloc_preinit(); } else { +#if HAVE_DLFUNC + mm_real_realloc = (void *(*)(void *, size_t))dlfunc(RTLD_NEXT, "realloc"); + mm_real_malloc = (void *(*)(size_t))dlfunc(RTLD_NEXT, "malloc"); + mm_real_free = (void (*)(void *))dlfunc(RTLD_NEXT, "free"); + mm_real_calloc = (void *(*)(size_t, size_t))dlfunc(RTLD_NEXT, "calloc"); +#else mm_real_realloc = dlsym(RTLD_NEXT, "realloc"); mm_real_malloc = dlsym(RTLD_NEXT, "malloc"); mm_real_free = dlsym(RTLD_NEXT, "free"); mm_real_calloc = dlsym(RTLD_NEXT, "calloc"); +#endif } mm_initializing = 0; mm_initialized = 1; @@ -253,4 +267,4 @@ void free(void *p) mfree(mdp, p); UNLOCK(mdp); } -#endif /* WANT_MALLOC_OVERRIDE */ +#endif /* HAVE_MC */