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 TROOT 1 0 208 208 0
54 TApplication 1 1 28 28 28
55 TFileHandler 1 1 20 20 20
56 TColor 163 163 40 6520 6520
57 TStyle 1 1 364 364 364
58 TRealData 117 117 28 3276 3276
59 TBaseClass 88 88 36 3168 3168
60 THashList 5 5 40 200 200
61 THashTable 5 5 36 180 180
62 TGeometry 1 1 64 64 64
63 TLink 7 7 60 420 420
64 TPostScript 1 1 764 764 764
65 TMinuit 1 1 792 792 792
66 TStopwatch 1 0 56 56 0
67 TRootGuiFactory 1 1 28 28 28
68 TGX11 1 1 172 172 172
69 TUnixSystem 1 1 252 252 252
70 TSignalHandler 1 1 20 20 20
71 TOrdCollection 3 3 40 120 120
72 TEnv 1 1 24 24 24
73 TCling 1 1 208 208 208
74 TBenchmark 1 1 52 52 52
75 TClassTable 1 1 12 12 12
76 TObjectTable 1 1 12 12 12
77 ----------------------------------------------------------------------------
78 Total: 10225 10219 5976 506988 506340
79 ============================================================================
80~~~
81*/
82
83#include "TObjectTable.h"
84#include "TList.h"
85#include "TROOT.h"
86#include "TClass.h"
87#include "TError.h"
88#include "TMathBase.h"
89
90
92
93
94
95////////////////////////////////////////////////////////////////////////////////
96/// Create an object table.
97
105
106////////////////////////////////////////////////////////////////////////////////
107/// Delete TObjectTable.
108
110{
111 delete [] fTable; fTable = nullptr;
112}
113
114////////////////////////////////////////////////////////////////////////////////
115/// Print the object table.
116/// If option ="all" prints the list of all objects with the format
117/// object number, pointer, class name, object name
118
120{
121 TString opt = option;
122 opt.ToLower();
123 if (opt.Contains("all")) {
124 TObject *obj;
125 int i, num = 0;
126 Printf("\nList of all objects");
127 Printf("object address class name");
128 Printf("================================================================================");
129 for (i = 0; i < fSize; i++) {
130 if (!fTable[i]) continue;
131 num++;
132 obj = fTable[i];
133 printf("%-8d 0x%-16zx %-24s %s\n", num, (size_t)obj, obj->ClassName(),
134 obj->GetName());
135 }
136 Printf("================================================================================\n");
137 }
138
139 //print the number of instances per class
141}
142
143////////////////////////////////////////////////////////////////////////////////
144/// Add an object to the object table.
145
147{
148 if (!op) {
149 Error("Add", "op is 0");
150 return;
151 }
152 if (!fTable)
153 return;
154
156 if (!fTable[slot]) {
157 fTable[slot] = op;
158 fTally++;
159 if (HighWaterMark())
160 Expand(2 * fSize);
161 }
162}
163
164////////////////////////////////////////////////////////////////////////////////
165/// Add an object to the global object table gObjectTable. If the global
166/// table does not exist create it first. This member function may only
167/// be used by TObject::TObject. Use Add() to add objects to any other
168/// TObjectTable object. This is a static function.
169
171{
172 static Bool_t olock = kFALSE;
173
174 if (!op) {
175 ::Error("TObjectTable::AddObj", "op is 0");
176 return;
177 }
178 if (olock)
179 return;
180
181 if (!gObjectTable) {
182 olock = kTRUE;
183 gObjectTable = new TObjectTable(10000);
184 olock = kFALSE;
186 }
187
189}
190
191////////////////////////////////////////////////////////////////////////////////
192/// Delete all objects stored in the TObjectTable.
193
195{
196 for (int i = 0; i < fSize; i++) {
197 if (fTable[i]) {
198 delete fTable[i];
199 fTable[i] = nullptr;
200 }
201 }
202 fTally = 0;
203}
204
205////////////////////////////////////////////////////////////////////////////////
206/// Remove an object from the object table.
207
209{
210 if (!op) {
211 Error("Remove", "remove 0 from TObjectTable");
212 return;
213 }
214
215 if (!fTable)
216 return;
217
218 Int_t i = FindElement(op);
219 if (!fTable[i]) {
220 Warning("Remove", "0x%zx not found at %d", (size_t)op, i);
221 for (int j = 0; j < fSize; j++) {
222 if (fTable[j] == op) {
223 Error("Remove", "0x%zx found at %d !!!", (size_t)op, j);
224 i = j;
225 }
226 }
227 }
228
229 if (fTable[i]) {
230 fTable[i] = nullptr;
231 FixCollisions(i);
232 fTally--;
233 }
234}
235
236////////////////////////////////////////////////////////////////////////////////
237/// Remove an object from the object table. If op is 0 or not in the table
238/// don't complain. Currently only used by the TClonesArray dtor. Should not
239/// be used anywhere else, except in places where "special" allocation and
240/// de-allocation tricks are performed.
241
243{
244 if (!op) return;
245
246 if (!fTable)
247 return;
248
249 Int_t i = FindElement(op);
250 if (!fTable[i])
251 for (int j = 0; j < fSize; j++)
252 if (fTable[j] == op)
253 i = j;
254
255 fTable[i] = nullptr;
256 FixCollisions(i);
257 fTally--;
258}
259
260////////////////////////////////////////////////////////////////////////////////
261/// Deletes the object table (this static class function calls the dtor).
262
264{
266 delete [] fTable;
267 fTable = nullptr;
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;
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]) == nullptr)
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{
303
304 for (oldIndex = index+1; ;oldIndex++) {
305 if (oldIndex >= fSize)
306 oldIndex = 0;
308 if (!nextObject)
309 break;
311 if (nextIndex != oldIndex) {
313 fTable[oldIndex] = nullptr;
314 }
315 }
316}
317
318////////////////////////////////////////////////////////////////////////////////
319/// Expand the object table.
320
322{
324 int oldsize = fSize;
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
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
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:421
void Printf(const char *fmt,...)
Formats a string in a circular formatting buffer and prints the string.
Definition TString.cxx:2509
TClass instances represent classes, structs and namespaces in the ROOT type system.
Definition TClass.h:84
UInt_t GetInstanceCount() const
Definition TClass.h:481
Int_t Size() const
Return size of object of this class.
Definition TClass.cxx:5806
UInt_t GetHeapInstanceCount() const
Definition TClass.h:482
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:42
virtual void Warning(const char *method, const char *msgfmt,...) const
Issue warning message.
Definition TObject.cxx:1081
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition TObject.cxx:1095
static Bool_t Initialized()
Return kTRUE if the TROOT object has been initialized.
Definition TROOT.cxx:2925
Basic string class.
Definition TString.h:138
void ToLower()
Change string to lower-case.
Definition TString.cxx:1189
UInt_t Hash(ECaseCompare cmp=kExact) const
Return hash value.
Definition TString.cxx:684
Bool_t Contains(const char *pat, ECaseCompare cmp=kExact) const
Definition TString.h:641
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:409
Long_t NextPrime(Long_t x)