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#include "TMathBase.h"
90
91
93
94
96
97////////////////////////////////////////////////////////////////////////////////
98/// Create an object table.
99
107
108////////////////////////////////////////////////////////////////////////////////
109/// Delete TObjectTable.
110
112{
113 delete [] fTable; fTable = nullptr;
114}
115
116////////////////////////////////////////////////////////////////////////////////
117/// Print the object table.
118/// If option ="all" prints the list of all objects with the format
119/// object number, pointer, class name, object name
120
122{
123 TString opt = option;
124 opt.ToLower();
125 if (opt.Contains("all")) {
126 TObject *obj;
127 int i, num = 0;
128 Printf("\nList of all objects");
129 Printf("object address class name");
130 Printf("================================================================================");
131 for (i = 0; i < fSize; i++) {
132 if (!fTable[i]) continue;
133 num++;
134 obj = fTable[i];
135 printf("%-8d 0x%-16zx %-24s %s\n", num, (size_t)obj, obj->ClassName(),
136 obj->GetName());
137 }
138 Printf("================================================================================\n");
139 }
140
141 //print the number of instances per class
143}
144
145////////////////////////////////////////////////////////////////////////////////
146/// Add an object to the object table.
147
149{
150 if (!op) {
151 Error("Add", "op is 0");
152 return;
153 }
154 if (!fTable)
155 return;
156
158 if (!fTable[slot]) {
159 fTable[slot] = op;
160 fTally++;
161 if (HighWaterMark())
162 Expand(2 * fSize);
163 }
164}
165
166////////////////////////////////////////////////////////////////////////////////
167/// Add an object to the global object table gObjectTable. If the global
168/// table does not exist create it first. This member function may only
169/// be used by TObject::TObject. Use Add() to add objects to any other
170/// TObjectTable object. This is a static function.
171
173{
174 static Bool_t olock = kFALSE;
175
176 if (!op) {
177 ::Error("TObjectTable::AddObj", "op is 0");
178 return;
179 }
180 if (olock)
181 return;
182
183 if (!gObjectTable) {
184 olock = kTRUE;
185 gObjectTable = new TObjectTable(10000);
186 olock = kFALSE;
188 }
189
191}
192
193////////////////////////////////////////////////////////////////////////////////
194/// Delete all objects stored in the TObjectTable.
195
197{
198 for (int i = 0; i < fSize; i++) {
199 if (fTable[i]) {
200 delete fTable[i];
201 fTable[i] = nullptr;
202 }
203 }
204 fTally = 0;
205}
206
207////////////////////////////////////////////////////////////////////////////////
208/// Remove an object from the object table.
209
211{
212 if (!op) {
213 Error("Remove", "remove 0 from TObjectTable");
214 return;
215 }
216
217 if (!fTable)
218 return;
219
220 Int_t i = FindElement(op);
221 if (!fTable[i]) {
222 Warning("Remove", "0x%zx not found at %d", (size_t)op, i);
223 for (int j = 0; j < fSize; j++) {
224 if (fTable[j] == op) {
225 Error("Remove", "0x%zx found at %d !!!", (size_t)op, j);
226 i = j;
227 }
228 }
229 }
230
231 if (fTable[i]) {
232 fTable[i] = nullptr;
233 FixCollisions(i);
234 fTally--;
235 }
236}
237
238////////////////////////////////////////////////////////////////////////////////
239/// Remove an object from the object table. If op is 0 or not in the table
240/// don't complain. Currently only used by the TClonesArray dtor. Should not
241/// be used anywhere else, except in places where "special" allocation and
242/// de-allocation tricks are performed.
243
245{
246 if (!op) return;
247
248 if (!fTable)
249 return;
250
251 Int_t i = FindElement(op);
252 if (!fTable[i])
253 for (int j = 0; j < fSize; j++)
254 if (fTable[j] == op)
255 i = j;
256
257 fTable[i] = nullptr;
258 FixCollisions(i);
259 fTally--;
260}
261
262////////////////////////////////////////////////////////////////////////////////
263/// Deletes the object table (this static class function calls the dtor).
264
266{
268 delete [] fTable;
269 fTable = nullptr;
270}
271
272////////////////////////////////////////////////////////////////////////////////
273/// Find an object in the object table. Returns the slot where to put
274/// the object. To test if the object is actually already in the table
275/// use PtrIsValid().
276
278{
279 Int_t slot, n;
281
282 if (!fTable)
283 return 0;
284
285 //slot = Int_t(((ULong_t) op >> 2) % fSize);
286 slot = Int_t(TString::Hash(&op, sizeof(TObject*)) % fSize);
287 for (n = 0; n < fSize; n++) {
288 if ((slotOp = fTable[slot]) == nullptr)
289 break;
290 if (op == slotOp)
291 break;
292 if (++slot == fSize)
293 slot = 0;
294 }
295 return slot;
296}
297
298////////////////////////////////////////////////////////////////////////////////
299/// Rehash the object table in case an object has been removed.
300
302{
305
306 for (oldIndex = index+1; ;oldIndex++) {
307 if (oldIndex >= fSize)
308 oldIndex = 0;
310 if (!nextObject)
311 break;
313 if (nextIndex != oldIndex) {
315 fTable[oldIndex] = nullptr;
316 }
317 }
318}
319
320////////////////////////////////////////////////////////////////////////////////
321/// Expand the object table.
322
324{
326 int oldsize = fSize;
328 fTable = new TObject* [newSize];
329 memset(fTable, 0, newSize*sizeof(TObject*));
330 fSize = newSize;
331 fTally = 0;
332 for (int i = 0; i < oldsize; i++)
333 if ((op = oldTable[i]))
334 Add(op);
335 delete [] oldTable;
336}
337
338////////////////////////////////////////////////////////////////////////////////
339/// Print the object table.
340
342{
343 int n, h, s, ncum = 0, hcum = 0, scum = 0, tcum = 0, thcum = 0;
344
345 if (fTally == 0 || !fTable)
346 return;
347
349
350 Printf("\nObject statistics");
351 Printf("class cnt on heap size total size heap size");
352 Printf("================================================================================");
353 TIter next(gROOT->GetListOfClasses());
354 TClass *cl;
355 while ((cl = (TClass*) next())) {
356 n = cl->GetInstanceCount();
357 h = cl->GetHeapInstanceCount();
358 s = cl->Size();
359 if (n > 0) {
360 Printf("%-24s %8d%11d%9d%14d%13d", cl->GetName(), n, h, s, n*s, h*s);
361 ncum += n;
362 hcum += h;
363 scum += s;
364 tcum += n*s;
365 thcum += h*s;
366 }
367 }
368 Printf("--------------------------------------------------------------------------------");
369 Printf("Total: %8d%11d%9d%14d%13d", ncum, hcum, scum, tcum, thcum);
370 Printf("================================================================================\n");
371}
372
373////////////////////////////////////////////////////////////////////////////////
374/// Histogram all objects according to their classes.
375
377{
378 TObject *op;
379
380 if (!fTable || !TROOT::Initialized())
381 return;
382
383 gROOT->GetListOfClasses()->R__FOR_EACH(TClass,ResetInstanceCount)();
384
385 for (int i = 0; i < fSize; i++)
386 if ((op = fTable[i])) { // attention: no ==
388 op->IsA()->AddInstance(op->IsOnHeap());
389 else
390 Error("UpdateInstCount", "oops 0x%zx\n", (size_t)op);
391 }
392}
393
394////////////////////////////////////////////////////////////////////////////////
395/// Issue a warning in case an object still appears in the table
396/// while it should not.
397
398void *TObjectTable::CheckPtrAndWarn(const char *msg, void *vp)
399{
400 if (fTable && vp && fTable[FindElement((TObject*)vp)]) {
401 Remove((TObject*)vp);
402 Warning("CheckPtrAndWarn", "%s (0x%zx)\n", msg, (size_t)vp);
403 }
404 return vp;
405}
#define h(i)
Definition RSha256.hxx:106
int Int_t
Signed integer 4 bytes (int)
Definition RtypesCore.h:59
constexpr Bool_t kFALSE
Definition RtypesCore.h:108
constexpr Bool_t kTRUE
Definition RtypesCore.h:107
const char Option_t
Option string (const char)
Definition RtypesCore.h:80
#define ClassImp(name)
Definition Rtypes.h:376
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
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:411
void Printf(const char *fmt,...)
Formats a string in a circular formatting buffer and prints the string.
Definition TString.cxx:2510
TClass instances represent classes, structs and namespaces in the ROOT type system.
Definition TClass.h:84
UInt_t GetInstanceCount() const
Definition TClass.h:478
Int_t Size() const
Return size of object of this class.
Definition TClass.cxx:5744
UInt_t GetHeapInstanceCount() const
Definition TClass.h:479
const char * GetName() const override
Returns name of object.
Definition TNamed.h:49
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
virtual void Warning(const char *method, const char *msgfmt,...) const
Issue warning message.
Definition TObject.cxx:1058
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition TObject.cxx:1072
static Bool_t Initialized()
Return kTRUE if the TROOT object has been initialized.
Definition TROOT.cxx:2914
Basic string class.
Definition TString.h:138
void ToLower()
Change string to lower-case.
Definition TString.cxx:1190
UInt_t Hash(ECaseCompare cmp=kExact) const
Return hash value.
Definition TString.cxx:685
Bool_t Contains(const char *pat, ECaseCompare cmp=kExact) const
Definition TString.h:640
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:405
Long_t NextPrime(Long_t x)