Logo ROOT  
Reference Guide
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 = 0;
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] == 0) {
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] = 0;
201 }
202 }
203 fTally = 0;
204}
205
206////////////////////////////////////////////////////////////////////////////////
207/// Remove an object from the object table.
208
210{
211 if (op == 0) {
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] == 0) {
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] = 0;
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 == 0) return;
246
247 if (!fTable)
248 return;
249
250 Int_t i = FindElement(op);
251 if (fTable[i] == 0)
252 for (int j = 0; j < fSize; j++)
253 if (fTable[j] == op)
254 i = j;
255
256 fTable[i] = 0;
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; fTable = 0;
268}
269
270////////////////////////////////////////////////////////////////////////////////
271/// Find an object in the object table. Returns the slot where to put
272/// the object. To test if the object is actually already in the table
273/// use PtrIsValid().
274
276{
277 Int_t slot, n;
278 TObject *slotOp;
279
280 if (!fTable)
281 return 0;
282
283 //slot = Int_t(((ULong_t) op >> 2) % fSize);
284 slot = Int_t(TString::Hash(&op, sizeof(TObject*)) % fSize);
285 for (n = 0; n < fSize; n++) {
286 if ((slotOp = fTable[slot]) == 0)
287 break;
288 if (op == slotOp)
289 break;
290 if (++slot == fSize)
291 slot = 0;
292 }
293 return slot;
294}
295
296////////////////////////////////////////////////////////////////////////////////
297/// Rehash the object table in case an object has been removed.
298
300{
301 Int_t oldIndex, nextIndex;
302 TObject *nextObject;
303
304 for (oldIndex = index+1; ;oldIndex++) {
305 if (oldIndex >= fSize)
306 oldIndex = 0;
307 nextObject = fTable[oldIndex];
308 if (nextObject == 0)
309 break;
310 nextIndex = FindElement(nextObject);
311 if (nextIndex != oldIndex) {
312 fTable[nextIndex] = nextObject;
313 fTable[oldIndex] = 0;
314 }
315 }
316}
317
318////////////////////////////////////////////////////////////////////////////////
319/// Expand the object table.
320
322{
323 TObject **oldTable = fTable, *op;
324 int oldsize = fSize;
325 newSize = (Int_t)TMath::NextPrime(newSize);
326 fTable = new TObject* [newSize];
327 memset(fTable, 0, newSize*sizeof(TObject*));
328 fSize = newSize;
329 fTally = 0;
330 for (int i = 0; i < oldsize; i++)
331 if ((op = oldTable[i]))
332 Add(op);
333 delete [] oldTable;
334}
335
336////////////////////////////////////////////////////////////////////////////////
337/// Print the object table.
338
340{
341 int n, h, s, ncum = 0, hcum = 0, scum = 0, tcum = 0, thcum = 0;
342
343 if (fTally == 0 || !fTable)
344 return;
345
347
348 Printf("\nObject statistics");
349 Printf("class cnt on heap size total size heap size");
350 Printf("================================================================================");
351 TIter next(gROOT->GetListOfClasses());
352 TClass *cl;
353 while ((cl = (TClass*) next())) {
354 n = cl->GetInstanceCount();
355 h = cl->GetHeapInstanceCount();
356 s = cl->Size();
357 if (n > 0) {
358 Printf("%-24s %8d%11d%9d%14d%13d", cl->GetName(), n, h, s, n*s, h*s);
359 ncum += n;
360 hcum += h;
361 scum += s;
362 tcum += n*s;
363 thcum += h*s;
364 }
365 }
366 Printf("--------------------------------------------------------------------------------");
367 Printf("Total: %8d%11d%9d%14d%13d", ncum, hcum, scum, tcum, thcum);
368 Printf("================================================================================\n");
369}
370
371////////////////////////////////////////////////////////////////////////////////
372/// Histogram all objects according to their classes.
373
375{
376 TObject *op;
377
378 if (!fTable || !TROOT::Initialized())
379 return;
380
381 gROOT->GetListOfClasses()->R__FOR_EACH(TClass,ResetInstanceCount)();
382
383 for (int i = 0; i < fSize; i++)
384 if ((op = fTable[i])) { // attention: no ==
386 op->IsA()->AddInstance(op->IsOnHeap());
387 else
388 Error("UpdateInstCount", "oops 0x%zx\n", (size_t)op);
389 }
390}
391
392////////////////////////////////////////////////////////////////////////////////
393/// Issue a warning in case an object still appears in the table
394/// while it should not.
395
396void *TObjectTable::CheckPtrAndWarn(const char *msg, void *vp)
397{
398 if (fTable && vp && fTable[FindElement((TObject*)vp)]) {
399 Remove((TObject*)vp);
400 Warning("CheckPtrAndWarn", "%s (0x%zx)\n", msg, (size_t)vp);
401 }
402 return vp;
403}
#define h(i)
Definition: RSha256.hxx:106
int Int_t
Definition: RtypesCore.h:45
const Bool_t kFALSE
Definition: RtypesCore.h:101
const Bool_t kTRUE
Definition: RtypesCore.h:100
const char Option_t
Definition: RtypesCore.h:66
#define ClassImp(name)
Definition: Rtypes.h:375
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
#define gROOT
Definition: TROOT.h:404
void Printf(const char *fmt,...)
Formats a string in a circular formatting buffer and prints the string.
Definition: TString.cxx:2461
TClass instances represent classes, structs and namespaces in the ROOT type system.
Definition: TClass.h:80
void AddInstance(Bool_t heap=kFALSE)
Definition: TClass.h:378
UInt_t GetInstanceCount() const
Definition: TClass.h:463
Int_t Size() const
Return size of object of this class.
Definition: TClass.cxx:5692
UInt_t GetHeapInstanceCount() const
Definition: TClass.h:464
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.
Definition: TObjectTable.h:35
void InstanceStatistics() const
Print the object table.
TObject ** fTable
Definition: TObjectTable.h:38
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()
Definition: TObjectTable.h:75
Mother of all ROOT objects.
Definition: TObject.h:37
R__ALWAYS_INLINE Bool_t TestBit(UInt_t f) const
Definition: TObject.h:187
@ kNotDeleted
object has not been deleted
Definition: TObject.h:78
R__ALWAYS_INLINE Bool_t IsOnHeap() const
Definition: TObject.h:148
virtual const char * ClassName() const
Returns name of class to which the object belongs.
Definition: TObject.cxx:130
virtual void Warning(const char *method, const char *msgfmt,...) const
Issue warning message.
Definition: TObject.cxx:879
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition: TObject.cxx:893
virtual TClass * IsA() const
Definition: TObject.h:230
static Bool_t Initialized()
Return kTRUE if the TROOT object has been initialized.
Definition: TROOT.cxx:2822
Basic string class.
Definition: TString.h:136
void ToLower()
Change string to lower-case.
Definition: TString.cxx:1150
UInt_t Hash(ECaseCompare cmp=kExact) const
Return hash value.
Definition: TString.cxx:662
Bool_t Contains(const char *pat, ECaseCompare cmp=kExact) const
Definition: TString.h:624
Long_t NextPrime(Long_t x)
TMath Base functions.
Definition: TMathBase.cxx:30
const Int_t n
Definition: legend1.C:16
static constexpr double s