Logo ROOT   6.07/09
Reference Guide
TStorage.h
Go to the documentation of this file.
1 // @(#)root/base:$Id$
2 // Author: Fons Rademakers 29/07/95
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2000, 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 
12 #ifndef ROOT_TStorage
13 #define ROOT_TStorage
14 
15 
16 //////////////////////////////////////////////////////////////////////////
17 // //
18 // TStorage //
19 // //
20 // Storage manager. //
21 // //
22 //////////////////////////////////////////////////////////////////////////
23 
24 #ifndef ROOT_Rtypes
25 #include "Rtypes.h"
26 #endif
27 
28 typedef void (*FreeHookFun_t)(void*, void *addr, size_t);
29 typedef void *(*ReAllocFun_t)(void*, size_t);
30 typedef void *(*ReAllocCFun_t)(void*, size_t, size_t);
31 typedef char *(*ReAllocCharFun_t)(char*, size_t, size_t);
32 
33 
34 class TStorage {
35 
36 private:
37  static size_t fgMaxBlockSize; // largest block allocated
38  static FreeHookFun_t fgFreeHook; // function called on free
39  static void *fgFreeHookData; // data used by this function
40  static ReAllocFun_t fgReAllocHook; // custom ReAlloc
41  static ReAllocCFun_t fgReAllocCHook; // custom ReAlloc with length check
42  static Bool_t fgHasCustomNewDelete; // true if using ROOT's new/delete
43  static const UInt_t kObjectAllocMemValue = 0x99999999;
44  // magic number for ObjectAlloc
45 
46 public:
47  virtual ~TStorage() { }
48 
49  static ULong_t GetHeapBegin();
50  static ULong_t GetHeapEnd();
51  static FreeHookFun_t GetFreeHook();
52  static void *GetFreeHookData();
53  static size_t GetMaxBlockSize();
54  static void *Alloc(size_t size);
55  static void Dealloc(void *ptr);
56  static void *ReAlloc(void *vp, size_t size);
57  static void *ReAlloc(void *vp, size_t size, size_t oldsize);
58  static char *ReAllocChar(char *vp, size_t size, size_t oldsize);
59  static Int_t *ReAllocInt(Int_t *vp, size_t size, size_t oldsize);
60  static void *ObjectAlloc(size_t size);
61  static void *ObjectAllocArray(size_t size);
62  static void *ObjectAlloc(size_t size, void *vp);
63  static void ObjectDealloc(void *vp);
64  static void ObjectDealloc(void *vp, void *ptr);
65 
66  static void EnterStat(size_t size, void *p);
67  static void RemoveStat(void *p);
68  static void PrintStatistics();
69  static void SetMaxBlockSize(size_t size);
70  static void SetFreeHook(FreeHookFun_t func, void *data);
71  static void SetReAllocHooks(ReAllocFun_t func1, ReAllocCFun_t func2);
72  static void SetCustomNewDelete();
73  static void EnableStatistics(int size= -1, int ix= -1);
74 
75  static Bool_t HasCustomNewDelete();
76 
77  // only valid after call to a TStorage allocating method
78  static void AddToHeap(ULong_t begin, ULong_t end);
79  static Bool_t IsOnHeap(void *p);
80 
81  static Bool_t FilledByObjectAlloc(UInt_t* member);
82 
83  ClassDef(TStorage,0) //Storage manager class
84 };
85 
86 #ifndef WIN32
88  //called by TObject's constructor to determine if object was created by call to new
89 
90  // This technique is necessary as there is one stack per thread
91  // and we can not rely on comparison with the current stack memory position.
92  // Note that a false positive (this routine returning true for an object
93  // created on the stack) requires the previous stack value to have been
94  // set to exactly kObjectAllocMemValue at exactly the right position (i.e.
95  // where this object's fUniqueID is located.
96  // The consequence of a false positive will be visible if and only if
97  // the object is auto-added to a TDirectory (i.e. TTree, TH*, TGraph,
98  // TEventList) or explicitly added to the directory by the user
99  // and
100  // the TDirectory (or TFile) object is created on the stack *before*
101  // the object.
102  // The consequence would be that those objects would be deleted twice, once
103  // by the TDirectory and once automatically when going out of scope
104  // (and thus quite visible). A false negative (which is not posible with
105  // this implementation) would have been a silent memory leak.
106 
107  // This will be reported by valgrind as uninitialized memory reads for
108  // object created on the stack, use $ROOTSYS/etc/valgrind-root.supp
109  return *member == kObjectAllocMemValue;
110 }
111 
112 inline size_t TStorage::GetMaxBlockSize() { return fgMaxBlockSize; }
113 
114 inline void TStorage::SetMaxBlockSize(size_t size) { fgMaxBlockSize = size; }
115 
117 #endif
118 
119 #endif
static void Dealloc(void *ptr)
De-allocate block of memory, that was allocated via TStorage::Alloc().
Definition: TStorage.cxx:165
void *(* ReAllocFun_t)(void *, size_t)
Definition: TStorage.h:29
static const UInt_t kObjectAllocMemValue
Definition: TStorage.h:43
static ULong_t GetHeapBegin()
Definition: TStorage.cxx:447
static void * ObjectAlloc(size_t size)
Used to allocate a TObject on the heap (via TObject::operator new()).
Definition: TStorage.cxx:323
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
static ULong_t GetHeapEnd()
Definition: TStorage.cxx:456
static ReAllocCFun_t fgReAllocCHook
Definition: TStorage.h:41
static void AddToHeap(ULong_t begin, ULong_t end)
add a range to the heap
Definition: TStorage.cxx:491
#define ClassDef(name, id)
Definition: Rtypes.h:254
static char * ReAllocChar(char *vp, size_t size, size_t oldsize)
Reallocate (i.e.
Definition: TStorage.cxx:260
static void * fgFreeHookData
Definition: TStorage.h:39
static Bool_t fgHasCustomNewDelete
Definition: TStorage.h:42
static size_t GetMaxBlockSize()
Definition: TStorage.h:112
static void * GetFreeHookData()
return static free hook data
Definition: TStorage.cxx:466
static FreeHookFun_t GetFreeHook()
Definition: TStorage.h:116
unsigned int UInt_t
Definition: RtypesCore.h:42
static void EnableStatistics(int size=-1, int ix=-1)
Enable memory usage statistics gathering.
Definition: TStorage.cxx:434
static Bool_t HasCustomNewDelete()
return the has custom delete flag
Definition: TStorage.cxx:474
static void SetCustomNewDelete()
set the has custom delete flag
Definition: TStorage.cxx:482
static FreeHookFun_t fgFreeHook
Definition: TStorage.h:38
static void SetReAllocHooks(ReAllocFun_t func1, ReAllocCFun_t func2)
Set a custom ReAlloc handlers.
Definition: TStorage.cxx:381
static void * Alloc(size_t size)
Allocate a block of memory, that later can be resized using TStorage::ReAlloc().
Definition: TStorage.cxx:147
static void SetFreeHook(FreeHookFun_t func, void *data)
Set a free handler.
Definition: TStorage.cxx:371
static Bool_t IsOnHeap(void *p)
is object at p in the heap?
Definition: TStorage.cxx:499
unsigned long ULong_t
Definition: RtypesCore.h:51
double func(double *x, double *p)
Definition: stressTF1.cxx:213
void *(* ReAllocCFun_t)(void *, size_t, size_t)
Definition: TStorage.h:30
static void * ReAlloc(void *vp, size_t size)
Reallocate (i.e.
Definition: TStorage.cxx:178
void(* FreeHookFun_t)(void *, void *addr, size_t)
Definition: TStorage.h:28
typedef void((*Func_t)())
static void RemoveStat(void *p)
Register a memory free operation.
Definition: TStorage.cxx:124
static void EnterStat(size_t size, void *p)
Register a memory allocation operation.
Definition: TStorage.cxx:94
static void SetMaxBlockSize(size_t size)
Definition: TStorage.h:114
static void * ObjectAllocArray(size_t size)
Used to allocate array of TObject on the heap (via TObject::operator new[]()).
Definition: TStorage.cxx:335
static Bool_t FilledByObjectAlloc(UInt_t *member)
Definition: TStorage.h:87
static ReAllocFun_t fgReAllocHook
Definition: TStorage.h:40
static size_t fgMaxBlockSize
Definition: TStorage.h:37
static void ObjectDealloc(void *vp)
Used to deallocate a TObject on the heap (via TObject::operator delete()).
Definition: TStorage.cxx:354
static void PrintStatistics()
Print memory usage statistics.
Definition: TStorage.cxx:390
Storage manager.
Definition: TStorage.h:34
static Int_t * ReAllocInt(Int_t *vp, size_t size, size_t oldsize)
Reallocate (i.e.
Definition: TStorage.cxx:290
virtual ~TStorage()
Definition: TStorage.h:47