Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
remove unused lines
[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 #include "xbt_modinter.h"
14
15 void* mmalloc_get_current_heap(void) {
16   return __mmalloc_current_heap;
17 }
18 void mmalloc_set_current_heap(void *new_heap) {
19   __mmalloc_current_heap=new_heap;
20 }
21
22 #ifdef MMALLOC_WANT_OVERIDE_LEGACY
23 void *malloc(size_t n) {
24 #ifdef HAVE_MMAP
25   if (!__mmalloc_current_heap) mmalloc_preinit();
26 #endif
27   void *ret = mmalloc(__mmalloc_current_heap, n);
28
29   return ret;
30 }
31
32 void *calloc(size_t nmemb, size_t size) {
33   size_t total_size = nmemb * size;
34 #ifdef HAVE_MMAP
35   if (!__mmalloc_current_heap) mmalloc_preinit();
36 #endif
37   void *ret = mmalloc(__mmalloc_current_heap, total_size);
38
39   /* Fill the allocated memory with zeroes to mimic calloc behaviour */
40   memset(ret,'\0', total_size);
41
42   return ret;
43 }
44
45 void *realloc(void *p, size_t s) {
46   void *ret = NULL;
47 #ifdef HAVE_MMAP
48   if (!__mmalloc_current_heap) mmalloc_preinit();
49 #endif
50   if (s) {
51         if (p)
52           ret = mrealloc(__mmalloc_current_heap, p,s);
53         else
54           ret = mmalloc(__mmalloc_current_heap,s);
55   } else {
56         if (p)
57           mfree(__mmalloc_current_heap,p);
58   }
59
60   return ret;
61 }
62
63 void free(void *p) {
64   return mfree(__mmalloc_current_heap, p);
65 }
66 #endif
67
68 /* Make sure it works with md==NULL */
69
70 #define HEAP_OFFSET   40960000    /* Safety gap from the heap's break address */
71
72 void *mmalloc_get_default_md(void) {
73   xbt_assert(__mmalloc_default_mdp);
74   return __mmalloc_default_mdp;
75 }
76
77 /* Initialize the default malloc descriptor. */
78 void mmalloc_preinit(void) {
79   if(!__mmalloc_default_mdp) __mmalloc_default_mdp = mmalloc_attach(-1, (char *)sbrk(0) + HEAP_OFFSET);
80   xbt_assert(__mmalloc_default_mdp != NULL);
81 }
82 void mmalloc_postexit(void) {
83   /* Do not detach the default mdp or ldl won't be able to free the memory it allocated since we're in memory */
84   //  mmalloc_detach(__mmalloc_default_mdp);
85 }