Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Rename gras_config to internal_config.
[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 "internal_config.h"
11 #include <math.h>
12
13
14 /* The mmalloc() package can use a single implicit malloc descriptor
15    for mmalloc/mrealloc/mfree operations which do not supply an explicit
16    descriptor.  This allows mmalloc() to provide
17    backwards compatibility with the non-mmap'd version. */
18 xbt_mheap_t __mmalloc_default_mdp = NULL;
19
20
21 static xbt_mheap_t __mmalloc_current_heap = NULL;     /* The heap we are currently using. */
22
23 xbt_mheap_t mmalloc_get_current_heap(void)
24 {
25   return __mmalloc_current_heap;
26 }
27
28 void mmalloc_set_current_heap(xbt_mheap_t new_heap)
29 {
30   __mmalloc_current_heap = new_heap;
31 }
32
33 #ifdef MMALLOC_WANT_OVERRIDE_LEGACY
34 void *malloc(size_t n)
35 {
36   xbt_mheap_t mdp = __mmalloc_current_heap ?: (xbt_mheap_t) mmalloc_preinit();
37
38   LOCK(mdp);
39   void *ret = mmalloc(mdp, n);
40   UNLOCK(mdp);
41
42   return ret;
43 }
44
45 void *calloc(size_t nmemb, size_t size)
46 {
47   xbt_mheap_t mdp = __mmalloc_current_heap ?: (xbt_mheap_t) mmalloc_preinit();
48
49   LOCK(mdp);
50   void *ret = mmalloc(mdp, nmemb*size);
51   UNLOCK(mdp);
52   memset(ret, 0, nmemb * size);
53
54
55   return ret;
56 }
57
58 void *realloc(void *p, size_t s)
59 {
60   void *ret = NULL;
61   xbt_mheap_t mdp = __mmalloc_current_heap ?: (xbt_mheap_t) mmalloc_preinit();
62
63   LOCK(mdp);
64   ret = mrealloc(mdp, p, s);
65   UNLOCK(mdp);
66
67   return ret;
68 }
69
70 void free(void *p)
71 {
72   if (p != NULL) {
73     xbt_mheap_t mdp = __mmalloc_current_heap ?: (xbt_mheap_t) mmalloc_preinit();
74
75     LOCK(mdp);
76     mfree(mdp, p);
77     UNLOCK(mdp);
78   }
79 }
80 #endif
81
82