ROOT  6.06/09
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 This class registers all instances of TObject and its derived
14 classes in a hash table. The Add() and Remove() members are called
15 from the TObject ctor and dtor, respectively. Using the Print()
16 member one can see all currently active objects in the system.
17 Using the resource (in .rootrc): Root.ObjectStat one can toggle this
18 feature on or off.
19 
20 Using the compile option R__NOSTATS one can de-active this feature
21 for the entire system (for maximum performance in highly time
22 critical applications).
23 
24 The following output has been produced in a ROOT interactive session
25 via the command gObjectTable->Print()
26 ~~~ {.cpp}
27  class cnt on heap size total size heap size
28  ============================================================================
29  TKey 4 4 72 288 288
30  TClass 84 84 80 6720 6720
31  TDataMember 276 276 24 6624 6624
32  TObject 11 11 12 132 132
33  TMethod 1974 1974 64 126336 126336
34  TDataType 34 34 56 1904 1904
35  TList 2328 2328 36 83808 83808
36  TH1F 1 1 448 448 448
37  TText 2688 2688 56 150528 150528
38  TGaxis 1 0 120 120 0
39  TAxis 6 3 88 528 264
40  TBox 57 57 52 2964 2964
41  TLine 118 118 40 4720 4720
42  TWbox 1 1 56 56 56
43  TArrow 1 1 64 64 64
44  TPaveText 59 59 124 7316 7316
45  TPave 1 1 92 92 92
46  TFile 1 1 136 136 136
47  TCanvas 3 3 444 1332 1332
48  TPad 1 1 312 312 312
49  TContextMenu 3 3 48 144 144
50  TMethodArg 2166 2166 44 95304 95304
51  TPaveLabel 1 1 120 120 120
52  THtml 1 1 32 32 32
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 "TROOT.h"
85 #include "TClass.h"
86 #include "TError.h"
87 
88 
90 
91 
93 
94 ////////////////////////////////////////////////////////////////////////////////
95 /// Create an object table.
96 
98 {
99  fSize = (Int_t)TMath::NextPrime(tableSize);
100  fTable = new TObject* [fSize];
101  memset(fTable, 0, fSize*sizeof(TObject*));
102  fTally = 0;
103 }
104 
105 ////////////////////////////////////////////////////////////////////////////////
106 /// Delete TObjectTable.
107 
109 {
110  delete [] fTable; fTable = 0;
111 }
112 
113 ////////////////////////////////////////////////////////////////////////////////
114 /// Print the object table.
115 /// If option ="all" prints the list of all objects with the format
116 /// object number, pointer, class name, object name
117 
118 void TObjectTable::Print(Option_t *option) const
119 {
120  TString opt = option;
121  opt.ToLower();
122  if (opt.Contains("all")) {
123  TObject *obj;
124  int i, num = 0;
125  Printf("\nList of all objects");
126  Printf("object address class name");
127  Printf("================================================================================");
128  for (i = 0; i < fSize; i++) {
129  if (!fTable[i]) continue;
130  num++;
131  obj = fTable[i];
132  printf("%-8d 0x%-16lx %-24s %s\n", num, (Long_t)obj, obj->ClassName(),
133  obj->GetName());
134  }
135  Printf("================================================================================\n");
136  }
137 
138  //print the number of instances per class
140 }
141 
142 ////////////////////////////////////////////////////////////////////////////////
143 /// Add an object to the object table.
144 
146 {
147  if (!op) {
148  Error("Add", "op is 0");
149  return;
150  }
151  if (!fTable)
152  return;
153 
154  Int_t slot = FindElement(op);
155  if (fTable[slot] == 0) {
156  fTable[slot] = op;
157  fTally++;
158  if (HighWaterMark())
159  Expand(2 * fSize);
160  }
161 }
162 
163 ////////////////////////////////////////////////////////////////////////////////
164 /// Add an object to the global object table gObjectTable. If the global
165 /// table does not exist create it first. This member function may only
166 /// be used by TObject::TObject. Use Add() to add objects to any other
167 /// TObjectTable object. This is a static function.
168 
170 {
171  static Bool_t olock = kFALSE;
172 
173  if (!op) {
174  ::Error("TObjectTable::AddObj", "op is 0");
175  return;
176  }
177  if (olock)
178  return;
179 
180  if (!gObjectTable) {
181  olock = kTRUE;
182  gObjectTable = new TObjectTable(10000);
183  olock = kFALSE;
184  gObjectTable->Add(gObjectTable);
185  }
186 
187  gObjectTable->Add(op);
188 }
189 
190 ////////////////////////////////////////////////////////////////////////////////
191 /// Delete all objects stored in the TObjectTable.
192 
194 {
195  for (int i = 0; i < fSize; i++) {
196  if (fTable[i]) {
197  delete fTable[i];
198  fTable[i] = 0;
199  }
200  }
201  fTally = 0;
202 }
203 
204 ////////////////////////////////////////////////////////////////////////////////
205 /// Remove an object from the object table.
206 
208 {
209  if (op == 0) {
210  Error("Remove", "remove 0 from TObjectTable");
211  return;
212  }
213 
214  if (!fTable)
215  return;
216 
217  Int_t i = FindElement(op);
218  if (fTable[i] == 0) {
219  Warning("Remove", "0x%lx not found at %d", (Long_t)op, i);
220  for (int j = 0; j < fSize; j++) {
221  if (fTable[j] == op) {
222  Error("Remove", "0x%lx found at %d !!!", (Long_t)op, j);
223  i = j;
224  }
225  }
226  }
227 
228  if (fTable[i]) {
229  fTable[i] = 0;
230  FixCollisions(i);
231  fTally--;
232  }
233 }
234 
235 ////////////////////////////////////////////////////////////////////////////////
236 /// Remove an object from the object table. If op is 0 or not in the table
237 /// don't complain. Currently only used by the TClonesArray dtor. Should not
238 /// be used anywhere else, except in places where "special" allocation and
239 /// de-allocation tricks are performed.
240 
242 {
243  if (op == 0) return;
244 
245  if (!fTable)
246  return;
247 
248  Int_t i = FindElement(op);
249  if (fTable[i] == 0)
250  for (int j = 0; j < fSize; j++)
251  if (fTable[j] == op)
252  i = j;
253 
254  fTable[i] = 0;
255  FixCollisions(i);
256  fTally--;
257 }
258 
259 ////////////////////////////////////////////////////////////////////////////////
260 /// Deletes the object table (this static class function calls the dtor).
261 
263 {
265  delete [] fTable; fTable = 0;
266 }
267 
268 ////////////////////////////////////////////////////////////////////////////////
269 /// Find an object in the object table. Returns the slot where to put
270 /// the object. To test if the object is actually already in the table
271 /// use PtrIsValid().
272 
274 {
275  Int_t slot, n;
276  TObject *slotOp;
277 
278  if (!fTable)
279  return 0;
280 
281  //slot = Int_t(((ULong_t) op >> 2) % fSize);
282  slot = Int_t(TString::Hash(&op, sizeof(TObject*)) % fSize);
283  for (n = 0; n < fSize; n++) {
284  if ((slotOp = fTable[slot]) == 0)
285  break;
286  if (op == slotOp)
287  break;
288  if (++slot == fSize)
289  slot = 0;
290  }
291  return slot;
292 }
293 
294 ////////////////////////////////////////////////////////////////////////////////
295 /// Rehash the object table in case an object has been removed.
296 
298 {
299  Int_t oldIndex, nextIndex;
300  TObject *nextObject;
301 
302  for (oldIndex = index+1; ;oldIndex++) {
303  if (oldIndex >= fSize)
304  oldIndex = 0;
305  nextObject = fTable[oldIndex];
306  if (nextObject == 0)
307  break;
308  nextIndex = FindElement(nextObject);
309  if (nextIndex != oldIndex) {
310  fTable[nextIndex] = nextObject;
311  fTable[oldIndex] = 0;
312  }
313  }
314 }
315 
316 ////////////////////////////////////////////////////////////////////////////////
317 /// Expand the object table.
318 
320 {
321  TObject **oldTable = fTable, *op;
322  int oldsize = fSize;
323  newSize = (Int_t)TMath::NextPrime(newSize);
324  fTable = new TObject* [newSize];
325  memset(fTable, 0, newSize*sizeof(TObject*));
326  fSize = newSize;
327  fTally = 0;
328  for (int i = 0; i < oldsize; i++)
329  if ((op = oldTable[i]))
330  Add(op);
331  delete [] oldTable;
332 }
333 
334 ////////////////////////////////////////////////////////////////////////////////
335 /// Print the object table.
336 
338 {
339  int n, h, s, ncum = 0, hcum = 0, scum = 0, tcum = 0, thcum = 0;
340 
341  if (fTally == 0 || !fTable)
342  return;
343 
344  UpdateInstCount();
345 
346  Printf("\nObject statistics");
347  Printf("class cnt on heap size total size heap size");
348  Printf("================================================================================");
349  TIter next(gROOT->GetListOfClasses());
350  TClass *cl;
351  while ((cl = (TClass*) next())) {
352  n = cl->GetInstanceCount();
353  h = cl->GetHeapInstanceCount();
354  s = cl->Size();
355  if (n > 0) {
356  Printf("%-24s %8d%11d%9d%14d%13d", cl->GetName(), n, h, s, n*s, h*s);
357  ncum += n;
358  hcum += h;
359  scum += s;
360  tcum += n*s;
361  thcum += h*s;
362  }
363  }
364  Printf("--------------------------------------------------------------------------------");
365  Printf("Total: %8d%11d%9d%14d%13d", ncum, hcum, scum, tcum, thcum);
366  Printf("================================================================================\n");
367 }
368 
369 ////////////////////////////////////////////////////////////////////////////////
370 /// Histogram all objects according to their classes.
371 
373 {
374  TObject *op;
375 
376  if (!fTable || !TROOT::Initialized())
377  return;
378 
379  gROOT->GetListOfClasses()->R__FOR_EACH(TClass,ResetInstanceCount)();
380 
381  for (int i = 0; i < fSize; i++)
382  if ((op = fTable[i])) { // attention: no ==
383  if (op->TestBit(TObject::kNotDeleted))
384  op->IsA()->AddInstance(op->IsOnHeap());
385  else
386  Error("UpdateInstCount", "oops 0x%lx\n", (Long_t)op);
387  }
388 }
389 
390 ////////////////////////////////////////////////////////////////////////////////
391 /// Issue a warning in case an object still appears in the table
392 /// while it should not.
393 
394 void *TObjectTable::CheckPtrAndWarn(const char *msg, void *vp)
395 {
396  if (fTable && vp && fTable[FindElement((TObject*)vp)]) {
397  Remove((TObject*)vp);
398  Warning("CheckPtrAndWarn", "%s (0x%lx)\n", msg, (Long_t)vp);
399  }
400  return vp;
401 }
Int_t FindElement(TObject *obj)
Find an object in the object table.
Bool_t IsOnHeap() const
Definition: TObject.h:140
TObjectTable * gObjectTable
ClassImp(TSeqCollection) Int_t TSeqCollection TIter next(this)
Return index of object in collection.
const char Option_t
Definition: RtypesCore.h:62
ClassImp(TObjectTable) TObjectTable
Create an object table.
TH1 * h
Definition: legend2.C:5
void Expand(Int_t newsize)
Expand the object table.
#define gROOT
Definition: TROOT.h:340
void RemoveQuietly(TObject *obj)
Remove an object from the object table.
Basic string class.
Definition: TString.h:137
~TObjectTable()
Delete TObjectTable.
void ToLower()
Change string to lower-case.
Definition: TString.cxx:1088
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
const Bool_t kFALSE
Definition: Rtypes.h:92
void Print(Option_t *option="") const
Print the object table.
static Bool_t Initialized()
Return kTRUE if the TROOT object has been initialized.
Definition: TROOT.cxx:2469
void Terminate()
Deletes the object table (this static class function calls the dtor).
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition: TObject.cxx:918
void * CheckPtrAndWarn(const char *msg, void *vp)
Issue a warning in case an object still appears in the table while it should not. ...
TObjectTable(const TObjectTable &)
virtual const char * ClassName() const
Returns name of class to which the object belongs.
Definition: TObject.cxx:187
UInt_t GetInstanceCount() const
Definition: TClass.h:418
Bool_t TestBit(UInt_t f) const
Definition: TObject.h:173
The ROOT global object gROOT contains a list of all defined classes.
Definition: TClass.h:81
This class registers all instances of TObject and its derived classes in a hash table.
Definition: TObjectTable.h:37
static void AddObj(TObject *obj)
Add an object to the global object table gObjectTable.
#define Printf
Definition: TGeoToOCC.h:18
Bool_t HighWaterMark()
Definition: TObjectTable.h:77
long Long_t
Definition: RtypesCore.h:50
Long_t NextPrime(Long_t x)
TMath Base functions.
Definition: TMathBase.cxx:29
TObject ** fTable
Definition: TObjectTable.h:40
void Add(TObject *obj)
Add an object to the object table.
ClassImp(TMCParticle) void TMCParticle printf(": p=(%7.3f,%7.3f,%9.3f) ;", fPx, fPy, fPz)
void FixCollisions(Int_t index)
Rehash the object table in case an object has been removed.
Mother of all ROOT objects.
Definition: TObject.h:58
Bool_t Contains(const char *pat, ECaseCompare cmp=kExact) const
Definition: TString.h:567
void InstanceStatistics() const
Print the object table.
void Delete(Option_t *opt="")
Delete all objects stored in the TObjectTable.
const Bool_t kTRUE
Definition: Rtypes.h:91
TObject * obj
const Int_t n
Definition: legend1.C:16
void Remove(TObject *obj)
Remove an object from the object table.
void UpdateInstCount() const
Histogram all objects according to their classes.
UInt_t Hash(ECaseCompare cmp=kExact) const
Return hash value.
Definition: TString.cxx:605
virtual void Warning(const char *method, const char *msgfmt,...) const
Issue warning message.
Definition: TObject.cxx:904