#include <stdlib.h>
#include "TROOT.h"
#include "TObjectTable.h"
#include "TError.h"
#include "TString.h"
#include "TVirtualMutex.h"
#include "TInterpreter.h"
#if !defined(R__NOSTATS)
# define MEM_DEBUG
# define MEM_STAT
# define MEM_CHECKOBJECTPOINTERS
#endif
#if defined(MEM_STAT) && !defined(MEM_DEBUG)
# define MEM_DEBUG
#endif
#ifdef MEM_DEBUG
# ifdef R__B64
# define storage_size(p) ((size_t)(((size_t*)p)[-1]))
# else
# define storage_size(p) ((size_t)(((int*)p)[-2]))
# endif
#else
# define storage_size(p) ((size_t)0)
#endif
#define PVOID (-1)
ULong_t TStorage::fgHeapBegin = (ULong_t)-1L;
ULong_t TStorage::fgHeapEnd;
size_t TStorage::fgMaxBlockSize;
FreeHookFun_t TStorage::fgFreeHook;
void *TStorage::fgFreeHookData;
ReAllocFun_t TStorage::fgReAllocHook;
ReAllocCFun_t TStorage::fgReAllocCHook;
Bool_t TStorage::fgHasCustomNewDelete;
ClassImp(TStorage)
static const char *gSpaceErr = "storage exhausted";
const size_t kObjMaxSize = 10024;
static Bool_t gMemStatistics;
static Int_t gAllocated[kObjMaxSize], gFreed[kObjMaxSize];
static Int_t gAllocatedTotal, gFreedTotal;
static void **gTraceArray = 0;
static Int_t gTraceCapacity = 10, gTraceIndex = 0,
gMemSize = -1, gMemIndex = -1;
void TStorage::EnterStat(size_t size, void *p)
{
TStorage::SetMaxBlockSize(TMath::Max(TStorage::GetMaxBlockSize(), size));
if (!gMemStatistics) return;
if ((Int_t)size == gMemSize) {
if (gTraceIndex == gMemIndex)
Fatal("EnterStat", "trapped allocation %d", gMemIndex);
if (!gTraceArray)
gTraceArray = (void**) malloc(sizeof(void*)*gTraceCapacity);
if (gTraceIndex >= gTraceCapacity) {
gTraceCapacity = gTraceCapacity*2;
gTraceArray = (void**) realloc(gTraceArray, sizeof(void*)*gTraceCapacity);
}
gTraceArray[gTraceIndex++] = p;
}
if (size >= kObjMaxSize)
gAllocated[kObjMaxSize-1]++;
else
gAllocated[size]++;
gAllocatedTotal += size;
}
void TStorage::RemoveStat(void *vp)
{
if (!gMemStatistics) return;
size_t size = storage_size(vp);
if ((Int_t)size == gMemSize) {
for (int i = 0; i < gTraceIndex; i++)
if (gTraceArray[i] == vp) {
gTraceArray[i] = 0;
break;
}
}
if (size >= kObjMaxSize)
gFreed[kObjMaxSize-1]++;
else
gFreed[size]++;
gFreedTotal += size;
}
void *TStorage::Alloc(size_t size)
{
static const char *where = "TStorage::Alloc";
#ifndef WIN32
void *vp = ::operator new[](size);
#else
void *vp = ::operator new(size);
#endif
if (vp == 0)
Fatal(where, gSpaceErr);
return vp;
}
void TStorage::Dealloc(void *ptr)
{
#ifndef WIN32
::operator delete[](ptr);
#else
::operator delete(ptr);
#endif
}
void *TStorage::ReAlloc(void *ovp, size_t size)
{
R__LOCKGUARD(gGlobalMutex);
if (fgReAllocHook && fgHasCustomNewDelete && !TROOT::MemCheck())
return (*fgReAllocHook)(ovp, size);
static const char *where = "TStorage::ReAlloc";
#ifndef WIN32
void *vp = ::operator new[](size);
#else
void *vp = ::operator new(size);
#endif
if (vp == 0)
Fatal(where, gSpaceErr);
if (ovp == 0)
return vp;
memmove(vp, ovp, size);
#ifndef WIN32
::operator delete[](ovp);
#else
::operator delete(ovp);
#endif
return vp;
}
void *TStorage::ReAlloc(void *ovp, size_t size, size_t oldsize)
{
R__LOCKGUARD(gGlobalMutex);
if (fgReAllocCHook && fgHasCustomNewDelete && !TROOT::MemCheck())
return (*fgReAllocCHook)(ovp, size, oldsize);
static const char *where = "TStorage::ReAlloc";
if (oldsize == size)
return ovp;
#ifndef WIN32
void *vp = ::operator new[](size);
#else
void *vp = ::operator new(size);
#endif
if (vp == 0)
Fatal(where, gSpaceErr);
if (ovp == 0)
return vp;
if (size > oldsize) {
memcpy(vp, ovp, oldsize);
memset((char*)vp+oldsize, 0, size-oldsize);
} else
memcpy(vp, ovp, size);
#ifndef WIN32
::operator delete[](ovp);
#else
::operator delete(ovp);
#endif
return vp;
}
char *TStorage::ReAllocChar(char *ovp, size_t size, size_t oldsize)
{
R__LOCKGUARD(gGlobalMutex);
static const char *where = "TStorage::ReAllocChar";
char *vp;
if (ovp == 0) {
vp = new char[size];
if (vp == 0)
Fatal(where, gSpaceErr);
return vp;
}
if (oldsize == size)
return ovp;
vp = new char[size];
if (vp == 0)
Fatal(where, gSpaceErr);
if (size > oldsize) {
memcpy(vp, ovp, oldsize);
memset((char*)vp+oldsize, 0, size-oldsize);
} else
memcpy(vp, ovp, size);
delete [] ovp;
return vp;
}
Int_t *TStorage::ReAllocInt(Int_t *ovp, size_t size, size_t oldsize)
{
R__LOCKGUARD(gGlobalMutex);
static const char *where = "TStorage::ReAllocInt";
Int_t *vp;
if (ovp == 0) {
vp = new Int_t[size];
if (vp == 0)
Fatal(where, gSpaceErr);
return vp;
}
if (oldsize == size)
return ovp;
vp = new Int_t[size];
if (vp == 0)
Fatal(where, gSpaceErr);
if (size > oldsize) {
memcpy(vp, ovp, oldsize*sizeof(Int_t));
memset((Int_t*)vp+oldsize, 0, (size-oldsize)*sizeof(Int_t));
} else
memcpy(vp, ovp, size*sizeof(Int_t));
delete [] ovp;
return vp;
}
void *TStorage::ObjectAlloc(size_t sz)
{
R__LOCKGUARD(gGlobalMutex);
ULong_t space = (ULong_t) ::operator new(sz);
AddToHeap(space, space+sz);
return (void*) space;
}
void *TStorage::ObjectAlloc(size_t , void *vp)
{
return vp;
}
void TStorage::ObjectDealloc(void *vp)
{
R__LOCKGUARD(gGlobalMutex);
#ifndef NOCINT
Long_t gvp = 0;
if (gCint) gvp = gCint->Getgvp();
if ((Long_t)vp == gvp && gvp != (Long_t)PVOID)
return;
#endif
::operator delete(vp);
}
void TStorage::ObjectDealloc(void *vp, void *ptr)
{
if (vp && ptr) { }
}
void TStorage::SetFreeHook(FreeHookFun_t fh, void *data)
{
fgFreeHook = fh;
fgFreeHookData = data;
}
void TStorage::SetReAllocHooks(ReAllocFun_t rh1, ReAllocCFun_t rh2)
{
fgReAllocHook = rh1;
fgReAllocCHook = rh2;
}
void TStorage::PrintStatistics()
{
R__LOCKGUARD(gGlobalMutex);
#if defined(MEM_DEBUG) && defined(MEM_STAT)
if (!gMemStatistics || !HasCustomNewDelete())
return;
Printf("Heap statistics");
Printf("%12s%12s%12s%12s", "size", "alloc", "free", "diff");
Printf("================================================");
int i;
for (i = 0; i < (int)kObjMaxSize; i++)
if (gAllocated[i] != gFreed[i])
Printf("%12d%12d%12d%12d", i, gAllocated[i], gFreed[i],
gAllocated[i]-gFreed[i]);
if (gAllocatedTotal != gFreedTotal) {
Printf("------------------------------------------------");
Printf("Total: %12d%12d%12d", gAllocatedTotal, gFreedTotal,
gAllocatedTotal-gFreedTotal);
}
if (gMemSize != -1) {
Printf("------------------------------------------------");
for (i= 0; i < gTraceIndex; i++)
if (gTraceArray[i])
Printf("block %d of size %d not freed", i, gMemSize);
}
Printf("================================================");
Printf("");
#endif
}
void TStorage::EnableStatistics(int size, int ix)
{
#ifdef MEM_STAT
gMemSize = size;
gMemIndex = ix;
gMemStatistics = kTRUE;
#else
int idum = size; int iidum = ix;
#endif
}
ULong_t TStorage::GetHeapBegin()
{
return fgHeapBegin;
}
ULong_t TStorage::GetHeapEnd()
{
return fgHeapEnd;
}
void *TStorage::GetFreeHookData()
{
return fgFreeHookData;
}
Bool_t TStorage::HasCustomNewDelete()
{
return fgHasCustomNewDelete;
}
void TStorage::SetCustomNewDelete()
{
fgHasCustomNewDelete = kTRUE;
}
#ifdef WIN32
void TStorage::AddToHeap(ULong_t begin, ULong_t end)
{
if (begin < fgHeapBegin) fgHeapBegin = begin;
if (end > fgHeapEnd) fgHeapEnd = end;
}
Bool_t TStorage::IsOnHeap(void *p)
{
return (ULong_t)p >= fgHeapBegin && (ULong_t)p < fgHeapEnd;
}
size_t TStorage::GetMaxBlockSize()
{
return fgMaxBlockSize;
}
void TStorage::SetMaxBlockSize(size_t size)
{
fgMaxBlockSize = size;
}
FreeHookFun_t TStorage::GetFreeHook()
{
return fgFreeHook;
}
#endif
Last change: Wed Jun 25 08:53:27 2008
Last generated: 2008-06-25 08:53
This page has been automatically generated. If you have any comments or suggestions about the page layout send a mail to ROOT support, or contact the developers with any questions or problems regarding ROOT.