Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fix sem_init testing.
[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 {
17   return __mmalloc_current_heap;
18 }
19
20 void mmalloc_set_current_heap(void *new_heap)
21 {
22   __mmalloc_current_heap = new_heap;
23 }
24
25 #ifdef MMALLOC_WANT_OVERIDE_LEGACY
26 void *malloc(size_t n)
27 {
28 #ifdef HAVE_MMAP
29   if (!__mmalloc_current_heap)
30     mmalloc_preinit();
31 #endif
32   LOCK(__mmalloc_current_heap);
33   void *ret = mmalloc(__mmalloc_current_heap, n);
34   UNLOCK(__mmalloc_current_heap);
35
36   return ret;
37 }
38
39 void *calloc(size_t nmemb, size_t size)
40 {
41   size_t total_size = nmemb * size;
42 #ifdef HAVE_MMAP
43   if (!__mmalloc_current_heap)
44     mmalloc_preinit();
45 #endif
46   LOCK(__mmalloc_current_heap);
47   void *ret = mmalloc(__mmalloc_current_heap, total_size);
48   UNLOCK(__mmalloc_current_heap);
49
50   /* Fill the allocated memory with zeroes to mimic calloc behaviour */
51   memset(ret, '\0', total_size);
52
53   return ret;
54 }
55
56 void *realloc(void *p, size_t s)
57 {
58   void *ret = NULL;
59 #ifdef HAVE_MMAP
60   if (!__mmalloc_current_heap)
61     mmalloc_preinit();
62 #endif
63   LOCK(__mmalloc_current_heap);
64   if (s) {
65     if (p)
66       ret = mrealloc(__mmalloc_current_heap, p, s);
67     else
68       ret = mmalloc(__mmalloc_current_heap, s);
69   } else {
70     if (p)
71       mfree(__mmalloc_current_heap, p);
72   }
73   UNLOCK(__mmalloc_current_heap);
74
75   return ret;
76 }
77
78 void free(void *p)
79 {
80   LOCK(__mmalloc_current_heap);
81   mfree(__mmalloc_current_heap, p);
82   UNLOCK(__mmalloc_current_heap);
83 }
84 #endif
85
86 /* Make sure it works with md==NULL */
87
88 #define HEAP_OFFSET   (128<<20)  /* Safety gap from the heap's break address.
89                                   * Try to increase this first if you experience
90                                   * strange errors under valgrind. */
91
92 void *mmalloc_get_default_md(void)
93 {
94   xbt_assert(__mmalloc_default_mdp);
95   return __mmalloc_default_mdp;
96 }
97
98 static void mmalloc_fork_prepare(void)
99 {
100   if (__mmalloc_default_mdp)
101     LOCK(__mmalloc_default_mdp);
102 }
103
104 static void mmalloc_fork_finish(void)
105 {
106   if (__mmalloc_default_mdp)
107     UNLOCK(__mmalloc_default_mdp);
108 }
109
110 /* Initialize the default malloc descriptor. */
111 void mmalloc_preinit(void)
112 {
113   if (!__mmalloc_default_mdp) {
114     __mmalloc_default_mdp =
115         mmalloc_attach(-1, (char *) sbrk(0) + HEAP_OFFSET);
116     /* Fixme? only the default mdp in protected against forks */
117     if (xbt_os_thread_atfork(mmalloc_fork_prepare,
118                              mmalloc_fork_finish, mmalloc_fork_finish) != 0)
119       abort();
120   }
121   xbt_assert(__mmalloc_default_mdp != NULL);
122 }
123
124 void mmalloc_postexit(void)
125 {
126   /* Do not detach the default mdp or ldl won't be able to free the memory it allocated since we're in memory */
127   //  mmalloc_detach(__mmalloc_default_mdp);
128   mmalloc_pre_detach(__mmalloc_default_mdp);
129 }