Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
This commit 590f9c7e65ce6a64705272d79443599877d5cb72 add a segfault to compilation.
[simgrid.git] / src / xbt / mmalloc / mm_legacy.c
1 /* Copyright (c) 2010. The SimGrid Team.
2  * All rights reserved.                                                     */
3
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. */
6
7 /* Redefine the classical malloc/free/realloc functions so that they fit well in the mmalloc framework */
8
9 #include "mmprivate.h"
10
11 static void *__mmalloc_current_heap=NULL; /* The heap we are currently using. */
12
13 void* mmalloc_get_current_heap(void) {
14   return __mmalloc_current_heap;
15 }
16 void mmalloc_set_current_heap(void *new_heap) {
17   __mmalloc_current_heap=new_heap;
18 }
19
20 //#define MMALLOC_WANT_OVERIDE_LEGACY /* comment this when stuff goes horribly bad around memory allocation */
21 #ifdef MMALLOC_WANT_OVERIDE_LEGACY
22 void *malloc(size_t n) {
23   void *ret = mmalloc(__mmalloc_current_heap, n);
24
25   return ret;
26 }
27
28 void *calloc(size_t nmemb, size_t size) {
29   size_t total_size = nmemb * size;
30
31   void *ret = mmalloc(__mmalloc_current_heap, total_size);
32
33   /* Fill the allocated memory with zeroes to mimic calloc behaviour */
34   memset(ret,'\0', total_size);
35
36   return ret;
37 }
38
39 void *realloc(void *p, size_t s) {
40   void *ret = NULL;
41
42   if (s) {
43     if (p)
44       ret = mrealloc(__mmalloc_current_heap, p,s);
45     else
46       ret = mmalloc(__mmalloc_current_heap,s);
47   } else {
48     if (p)
49       mfree(__mmalloc_current_heap,p);
50   }
51
52   return ret;
53 }
54
55 void free(void *p) {
56   return mfree(__mmalloc_current_heap, p);
57 }
58 #endif
59
60 /* Make sure it works with md==NULL */
61
62 #define HEAP_OFFSET   40960000    /* Safety gap from the heap's break address */
63
64 void *mmalloc_get_default_md(void) {
65   xbt_assert(__mmalloc_default_mdp);
66   return __mmalloc_default_mdp;
67 }
68
69 /* Initialize the default malloc descriptor. */
70 #include "xbt_modinter.h"
71 void mmalloc_preinit(void) {
72   __mmalloc_default_mdp = mmalloc_attach(-1, (char *)sbrk(0) + HEAP_OFFSET);
73   xbt_assert(__mmalloc_default_mdp != NULL);
74 }
75 void mmalloc_postexit(void) {
76   /* Do not detach the default mdp or ldl won't be able to free the memory it allocated since we're in memory */
77   //  mmalloc_detach(__mmalloc_default_mdp);
78 }