#ifndef ROOT_TMemStatMng
#define ROOT_TMemStatMng
#include <map>
#ifndef ROOT_TObject
#include "TObject.h"
#endif
#ifndef ROOT_TTimeStamp
#include "TTimeStamp.h"
#endif
#ifndef ROOT_TMemStatHook
#include "TMemStatHook.h"
#endif
#ifndef ROOT_TMemStatDef
#include "TMemStatDef.h"
#endif
class TTree;
class TFile;
class TH1I;
class TObjArray;
namespace Memstat {
class TMemStatFAddrContainer {
typedef std::map<ULong_t, Int_t> Container_t;
typedef Container_t::iterator pos_type;
typedef Container_t::value_type value_type;
public:
bool add(ULong_t addr, Int_t idx) {
std::pair<pos_type, bool> ret = fContainer.insert(value_type(addr, idx));
return (ret.second);
}
Int_t find(ULong_t addr) {
pos_type iter = fContainer.find(addr);
if(fContainer.end() == iter)
return -1;
return iter->second;
}
private:
Container_t fContainer;
};
const UShort_t g_digestSize = 16;
struct SCustomDigest {
SCustomDigest() {
memset(fValue, 0, g_digestSize);
}
SCustomDigest(UChar_t _val[g_digestSize]) {
memcpy(fValue, _val, g_digestSize);
}
UChar_t fValue[g_digestSize];
};
inline bool operator< (const SCustomDigest &a, const SCustomDigest &b)
{
for(int i = 0; i < g_digestSize; ++i) {
if(a.fValue[i] != b.fValue[i])
return (a.fValue[i] < b.fValue[i]);
}
return false;
}
class TMemStatMng: public TObject {
typedef std::map<SCustomDigest, Int_t> CRCSet_t;
private:
TMemStatMng();
virtual ~TMemStatMng();
public:
void Enable();
void Disable();
static TMemStatMng* GetInstance();
static void Close();
void SetBufferSize(Int_t buffersize);
void SetMaxCalls(Int_t maxcalls);
public:
void SetUseGNUBuiltinBacktrace(Bool_t newVal) {
fUseGNUBuiltinBacktrace = newVal;
}
protected:
#if !defined(__APPLE__)
TMemStatHook::MallocHookFunc_t fPreviousMallocHook;
TMemStatHook::FreeHookFunc_t fPreviousFreeHook;
#endif
void Init();
void AddPointer(void *ptr, Int_t size);
void FillTree();
static void *AllocHook(size_t size, const void* );
static void FreeHook(void* ptr, const void* );
static void MacAllocHook(void *ptr, size_t size);
static void MacFreeHook(void *ptr);
Int_t generateBTID(UChar_t *CRCdigest, Int_t stackEntries,
void **stackPointers);
TFile* fDumpFile;
TTree *fDumpTree;
static TMemStatMng *fgInstance;
static void *fgStackTop;
Bool_t fUseGNUBuiltinBacktrace;
TTimeStamp fTimeStamp;
Double_t fBeginTime;
ULong64_t fPos;
Int_t fTimems;
Int_t fNBytes;
Int_t fBtID;
Int_t fMaxCalls;
Int_t fBufferSize;
Int_t fBufN;
ULong64_t *fBufPos;
Int_t *fBufTimems;
Int_t *fBufNBytes;
Int_t *fBufBtID;
Int_t *fIndex;
Bool_t *fMustWrite;
private:
TMemStatFAddrContainer fFAddrs;
TObjArray *fFAddrsList;
TH1I *fHbtids;
CRCSet_t fBTChecksums;
Int_t fBTCount;
UInt_t fBTIDCount;
TNamed *fSysInfo;
ClassDef(TMemStatMng, 0)
};
}
#endif