Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
TObjectTable.cxx
Go to the documentation of this file.
1// @(#)root/cont:$Id$
2// Author: Fons Rademakers 11/08/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/** \class TObjectTable
13\ingroup Containers
14This class registers all instances of TObject and its derived
15classes in a hash table. The Add() and Remove() members are called
16from the TObject ctor and dtor, respectively. Using the Print()
17member one can see all currently active objects in the system.
18Using the resource (in .rootrc): Root.ObjectStat one can toggle this
19feature on or off.
20
21Using the compile option R__NOSTATS one can de-active this feature
22for the entire system (for maximum performance in highly time
23critical applications).
24
25The following output has been produced in a ROOT interactive session
26via the command gObjectTable->Print()
27~~~ {.cpp}
28 class cnt on heap size total size heap size
29 ============================================================================
30 TKey 4 4 72 288 288
31 TClass 84 84 80 6720 6720
32 TDataMember 276 276 24 6624 6624
33 TObject 11 11 12 132 132
34 TMethod 1974 1974 64 126336 126336
35 TDataType 34 34 56 1904 1904
36 TList 2328 2328 36 83808 83808
37 TH1F 1 1 448 448 448
38 TText 2688 2688 56 150528 150528
39 TGaxis 1 0 120 120 0
40 TAxis 6 3 88 528 264
41 TBox 57 57 52 2964 2964
42 TLine 118 118 40 4720 4720
43 TWbox 1 1 56 56 56
44 TArrow 1 1 64 64 64
45 TPaveText 59 59 124 7316 7316
46 TPave 1 1 92 92 92
47 TFile 1 1 136 136 136
48 TCanvas 3 3 444 1332 1332
49 TPad 1 1 312 312 312
50 TContextMenu 3 3 48 144 144
51 TMethodArg 2166 2166 44 95304 95304
52 TPaveLabel 1 1 120 120 120
53 THtml 1 1 32 32 32
54 TROOT 1 0 208 208 0
55 TApplication 1 1 28 28 28
56 TFileHandler 1 1 20 20 20
57 TColor 163 163 40 6520 6520
58 TStyle 1 1 364 364 364
59 TRealData 117 117 28 3276 3276
60 TBaseClass 88 88 36 3168 3168
61 THashList 5 5 40 200 200
62 THashTable 5 5 36 180 180
63 TGeometry 1 1 64 64 64
64 TLink 7 7 60 420 420
65 TPostScript 1 1 764 764 764
66 TMinuit 1 1 792 792 792
67 TStopwatch 1 0 56 56 0
68 TRootGuiFactory 1 1 28 28 28
69 TGX11 1 1 172 172 172
70 TUnixSystem 1 1 252 252 252
71 TSignalHandler 1 1 20 20 20
72 TOrdCollection 3 3 40 120 120
73 TEnv 1 1 24 24 24
74 TCling 1 1 208 208 208
75 TBenchmark 1 1 52 52 52
76 TClassTable 1 1 12 12 12
77 TObjectTable 1 1 12 12 12
78 ----------------------------------------------------------------------------
79 Total: 10225 10219 5976 506988 506340
80 ============================================================================
81~~~
82*/
83
84#include "TObjectTable.h"
85#include "TList.h"
86#include "TROOT.h"
87#include "TClass.h"
88#include "TError.h"
89
90
92
93
95
96////////////////////////////////////////////////////////////////////////////////
97/// Create an object table.
98
100{
101 fSize = (Int_t)TMath::NextPrime(tableSize);
102 fTable = new TObject* [fSize];
103 memset(fTable, 0, fSize*sizeof(TObject*));
104 fTally = 0;
105}
106
107////////////////////////////////////////////////////////////////////////////////
108/// Delete TObjectTable.
109
111{
112 delete [] fTable; fTable = nullptr;
113}
114
115////////////////////////////////////////////////////////////////////////////////
116/// Print the object table.
117/// If option ="all" prints the list of all objects with the format
118/// object number, pointer, class name, object name
119
121{
122 TString opt = option;
123 opt.ToLower();
124 if (opt.Contains("all")) {
125 TObject *obj;
126 int i, num = 0;
127 Printf("\nList of all objects");
128 Printf("object address class name");
129 Printf("================================================================================");
130 for (i = 0; i < fSize; i++) {
131 if (!fTable[i]) continue;
132 num++;
133 obj = fTable[i];
134 printf("%-8d 0x%-16zx %-24s %s\n", num, (size_t)obj, obj->ClassName(),
135 obj->GetName());
136 }
137 Printf("================================================================================\n");
138 }
139
140 //print the number of instances per class
142}
143
144////////////////////////////////////////////////////////////////////////////////
145/// Add an object to the object table.
146
148{
149 if (!op) {
150 Error("Add", "op is 0");
151 return;
152 }
153 if (!fTable)
154 return;
155
156 Int_t slot = FindElement(op);
157 if (!fTable[slot]) {
158 fTable[slot] = op;
159 fTally++;
160 if (HighWaterMark())
161 Expand(2 * fSize);
162 }
163}
164
165////////////////////////////////////////////////////////////////////////////////
166/// Add an object to the global object table gObjectTable. If the global
167/// table does not exist create it first. This member function may only
168/// be used by TObject::TObject. Use Add() to add objects to any other
169/// TObjectTable object. This is a static function.
170
172{
173 static Bool_t olock = kFALSE;
174
175 if (!op) {
176 ::Error("TObjectTable::AddObj", "op is 0");
177 return;
178 }
179 if (olock)
180 return;
181
182 if (!gObjectTable) {
183 olock = kTRUE;
184 gObjectTable = new TObjectTable(10000);
185 olock = kFALSE;
187 }
188
189 gObjectTable->Add(op);
190}
191
192////////////////////////////////////////////////////////////////////////////////
193/// Delete all objects stored in the TObjectTable.
194
196{
197 for (int i = 0; i < fSize; i++) {
198 if (fTable[i]) {
199 delete fTable[i];
200 fTable[i] = nullptr;
201 }
202 }
203 fTally = 0;
204}
205
206////////////////////////////////////////////////////////////////////////////////
207/// Remove an object from the object table.
208
210{
211 if (!op) {
212 Error("Remove", "remove 0 from TObjectTable");
213 return;
214 }
215
216 if (!fTable)
217 return;
218
219 Int_t i = FindElement(op);
220 if (!fTable[i]) {
221 Warning("Remove", "0x%zx not found at %d", (size_t)op, i);
222 for (int j = 0; j < fSize; j++) {
223 if (fTable[j] == op) {
224 Error("Remove", "0x%zx found at %d !!!", (size_t)op, j);
225 i = j;
226 }
227 }
228 }
229
230 if (fTable[i]) {
231 fTable[i] = nullptr;
232 FixCollisions(i);
233 fTally--;
234 }
235}
236
237////////////////////////////////////////////////////////////////////////////////
238/// Remove an object from the object table. If op is 0 or not in the table
239/// don't complain. Currently only used by the TClonesArray dtor. Should not
240/// be used anywhere else, except in places where "special" allocation and
241/// de-allocation tricks are performed.
242
244{
245 if (!op) return;
246
247 if (!fTable)
248 return;
249
250 Int_t i = FindElement(op);
251 if (!fTable[i])
252 for (int j = 0; j < fSize; j++)
253 if (fTable[j] == op)
254 i = j;
255
256 fTable[i] = nullptr;
257 FixCollisions(i);
258 fTally--;
259}
260
261////////////////////////////////////////////////////////////////////////////////
262/// Deletes the object table (this static class function calls the dtor).
263
265{
267 delete [] fTable;
268 fTable = nullptr;
269}
270
271////////////////////////////////////////////////////////////////////////////////
272/// Find an object in the object table. Returns the slot where to put
273/// the object. To test if the object is actually already in the table
274/// use PtrIsValid().
275
277{
278 Int_t slot, n;
279 TObject *slotOp;
280
281 if (!fTable)
282 return 0;
283
284 //slot = Int_t(((ULong_t) op >> 2) % fSize);
285 slot = Int_t(TString::Hash(&op, sizeof(TObject*)) % fSize);
286 for (n = 0; n < fSize; n++) {
287 if ((slotOp = fTable[slot]) == nullptr)
288 break;
289 if (op == slotOp)
290 break;
291 if (++slot == fSize)
292 slot = 0;
293 }
294 return slot;
295}
296
297////////////////////////////////////////////////////////////////////////////////
298/// Rehash the object table in case an object has been removed.
299
301{
302 Int_t oldIndex, nextIndex;
303 TObject *nextObject;
304
305 for (oldIndex = index+1; ;oldIndex++) {
306 if (oldIndex >= fSize)
307 oldIndex = 0;
308 nextObject = fTable[oldIndex];
309 if (!nextObject)
310 break;
311 nextIndex = FindElement(nextObject);
312 if (nextIndex != oldIndex) {
313 fTable[nextIndex] = nextObject;
314 fTable[oldIndex] = nullptr;
315 }
316 }
317}
318
319////////////////////////////////////////////////////////////////////////////////
320/// Expand the object table.
321
323{
324 TObject **oldTable = fTable, *op;
325 int oldsize = fSize;
326 newSize = (Int_t)TMath::NextPrime(newSize);
327 fTable = new TObject* [newSize];
328 memset(fTable, 0, newSize*sizeof(TObject*));
329 fSize = newSize;
330 fTally = 0;
331 for (int i = 0; i < oldsize; i++)
332 if ((op = oldTable[i]))
333 Add(op);
334 delete [] oldTable;
335}
336
337////////////////////////////////////////////////////////////////////////////////
338/// Print the object table.
339
341{
342 int n, h, s, ncum = 0, hcum = 0, scum = 0, tcum = 0, thcum = 0;
343
344 if (fTally == 0 || !fTable)
345 return;
346
348
349 Printf("\nObject statistics");
350 Printf("class cnt on heap size total size heap size");
351 Printf("================================================================================");
352 TIter next(gROOT->GetListOfClasses());
353 TClass *cl;
354 while ((cl = (TClass*) next())) {
355 n = cl->GetInstanceCount();
356 h = cl->GetHeapInstanceCount();
357 s = cl->Size();
358 if (n > 0) {
359 Printf("%-24s %8d%11d%9d%14d%13d", cl->GetName(), n, h, s, n*s, h*s);
360 ncum += n;
361 hcum += h;
362 scum += s;
363 tcum += n*s;
364 thcum += h*s;
365 }
366 }
367 Printf("--------------------------------------------------------------------------------");
368 Printf("Total: %8d%11d%9d%14d%13d", ncum, hcum, scum, tcum, thcum);
369 Printf("================================================================================\n");
370}
371
372////////////////////////////////////////////////////////////////////////////////
373/// Histogram all objects according to their classes.
374
376{
377 TObject *op;
378
379 if (!fTable || !TROOT::Initialized())
380 return;
381
382 gROOT->GetListOfClasses()->R__FOR_EACH(TClass,ResetInstanceCount)();
383
384 for (int i = 0; i < fSize; i++)
385 if ((op = fTable[i])) { // attention: no ==
387 op->IsA()->AddInstance(op->IsOnHeap());
388 else
389 Error("UpdateInstCount", "oops 0x%zx\n", (size_t)op);
390 }
391}
392
393////////////////////////////////////////////////////////////////////////////////
394/// Issue a warning in case an object still appears in the table
395/// while it should not.
396
397void *TObjectTable::CheckPtrAndWarn(const char *msg, void *vp)
398{
399 if (fTable && vp && fTable[FindElement((TObject*)vp)]) {
400 Remove((TObject*)vp);
401 Warning("CheckPtrAndWarn", "%s (0x%zx)\n", msg, (size_t)vp);
402 }
403 return vp;
404}
#define h(i)
Definition RSha256.hxx:106
int Int_t
Definition RtypesCore.h:45
constexpr Bool_t kFALSE
Definition RtypesCore.h:101
constexpr Bool_t kTRUE
Definition RtypesCore.h:100
const char Option_t
Definition RtypesCore.h:66
#define ClassImp(name)
Definition Rtypes.h:377
Option_t Option_t option
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t index
TObjectTable * gObjectTable
R__EXTERN TObjectTable * gObjectTable
#define gROOT
Definition TROOT.h:406
void Printf(const char *fmt,...)
Formats a string in a circular formatting buffer and prints the string.
Definition TString.cxx:2503
TClass instances represent classes, structs and namespaces in the ROOT type system.
Definition TClass.h:81
void AddInstance(Bool_t heap=kFALSE)
Definition TClass.h:381
UInt_t GetInstanceCount() const
Definition TClass.h:466
Int_t Size() const
Return size of object of this class.
Definition TClass.cxx:5704
UInt_t GetHeapInstanceCount() const
Definition TClass.h:467
const char * GetName() const override
Returns name of object.
Definition TNamed.h:47
This class registers all instances of TObject and its derived classes in a hash table.
void InstanceStatistics() const
Print the object table.
TObject ** fTable
Int_t FindElement(TObject *obj)
Find an object in the object table.
void Terminate()
Deletes the object table (this static class function calls the dtor).
~TObjectTable()
Delete TObjectTable.
void Remove(TObject *obj)
Remove an object from the object table.
void * CheckPtrAndWarn(const char *msg, void *vp)
Issue a warning in case an object still appears in the table while it should not.
void UpdateInstCount() const
Histogram all objects according to their classes.
TObjectTable(const TObjectTable &)=delete
void Print(Option_t *option="") const override
Print the object table.
void FixCollisions(Int_t index)
Rehash the object table in case an object has been removed.
void Add(TObject *obj)
Add an object to the object table.
void Delete(Option_t *opt="") override
Delete all objects stored in the TObjectTable.
static void AddObj(TObject *obj)
Add an object to the global object table gObjectTable.
void Expand(Int_t newsize)
Expand the object table.
void RemoveQuietly(TObject *obj)
Remove an object from the object table.
Bool_t HighWaterMark()
Mother of all ROOT objects.
Definition TObject.h:41
R__ALWAYS_INLINE Bool_t IsOnHeap() const
Definition TObject.h:152
virtual const char * ClassName() const
Returns name of class to which the object belongs.
Definition TObject.cxx:207
virtual void Warning(const char *method, const char *msgfmt,...) const
Issue warning message.
Definition TObject.cxx:973
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition TObject.cxx:987
virtual TClass * IsA() const
Definition TObject.h:243
static Bool_t Initialized()
Return kTRUE if the TROOT object has been initialized.
Definition TROOT.cxx:2906
Basic string class.
Definition TString.h:139
void ToLower()
Change string to lower-case.
Definition TString.cxx:1182
UInt_t Hash(ECaseCompare cmp=kExact) const
Return hash value.
Definition TString.cxx:677
Bool_t Contains(const char *pat, ECaseCompare cmp=kExact) const
Definition TString.h:632
const Int_t n
Definition legend1.C:16
R__ALWAYS_INLINE bool HasBeenDeleted(const TObject *obj)
Check if the TObject's memory has been deleted.
Definition TObject.h:402
Long_t NextPrime(Long_t x)