Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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// #include "RConfigure.h" // included via Rtypes.h
25#include "Rtypes.h"
26
27typedef void (*FreeHookFun_t)(void*, void *addr, size_t);
28typedef void *(*ReAllocFun_t)(void*, size_t);
29typedef void *(*ReAllocCFun_t)(void*, size_t, size_t);
30typedef char *(*ReAllocCharFun_t)(char*, size_t, size_t);
31
32
33class TStorage {
34
35private:
36 static size_t fgMaxBlockSize; // largest block allocated
37 static FreeHookFun_t fgFreeHook; // function called on free
38 static void *fgFreeHookData; // data used by this function
39 static ReAllocCFun_t fgReAllocCHook; // custom ReAlloc with length check
40 static Bool_t fgHasCustomNewDelete; // true if using ROOT's new/delete
41
42 //----- Private bits, clients can only test but not change them
43 enum {
44 kIsOnHeap = 0x01000000, ///< object is on heap
45 };
46
47public:
48 static const UInt_t kObjectAllocMemValue = 0x99999999;
49 // magic number for ObjectAlloc
50
51public:
52 virtual ~TStorage() { }
53
55 static void *GetFreeHookData();
56 static size_t GetMaxBlockSize();
57 static void *Alloc(size_t size);
58 static void Dealloc(void *ptr);
59 static void *ReAlloc(void *vp, size_t size, size_t oldsize);
60 static char *ReAllocChar(char *vp, size_t size, size_t oldsize);
61 static Int_t *ReAllocInt(Int_t *vp, size_t size, size_t oldsize);
62 static void *ObjectAlloc(size_t size);
63 static void *ObjectAllocArray(size_t size);
64 static void *ObjectAlloc(size_t size, void *vp);
65 static void ObjectDealloc(void *vp);
66 static void ObjectDealloc(void *vp, size_t size);
67 static void ObjectDealloc(void *vp, void *ptr);
68
69 static void EnterStat(size_t size, void *p);
70 static void RemoveStat(void *p);
71 static void PrintStatistics();
72 static void SetMaxBlockSize(size_t size);
73 static void SetFreeHook(FreeHookFun_t func, void *data);
75 static void SetCustomNewDelete();
76 static void EnableStatistics(int size= -1, int ix= -1);
77
79
80 static Bool_t FilledByObjectAlloc(volatile const UInt_t* const member);
81 static void UpdateIsOnHeap(volatile const UInt_t &uniqueID, volatile UInt_t &bits);
82
83 ClassDef(TStorage,0) //Storage manager class
84};
85
86inline Bool_t TStorage::FilledByObjectAlloc(volatile const UInt_t *const member) {
87 //called by TObject's constructor to determine if object was created by call to new
88
89 // This technique is necessary as there is one stack per thread
90 // and we can not rely on comparison with the current stack memory position.
91 // Note that a false positive (this routine returning true for an object
92 // created on the stack) requires the previous stack value to have been
93 // set to exactly kObjectAllocMemValue at exactly the right position (i.e.
94 // where this object's fUniqueID is located.
95 // The consequence of a false positive will be visible if and only if
96 // the object is auto-added to a TDirectory (i.e. TTree, TH*, TGraph,
97 // TEventList) or explicitly added to the directory by the user
98 // and
99 // the TDirectory (or TFile) object is created on the stack *before*
100 // the object.
101 // The consequence would be that those objects would be deleted twice, once
102 // by the TDirectory and once automatically when going out of scope
103 // (and thus quite visible). A false negative (which is not possible with
104 // this implementation) would have been a silent memory leak.
105
106 // This will be reported by valgrind as uninitialized memory reads for
107 // object created on the stack, use $ROOTSYS/etc/valgrind-root.supp
109 return *member == kObjectAllocMemValue; // NOLINT
111}
112
113// Assign the kIsOnHeap bit in 'bits' based on the pattern seen in uniqueID.
114// See Storage::FilledByObjectAlloc for details.
115// This routine is marked as inline with attribute noinline so that it never
116// inlined and thus can be used in a valgrind suppression file to suppress
117// the known/intentional uninitialized memory read but still be a 'quick'
118// function call to avoid losing performance at object creation.
119// Moving the function into the source file, results in doubling of the
120// overhead (compared to inlining)
121R__NEVER_INLINE void TStorage::UpdateIsOnHeap(volatile const UInt_t &uniqueID, volatile UInt_t &bits) {
123 bits = bits | kIsOnHeap;
124 else
125 bits = bits & ~kIsOnHeap;
126}
127
128
129inline size_t TStorage::GetMaxBlockSize() { return fgMaxBlockSize; }
130
132
134
135namespace ROOT {
136namespace Internal {
137using FreeIfTMapFile_t = bool(void*);
138using GetMapFileMapllocDesc_t = void *(void*);
142}
143}
144
145#endif
#define R__EXTERN
Definition DllImport.h:26
#define R__INTENTIONALLY_UNINIT_END
Definition RConfig.hxx:550
#define R__NEVER_INLINE
Definition RConfig.hxx:572
#define R__INTENTIONALLY_UNINIT_BEGIN
Definition RConfig.hxx:549
size_t size(const MatrixT &matrix)
retrieve the size of a square matrix
bool Bool_t
Boolean (0=false, 1=true) (bool)
Definition RtypesCore.h:77
int Int_t
Signed integer 4 bytes (int)
Definition RtypesCore.h:59
unsigned int UInt_t
Unsigned integer 4 bytes (unsigned int)
Definition RtypesCore.h:60
#define ClassDef(name, id)
Definition Rtypes.h:344
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
winID h TVirtualViewer3D TVirtualGLPainter p
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void data
void *(* ReAllocCFun_t)(void *, size_t, size_t)
Definition TStorage.h:29
void *(* ReAllocFun_t)(void *, size_t)
Definition TStorage.h:28
void(* FreeHookFun_t)(void *, void *addr, size_t)
Definition TStorage.h:27
Storage manager.
Definition TStorage.h:33
static ReAllocCFun_t fgReAllocCHook
Definition TStorage.h:39
static void RemoveStat(void *p)
Register a memory free operation.
Definition TStorage.cxx:129
static void * Alloc(size_t size)
Allocate a block of memory, that later can be resized using TStorage::ReAlloc().
Definition TStorage.cxx:152
static Bool_t fgHasCustomNewDelete
Definition TStorage.h:40
static char * ReAllocChar(char *vp, size_t size, size_t oldsize)
Reallocate (i.e.
Definition TStorage.cxx:228
static FreeHookFun_t fgFreeHook
Definition TStorage.h:37
static Bool_t HasCustomNewDelete()
return the has custom delete flag
Definition TStorage.cxx:432
static void EnterStat(size_t size, void *p)
Register a memory allocation operation.
Definition TStorage.cxx:99
@ kIsOnHeap
object is on heap
Definition TStorage.h:44
static void ObjectDealloc(void *vp)
Used to deallocate a TObject on the heap (via TObject::operator delete()).
Definition TStorage.cxx:322
static void SetFreeHook(FreeHookFun_t func, void *data)
Set a free handler.
Definition TStorage.cxx:348
static void SetCustomNewDelete()
set the has custom delete flag
Definition TStorage.cxx:440
static void * GetFreeHookData()
return static free hook data
Definition TStorage.cxx:424
static void SetMaxBlockSize(size_t size)
Definition TStorage.h:131
static void * fgFreeHookData
Definition TStorage.h:38
static const UInt_t kObjectAllocMemValue
Definition TStorage.h:48
static void UpdateIsOnHeap(volatile const UInt_t &uniqueID, volatile UInt_t &bits)
Definition TStorage.h:121
static void Dealloc(void *ptr)
De-allocate block of memory, that was allocated via TStorage::Alloc().
Definition TStorage.cxx:170
static size_t fgMaxBlockSize
Definition TStorage.h:36
static Int_t * ReAllocInt(Int_t *vp, size_t size, size_t oldsize)
Reallocate (i.e.
Definition TStorage.cxx:258
static void SetReAllocHooks(ReAllocFun_t func1, ReAllocCFun_t func2)
Set a custom ReAlloc handlers.
Definition TStorage.cxx:358
static void * ObjectAllocArray(size_t size)
Used to allocate array of TObject on the heap (via TObject::operator new[]()).
Definition TStorage.cxx:303
static Bool_t FilledByObjectAlloc(volatile const UInt_t *const member)
Definition TStorage.h:86
virtual ~TStorage()
Definition TStorage.h:52
static void EnableStatistics(int size=-1, int ix=-1)
Enable memory usage statistics gathering.
Definition TStorage.cxx:410
static size_t GetMaxBlockSize()
Definition TStorage.h:129
static void * ReAlloc(void *vp, size_t size, size_t oldsize)
Reallocate (i.e.
Definition TStorage.cxx:183
static void * ObjectAlloc(size_t size)
Used to allocate a TObject on the heap (via TObject::operator new()).
Definition TStorage.cxx:291
static void PrintStatistics()
Print memory usage statistics.
Definition TStorage.cxx:366
static FreeHookFun_t GetFreeHook()
Definition TStorage.h:133
R__EXTERN GetMapFileMapllocDesc_t * gGetMapFileMallocDesc
Definition TStorage.h:140
R__EXTERN FreeIfTMapFile_t * gFreeIfTMapFile
Definition TStorage.h:139
R__EXTERN void * gMmallocDesc
Definition TStorage.h:141
Namespace for new ROOT classes and functions.