Logo ROOT   6.08/07
Reference Guide
TMemStatHook.cxx
Go to the documentation of this file.
1 // @(#)root/memstat:$Id$
2 // Author: Anar Manafov (A.Manafov@gsi.de) 2008-03-02
3 
4 /*************************************************************************
5 * Copyright (C) 1995-2010, Rene Brun and Fons Rademakers. *
6 * All rights reserved. *
7 * *
8 * For the licensing terms see $ROOTSYS/LICENSE. *
9 * For the list of contributors see $ROOTSYS/README/CREDITS. *
10 *************************************************************************/
11 //STD
12 #include <iostream>
13 // MemStat
14 #include "TMemStatHook.h"
15 #include "RConfig.h"
16 
17 // TODO: move it to a separate file
18 #if defined(__APPLE__)
19 static malloc_zone_t original_zone;
20 #ifndef __CINT__
21 static void* profile_malloc(malloc_zone_t *zone, size_t size);
22 static void* profile_calloc(malloc_zone_t *zone, size_t num_items, size_t size);
23 static void* profile_valloc(malloc_zone_t *zone, size_t size);
24 static void profile_free(malloc_zone_t *zone, void *ptr);
25 #if defined(MAC_OS_X_VERSION_10_6)
26 static void profile_free_definite_size(malloc_zone_t *zone, void *ptr, size_t size);
27 #endif
28 #endif
29 
30 static zoneMallocHookFunc_t m_pm;
31 static zoneFreeHookFunc_t m_pf;
32 #else
33 #include <malloc.h>
34 #endif
35 
36 #if defined(R__GNU) && (defined(R__LINUX) || defined(__APPLE__))
37 #define SUPPORTS_MEMSTAT
38 #endif
39 
40 
41 using namespace std;
42 
43 #if !defined(__APPLE__)
44 ////////////////////////////////////////////////////////////////////////////////
45 /// GetMallocHook - a static function
46 /// malloc function getter
47 
49 {
50 #if defined(SUPPORTS_MEMSTAT)
51  return __malloc_hook;
52 #else
53  return 0;
54 #endif
55 }
56 
57 ////////////////////////////////////////////////////////////////////////////////
58 /// GetFreeHook - a static function
59 /// free function getter
60 
62 {
63 #if defined(SUPPORTS_MEMSTAT)
64  return __free_hook;
65 #else
66  return 0;
67 #endif
68 }
69 
70 ////////////////////////////////////////////////////////////////////////////////
71 /// SetMallocHook - a static function
72 /// Set pointer to function replacing alloc function
73 
74 void TMemStatHook::SetMallocHook(MallocHookFunc_t p)
75 {
76 #if defined(SUPPORTS_MEMSTAT)
77  __malloc_hook = p;
78 #endif
79 }
80 
81 ////////////////////////////////////////////////////////////////////////////////
82 /// SetFreeHook - a static function
83 /// Set pointer to function replacing free function
84 
85 void TMemStatHook::SetFreeHook(FreeHookFunc_t p)
86 {
87 #if defined(SUPPORTS_MEMSTAT)
88  __free_hook = p;
89 #endif
90 }
91 #endif // !defined(__APPLE__)
92 
93 ////////////////////////////////////////////////////////////////////////////////
94 /// tracZoneMalloc - a static function
95 /// override the default Mac OS X memory zone
96 
97 #if defined (__APPLE__)
98 void TMemStatHook::trackZoneMalloc(zoneMallocHookFunc_t pm,
99  zoneFreeHookFunc_t pf)
100 {
101  malloc_zone_t* zone = malloc_default_zone();
102  if (!zone) {
103  cerr << "Error: Can't get malloc_default_zone" << endl;
104  return;
105  }
106  m_pm = pm;
107  m_pf = pf;
108 
109  original_zone = *zone;
110  zone->malloc = &profile_malloc;
111  zone->calloc = &profile_calloc;
112  zone->valloc = &profile_valloc;
113  zone->free = &profile_free;
114 #if defined(MAC_OS_X_VERSION_10_6)
115  if (zone->version >= 6 && zone->free_definite_size)
116  zone->free_definite_size = &profile_free_definite_size;
117 #endif
118 }
119 ////////////////////////////////////////////////////////////////////////////////
120 /// untrackZoneMalloc - a static function
121 /// set the default Mac OS X memory zone to original
122 
123 void TMemStatHook::untrackZoneMalloc()
124 {
125  malloc_zone_t* zone = malloc_default_zone();
126  if (!zone) {
127  cerr << "Error: Can't get malloc_default_zone" << endl;
128  return;
129  }
130  *zone = original_zone;
131 }
132 ////////////////////////////////////////////////////////////////////////////////
133 /// Mac OS X profiler of malloc calls
134 
135 void* profile_malloc(malloc_zone_t *zone, size_t size)
136 {
137  void* ptr = (*original_zone.malloc)(zone, size);
138  m_pm(ptr, size);
139  return ptr;
140 }
141 
142 ////////////////////////////////////////////////////////////////////////////////
143 /// Mac OS X profiler of calloc calls
144 
145 void* profile_calloc(malloc_zone_t *zone, size_t num_items, size_t size)
146 {
147  void* ptr = (*original_zone.calloc)(zone, num_items, size);
148  m_pm(ptr, size);
149  return ptr;
150 }
151 
152 ////////////////////////////////////////////////////////////////////////////////
153 /// Mac OS X profiler of valloc calls
154 
155 void* profile_valloc(malloc_zone_t *zone, size_t size)
156 {
157  void* ptr = (*original_zone.valloc)(zone, size);
158  m_pm(ptr, size);
159  return ptr;
160 }
161 
162 ////////////////////////////////////////////////////////////////////////////////
163 /// Mac OS X profiler of free calls
164 
165 void profile_free(malloc_zone_t *zone, void *ptr)
166 {
167  (*original_zone.free)(zone, ptr);
168  m_pf(ptr);
169 }
170 
171 ////////////////////////////////////////////////////////////////////////////////
172 /// Mac OS X profiler of free_definite_size calls
173 
174 #if defined(MAC_OS_X_VERSION_10_6)
175 void profile_free_definite_size(malloc_zone_t *zone, void *ptr, size_t size)
176 {
177  (*original_zone.free_definite_size)(zone, ptr, size);
178  m_pf(ptr);
179 }
180 #endif // defined(MAC_OS_X_VERSION_10_6)
181 #endif // defined(__APPLE__)
static void SetFreeHook(FreeHookFunc_t p)
SetFreeHook - a static function Set pointer to function replacing free function.
static void SetMallocHook(MallocHookFunc_t p)
SetMallocHook - a static function Set pointer to function replacing alloc function.
STL namespace.
void *(* MallocHookFunc_t)(size_t size, const void *caller)
Definition: TMemStatHook.h:35
static FreeHookFunc_t GetFreeHook()
GetFreeHook - a static function free function getter.
void(* FreeHookFunc_t)(void *ptr, const void *caller)
Definition: TMemStatHook.h:36
static MallocHookFunc_t GetMallocHook()
GetMallocHook - a static function malloc function getter.