TMemStatHook.cxx

Go to the documentation of this file.
00001 // @(#)root/memstat:$Id: TMemStatHook.cxx 35167 2010-09-06 11:09:19Z rdm $
00002 // Author: Anar Manafov (A.Manafov@gsi.de) 2008-03-02
00003 
00004 /*************************************************************************
00005 * Copyright (C) 1995-2010, Rene Brun and Fons Rademakers.               *
00006 * All rights reserved.                                                  *
00007 *                                                                       *
00008 * For the licensing terms see $ROOTSYS/LICENSE.                         *
00009 * For the list of contributors see $ROOTSYS/README/CREDITS.             *
00010 *************************************************************************/
00011 //STD
00012 #include <iostream>
00013 // MemStat
00014 #include "TMemStatHook.h"
00015 #include "RConfig.h"
00016 
00017 // TODO: move it to a separate file
00018 #if defined(__APPLE__)
00019 static malloc_zone_t original_zone;
00020 #ifndef __CINT__
00021 static void* profile_malloc(malloc_zone_t *zone, size_t size);
00022 static void* profile_calloc(malloc_zone_t *zone, size_t num_items, size_t size);
00023 static void* profile_valloc(malloc_zone_t *zone, size_t size);
00024 static void profile_free(malloc_zone_t *zone, void *ptr);
00025 #if defined(MAC_OS_X_VERSION_10_6)
00026 static void profile_free_definite_size(malloc_zone_t *zone, void *ptr, size_t size);
00027 #endif
00028 #endif
00029 
00030 static zoneMallocHookFunc_t m_pm;
00031 static zoneFreeHookFunc_t m_pf;
00032 #else
00033 #include <malloc.h>
00034 #endif
00035 
00036 #if defined(R__GNU) && (defined(R__LINUX) || defined(__APPLE__))
00037 #define SUPPORTS_MEMSTAT
00038 #endif
00039 
00040 
00041 using namespace std;
00042 
00043 #if !defined(__APPLE__)
00044 //______________________________________________________________________________
00045 TMemStatHook::MallocHookFunc_t TMemStatHook::GetMallocHook()
00046 {
00047    // GetMallocHook - a static function
00048    // malloc function getter
00049 
00050 #if defined(SUPPORTS_MEMSTAT)
00051    return __malloc_hook;
00052 #else
00053    return 0;
00054 #endif
00055 }
00056 
00057 //______________________________________________________________________________
00058 TMemStatHook::FreeHookFunc_t TMemStatHook::GetFreeHook()
00059 {
00060    // GetFreeHook - a static function
00061    // free function getter
00062 
00063 #if defined(SUPPORTS_MEMSTAT)
00064    return __free_hook;
00065 #else
00066    return 0;
00067 #endif
00068 }
00069 
00070 //______________________________________________________________________________
00071 void TMemStatHook::SetMallocHook(MallocHookFunc_t p)
00072 {
00073    // SetMallocHook - a static function
00074    // Set pointer to function replacing alloc function
00075 
00076 #if defined(SUPPORTS_MEMSTAT)
00077    __malloc_hook = p;
00078 #endif
00079 }
00080 
00081 //______________________________________________________________________________
00082 void TMemStatHook::SetFreeHook(FreeHookFunc_t p)
00083 {
00084    // SetFreeHook - a static function
00085    // Set pointer to function replacing free function
00086 
00087 #if defined(SUPPORTS_MEMSTAT)
00088    __free_hook = p;
00089 #endif
00090 }
00091 #endif // !defined(__APPLE__)
00092 
00093 //______________________________________________________________________________
00094 #if defined (__APPLE__)
00095 void TMemStatHook::trackZoneMalloc(zoneMallocHookFunc_t pm,
00096                                    zoneFreeHookFunc_t pf)
00097 {
00098    // tracZoneMalloc - a static function
00099    // override the defualt Mac OS X memory zone
00100 
00101    malloc_zone_t* zone = malloc_default_zone();
00102    if (!zone) {
00103       cerr << "Error: Can't get malloc_default_zone" << endl;
00104       return;
00105    }
00106    m_pm = pm;
00107    m_pf = pf;
00108 
00109    original_zone = *zone;
00110    zone->malloc = &profile_malloc;
00111    zone->calloc = &profile_calloc;
00112    zone->valloc = &profile_valloc;
00113    zone->free = &profile_free;
00114 #if defined(MAC_OS_X_VERSION_10_6)
00115    if (zone->version >= 6 && zone->free_definite_size)
00116       zone->free_definite_size = &profile_free_definite_size;
00117 #endif
00118 }
00119 //______________________________________________________________________________
00120 void TMemStatHook::untrackZoneMalloc()
00121 {
00122    // untrackZoneMalloc - a static function
00123    // set the defualt Mac OS X memory zone to original
00124 
00125    malloc_zone_t* zone = malloc_default_zone();
00126    if (!zone) {
00127       cerr << "Error: Can't get malloc_default_zone" << endl;
00128       return;
00129    }
00130    *zone = original_zone;
00131 }
00132 //______________________________________________________________________________
00133 void* profile_malloc(malloc_zone_t *zone, size_t size)
00134 {
00135    // Mac OS X profiler of malloc calls
00136 
00137    void* ptr = (*original_zone.malloc)(zone, size);
00138    m_pm(ptr, size);
00139    return ptr;
00140 }
00141 
00142 //______________________________________________________________________________
00143 void* profile_calloc(malloc_zone_t *zone, size_t num_items, size_t size)
00144 {
00145    // Mac OS X profiler of calloc calls
00146 
00147    void* ptr = (*original_zone.calloc)(zone, num_items, size);
00148    m_pm(ptr, size);
00149    return ptr;
00150 }
00151 
00152 //______________________________________________________________________________
00153 void* profile_valloc(malloc_zone_t *zone, size_t size)
00154 {
00155    // Mac OS X profiler of valloc calls
00156 
00157    void* ptr = (*original_zone.valloc)(zone, size);
00158    m_pm(ptr, size);
00159    return ptr;
00160 }
00161 
00162 //______________________________________________________________________________
00163 void profile_free(malloc_zone_t *zone, void *ptr)
00164 {
00165    // Mac OS X profiler of free calls
00166 
00167    (*original_zone.free)(zone, ptr);
00168    m_pf(ptr);
00169 }
00170 
00171 //______________________________________________________________________________
00172 #if defined(MAC_OS_X_VERSION_10_6)
00173 void profile_free_definite_size(malloc_zone_t *zone, void *ptr, size_t size)
00174 {
00175    // Mac OS X profiler of free_definite_size calls
00176 
00177    (*original_zone.free_definite_size)(zone, ptr, size);
00178    m_pf(ptr);
00179 }
00180 #endif // defined(MAC_OS_X_VERSION_10_6)
00181 #endif // defined(__APPLE__)

Generated on Tue Jul 5 14:44:00 2011 for ROOT_528-00b_version by  doxygen 1.5.1