Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
split memory snapshooting and diffing functions out in their own file
[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 #include "gras_config.h"
11 #include <math.h>
12
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_mm_legacy, xbt,
14                                 "Logging specific to mm_legacy in mmalloc");
15
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;
21
22
23 static xbt_mheap_t __mmalloc_current_heap = NULL;     /* The heap we are currently using. */
24
25 xbt_mheap_t mmalloc_get_current_heap(void)
26 {
27   return __mmalloc_current_heap;
28 }
29
30 void mmalloc_set_current_heap(xbt_mheap_t new_heap)
31 {
32   __mmalloc_current_heap = new_heap;
33 }
34
35 #ifdef MMALLOC_WANT_OVERIDE_LEGACY
36 void *malloc(size_t n)
37 {
38   xbt_mheap_t mdp = __mmalloc_current_heap ?: (xbt_mheap_t) mmalloc_preinit();
39
40   LOCK(mdp);
41   void *ret = mmalloc(mdp, n);
42   UNLOCK(mdp);
43
44   return ret;
45 }
46
47 void *calloc(size_t nmemb, size_t size)
48 {
49   xbt_mheap_t mdp = __mmalloc_current_heap ?: (xbt_mheap_t) mmalloc_preinit();
50
51   LOCK(mdp);
52   void *ret = mmalloc(mdp, nmemb*size);
53   UNLOCK(mdp);
54   memset(ret, 0, nmemb * size);
55
56
57   return ret;
58 }
59
60 void *realloc(void *p, size_t s)
61 {
62   void *ret = NULL;
63   xbt_mheap_t mdp = __mmalloc_current_heap ?: (xbt_mheap_t) mmalloc_preinit();
64
65   LOCK(mdp);
66   ret = mrealloc(mdp, p, s);
67   UNLOCK(mdp);
68
69   return ret;
70 }
71
72 void free(void *p)
73 {
74   xbt_mheap_t mdp = __mmalloc_current_heap ?: (xbt_mheap_t) mmalloc_preinit();
75
76   LOCK(mdp);
77   mfree(mdp, p);
78   UNLOCK(mdp);
79 }
80 #endif
81
82