ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
TProcessID.cxx
Go to the documentation of this file.
1 // @(#)root/cont:$Id$
2 // Author: Rene Brun 28/09/2001
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 TProcessID
13 A TProcessID identifies a ROOT job in a unique way in time and space.
14 The TProcessID title consists of a TUUID object which provides a globally
15 unique identifier (for more see TUUID.h).
16 
17 A TProcessID is automatically created by the TROOT constructor.
18 When a TFile contains referenced objects (see TRef), the TProcessID
19 object is written to the file.
20 If a file has been written in multiple sessions (same machine or not),
21 a TProcessID is written for each session.
22 These objects are used by the class TRef to uniquely identified
23 any TObject pointed by a TRef.
24 
25 When a referenced object is read from a file (its bit kIsReferenced is set),
26 this object is entered into the objects table of the corresponding TProcessID.
27 Each TFile has a list of TProcessIDs (see TFile::fProcessIDs) also
28 accessible via TProcessID::fgPIDs (for all files).
29 When this object is deleted, it is removed from the table via the cleanup
30 mechanism invoked by the TObject destructor.
31 
32 Each TProcessID has a table (TObjArray *fObjects) that keeps track
33 of all referenced objects. If a referenced object has a fUniqueID set,
34 a pointer to this unique object may be found via fObjects->At(fUniqueID).
35 In the same way, when a TRef::GetObject is called, GetObject uses
36 its own fUniqueID to find the pointer to the referenced object.
37 See TProcessID::GetObjectWithID and PutObjectWithID.
38 
39 When a referenced object is deleted, its slot in fObjects is set to null.
40 //
41 See also TProcessUUID: a specialized TProcessID to manage the single list
42 of TUUIDs.
43 */
44 
45 #include "TProcessID.h"
46 #include "TROOT.h"
47 #include "TObjArray.h"
48 #include "TExMap.h"
49 #include "TVirtualMutex.h"
50 #include "TError.h"
51 
52 TObjArray *TProcessID::fgPIDs = 0; //pointer to the list of TProcessID
53 TProcessID *TProcessID::fgPID = 0; //pointer to the TProcessID of the current session
54 UInt_t TProcessID::fgNumber = 0; //Current referenced object instance count
55 TExMap *TProcessID::fgObjPIDs= 0; //Table (pointer,pids)
57 
58 ////////////////////////////////////////////////////////////////////////////////
59 /// Return hash value for this object.
60 
61 static inline ULong_t Void_Hash(const void *ptr)
62 {
63  return TString::Hash(&ptr, sizeof(void*));
64 }
65 
66 ////////////////////////////////////////////////////////////////////////////////
67 /// Default constructor.
68 
70 {
71  fCount = 0;
72  fObjects = 0;
73 }
74 
75 ////////////////////////////////////////////////////////////////////////////////
76 /// Destructor.
77 
79 {
80  delete fObjects;
81  fObjects = 0;
83  fgPIDs->Remove(this);
84 }
85 
86 ////////////////////////////////////////////////////////////////////////////////
87 /// Static function to add a new TProcessID to the list of PIDs.
88 
90 {
92 
93  if (fgPIDs && fgPIDs->GetEntriesFast() >= 65534) {
94  if (fgPIDs->GetEntriesFast() == 65534) {
95  ::Warning("TProcessID::AddProcessID","Maximum number of TProcessID (65535) is almost reached (one left). TRef will stop being functional when the limit is reached.");
96  } else {
97  ::Fatal("TProcessID::AddProcessID","Maximum number of TProcessID (65535) has been reached. TRef are not longer functional.");
98  }
99  }
100 
101  TProcessID *pid = new TProcessID();
102 
103  if (!fgPIDs) {
104  fgPID = pid;
105  fgPIDs = new TObjArray(10);
106  gROOT->GetListOfCleanups()->Add(fgPIDs);
107  }
108  UShort_t apid = fgPIDs->GetEntriesFast();
109  pid->IncrementCount();
110 
111  fgPIDs->Add(pid);
112  // if (apid == 0) for(int incr=0; incr < 65533; ++incr) fgPIDs->Add(0); // NOTE: DEBUGGING ONLY MUST BE REMOVED!
113  char name[20];
114  snprintf(name,20,"ProcessID%d",apid);
115  pid->SetName(name);
116  pid->SetUniqueID((UInt_t)apid);
117  TUUID u;
118  //apid = fgPIDs->GetEntriesFast();
119  pid->SetTitle(u.AsString());
120  return pid;
121 }
122 
123 ////////////////////////////////////////////////////////////////////////////////
124 /// static function returning the ID assigned to obj
125 /// If the object is not yet referenced, its kIsReferenced bit is set
126 /// and its fUniqueID set to the current number of referenced objects so far.
127 
129 {
131 
132  UInt_t uid = obj->GetUniqueID() & 0xffffff;
133  if (obj == fgPID->GetObjectWithID(uid)) return uid;
134  if (obj->TestBit(kIsReferenced)) {
135  fgPID->PutObjectWithID(obj,uid);
136  return uid;
137  }
138  if (fgNumber >= 16777215) {
139  // This process id is 'full', we need to use a new one.
140  fgPID = AddProcessID();
141  fgNumber = 0;
142  for(Int_t i = 0; i < fgPIDs->GetLast()+1; ++i) {
143  TProcessID *pid = (TProcessID*)fgPIDs->At(i);
144  if (pid && pid->fObjects && pid->fObjects->GetEntries() == 0) {
145  pid->Clear();
146  }
147  }
148  }
149  fgNumber++;
150  obj->SetBit(kIsReferenced);
151  uid = fgNumber;
152  // if (fgNumber<10) fgNumber = 16777213; // NOTE: DEBUGGING ONLY MUST BE REMOVED!
153  if ( fgPID->GetUniqueID() < 255 ) {
154  obj->SetUniqueID( (uid & 0xffffff) + (fgPID->GetUniqueID()<<24) );
155  } else {
156  obj->SetUniqueID( (uid & 0xffffff) + 0xff000000 /* 255 << 24 */ );
157  }
158  fgPID->PutObjectWithID(obj,uid);
159  return uid;
160 }
161 
162 ////////////////////////////////////////////////////////////////////////////////
163 /// Initialize fObjects.
164 
166 {
167  if (!fObjects) fObjects = new TObjArray(100);
168 }
169 
170 ////////////////////////////////////////////////////////////////////////////////
171 /// static function (called by TROOT destructor) to delete all TProcessIDs
172 
174 {
176 
177  fgPIDs->Delete();
178  gROOT->GetListOfCleanups()->Remove(fgPIDs);
179  delete fgPIDs;
180  fgPIDs = 0;
181 }
182 
183 ////////////////////////////////////////////////////////////////////////////////
184 /// delete the TObjArray pointing to referenced objects
185 /// this function is called by TFile::Close("R")
186 
188 {
189  if (GetUniqueID()>254 && fObjects && fgObjPIDs) {
190  // We might have many references registered in the map
191  for(Int_t i = 0; i < fObjects->GetSize(); ++i) {
193  if (obj) {
194  ULong64_t hash = Void_Hash(obj);
195  fgObjPIDs->Remove(hash,(Long64_t)obj);
196  (*fObjects)[i] = 0;
197  }
198  }
199  }
200  delete fObjects; fObjects = 0;
201 }
202 
203 ////////////////////////////////////////////////////////////////////////////////
204 /// The reference fCount is used to delete the TProcessID
205 /// in the TFile destructor when fCount = 0
206 
208 {
209  fCount--;
210  if (fCount < 0) fCount = 0;
211  return fCount;
212 }
213 
214 ////////////////////////////////////////////////////////////////////////////////
215 /// static function returning a pointer to TProcessID number pid in fgPIDs
216 
218 {
219  return (TProcessID*)fgPIDs->At(pid);
220 }
221 
222 ////////////////////////////////////////////////////////////////////////////////
223 /// Return the (static) number of process IDs.
224 
226 {
227  return fgPIDs ? fgPIDs->GetLast()+1 : 0;
228 }
229 
230 ////////////////////////////////////////////////////////////////////////////////
231 /// static function returning a pointer to TProcessID with its pid
232 /// encoded in the highest byte of uid
233 
235 {
237 
238  Int_t pid = (uid>>24)&0xff;
239  if (pid==0xff) {
240  // Look up the pid in the table (pointer,pid)
241  if (fgObjPIDs==0) return 0;
242  ULong_t hash = Void_Hash(obj);
243  pid = fgObjPIDs->GetValue(hash,(Long_t)obj);
244  }
245  return (TProcessID*)fgPIDs->At(pid);
246 }
247 
248 ////////////////////////////////////////////////////////////////////////////////
249 /// static function returning a pointer to TProcessID with its pid
250 /// encoded in the highest byte of obj->GetUniqueID()
251 
253 {
254  return GetProcessWithUID(obj->GetUniqueID(),obj);
255 }
256 
257 ////////////////////////////////////////////////////////////////////////////////
258 /// static function returning the pointer to the session TProcessID
259 
261 {
262  return fgPID;
263 }
264 
265 ////////////////////////////////////////////////////////////////////////////////
266 /// Increase the reference count to this object.
267 
269 {
270  if (!fObjects) fObjects = new TObjArray(100);
271  fCount++;
272  return fCount;
273 }
274 
275 ////////////////////////////////////////////////////////////////////////////////
276 /// Return the current referenced object count
277 /// fgNumber is incremented every time a new object is referenced
278 
280 {
281  return fgNumber;
282 }
283 
284 ////////////////////////////////////////////////////////////////////////////////
285 /// returns the TObject with unique identifier uid in the table of objects
286 
288 {
289  Int_t uid = uidd & 0xffffff; //take only the 24 lower bits
290 
291  if (fObjects==0 || uid >= fObjects->GetSize()) return 0;
292  return fObjects->UncheckedAt(uid);
293 }
294 
295 ////////////////////////////////////////////////////////////////////////////////
296 /// static: returns pointer to current TProcessID
297 
299 {
300  return fgPID;
301 }
302 
303 ////////////////////////////////////////////////////////////////////////////////
304 /// static: returns array of TProcessIDs
305 
307 {
308  return fgPIDs;
309 }
310 
311 
312 ////////////////////////////////////////////////////////////////////////////////
313 /// static function. return kTRUE if pid is a valid TProcessID
314 
316 {
318 
319  if (fgPIDs==0) return kFALSE;
320  if (fgPIDs->IndexOf(pid) >= 0) return kTRUE;
321  if (pid == (TProcessID*)gROOT->GetUUIDs()) return kTRUE;
322  return kFALSE;
323 }
324 
325 ////////////////////////////////////////////////////////////////////////////////
326 /// stores the object at the uid th slot in the table of objects
327 /// The object uniqued is set as well as its kMustCleanup bit
328 
330 {
331  if (uid == 0) uid = obj->GetUniqueID() & 0xffffff;
332 
333  if (!fObjects) fObjects = new TObjArray(100);
334  fObjects->AddAtAndExpand(obj,uid);
335 
336  obj->SetBit(kMustCleanup);
337  if ( (obj->GetUniqueID()&0xff000000)==0xff000000 ) {
338  // We have more than 255 pids we need to store this
339  // pointer in the table(pointer,pid) since there is no
340  // more space in fUniqueID
341  if (fgObjPIDs==0) fgObjPIDs = new TExMap;
342  ULong_t hash = Void_Hash(obj);
343 
344  // We use operator() rather than Add() because
345  // if the address has already been registered, we want to
346  // update it's uniqueID (this can easily happen when the
347  // referenced object have been stored in a TClonesArray.
348  (*fgObjPIDs)(hash, (Long_t)obj) = GetUniqueID();
349  }
350 }
351 
352 ////////////////////////////////////////////////////////////////////////////////
353 /// called by the object destructor
354 /// remove reference to obj from the current table if it is referenced
355 
357 {
358  if (!fObjects) return;
359  if (!obj->TestBit(kIsReferenced)) return;
360  UInt_t uid = obj->GetUniqueID() & 0xffffff;
361  if (obj == GetObjectWithID(uid)) {
362  if (fgObjPIDs) {
363  ULong64_t hash = Void_Hash(obj);
364  fgObjPIDs->Remove(hash,(Long64_t)obj);
365  }
366  (*fObjects)[uid] = 0; // Avoid recalculation of fLast (compared to ->RemoveAt(uid))
367  }
368 }
369 
370 
371 ////////////////////////////////////////////////////////////////////////////////
372 /// static function to set the current referenced object count
373 /// fgNumber is incremented every time a new object is referenced
374 
376 {
377  fgNumber = number;
378 }
static TProcessID * GetPID()
static: returns pointer to current TProcessID
Definition: TProcessID.cxx:298
static TProcessID * GetProcessID(UShort_t pid)
static function returning a pointer to TProcessID number pid in fgPIDs
Definition: TProcessID.cxx:217
virtual void RecursiveRemove(TObject *obj)
called by the object destructor remove reference to obj from the current table if it is referenced ...
Definition: TProcessID.cxx:356
void PutObjectWithID(TObject *obj, UInt_t uid=0)
stores the object at the uid th slot in the table of objects The object uniqued is set as well as its...
Definition: TProcessID.cxx:329
void Remove(ULong64_t hash, Long64_t key)
Remove entry with specified key from the TExMap.
Definition: TExMap.cxx:216
An array of TObjects.
Definition: TObjArray.h:39
ClassImp(TProcessID) static inline ULong_t Void_Hash(const void *ptr)
Return hash value for this object.
Definition: TProcessID.cxx:56
long long Long64_t
Definition: RtypesCore.h:69
Int_t GetLast() const
Return index of last object in array.
Definition: TObjArray.cxx:528
const char Option_t
Definition: RtypesCore.h:62
virtual void Delete(Option_t *option="")
Remove all objects from the array AND delete all heap based objects.
Definition: TObjArray.cxx:329
static UInt_t GetNProcessIDs()
Return the (static) number of process IDs.
Definition: TProcessID.cxx:225
unsigned short UShort_t
Definition: RtypesCore.h:36
virtual void SetName(const char *name)
Change (i.e.
Definition: TNamed.cxx:128
virtual ~TProcessID()
Destructor.
Definition: TProcessID.cxx:78
#define gROOT
Definition: TROOT.h:344
virtual TObject * Remove(TObject *obj)
Remove object from array.
Definition: TObjArray.cxx:653
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
R__EXTERN TVirtualMutex * gROOTMutex
Definition: TROOT.h:63
const Bool_t kFALSE
Definition: Rtypes.h:92
Int_t GetEntriesFast() const
Definition: TObjArray.h:66
void SetBit(UInt_t f, Bool_t set)
Set or unset the user status bits as specified in f.
Definition: TObject.cxx:732
This class defines a UUID (Universally Unique IDentifier), also known as GUIDs (Globally Unique IDent...
Definition: TUUID.h:44
virtual void Fatal(const char *method, const char *msgfmt,...) const
Issue fatal error message.
Definition: TObject.cxx:946
static TObjArray * GetPIDs()
static: returns array of TProcessIDs
Definition: TProcessID.cxx:306
static TObjArray * fgPIDs
Definition: TProcessID.h:45
static TProcessID * fgPID
Array pointing to the referenced objects.
Definition: TProcessID.h:44
UChar_t mod R__LOCKGUARD2(gSrvAuthenticateMutex)
static void SetObjectCount(UInt_t number)
static function to set the current referenced object count fgNumber is incremented every time a new o...
Definition: TProcessID.cxx:375
A TProcessID identifies a ROOT job in a unique way in time and space.
Definition: TProcessID.h:34
static void Cleanup()
static function (called by TROOT destructor) to delete all TProcessIDs
Definition: TProcessID.cxx:173
virtual void SetUniqueID(UInt_t uid)
Set the unique object id.
Definition: TObject.cxx:743
TObject * UncheckedAt(Int_t i) const
Definition: TObjArray.h:91
Int_t fCount
Definition: TProcessID.h:41
Int_t IndexOf(const TObject *obj) const
Definition: TObjArray.cxx:552
Long64_t GetValue(ULong64_t hash, Long64_t key)
Return the value belonging to specified key and hash value.
Definition: TExMap.cxx:173
virtual void AddAtAndExpand(TObject *obj, Int_t idx)
Add object at position idx.
Definition: TObjArray.cxx:222
static TProcessID * GetProcessWithUID(const TObject *obj)
static function returning a pointer to TProcessID with its pid encoded in the highest byte of obj->Ge...
Definition: TProcessID.cxx:252
TProcessID()
Default constructor.
Definition: TProcessID.cxx:69
TObjArray * fObjects
Reference count to this object (from TFile)
Definition: TProcessID.h:42
Int_t IncrementCount()
Increase the reference count to this object.
Definition: TProcessID.cxx:268
unsigned int UInt_t
Definition: RtypesCore.h:42
Bool_t TestBit(UInt_t f) const
Definition: TObject.h:173
Int_t DecrementCount()
The reference fCount is used to delete the TProcessID in the TFile destructor when fCount = 0...
Definition: TProcessID.cxx:207
const char * AsString() const
Return UUID as string. Copy string immediately since it will be reused.
Definition: TUUID.cxx:536
static TProcessID * AddProcessID()
Static function to add a new TProcessID to the list of PIDs.
Definition: TProcessID.cxx:89
TObject * GetObjectWithID(UInt_t uid)
returns the TObject with unique identifier uid in the table of objects
Definition: TProcessID.cxx:287
void CheckInit()
Initialize fObjects.
Definition: TProcessID.cxx:165
long Long_t
Definition: RtypesCore.h:50
static UInt_t fgNumber
Definition: TProcessID.h:47
virtual Int_t GetSize() const
Definition: TCollection.h:95
static TExMap * fgObjPIDs
Definition: TProcessID.h:46
virtual void Clear(Option_t *option="")
delete the TObjArray pointing to referenced objects this function is called by TFile::Close("R") ...
Definition: TProcessID.cxx:187
unsigned long long ULong64_t
Definition: RtypesCore.h:70
unsigned long ULong_t
Definition: RtypesCore.h:51
Int_t GetEntries() const
Return the number of objects in array (i.e.
Definition: TObjArray.cxx:494
static TProcessID * GetSessionProcessID()
static function returning the pointer to the session TProcessID
Definition: TProcessID.cxx:260
virtual UInt_t GetUniqueID() const
Return the unique object id.
Definition: TObject.cxx:433
#define name(a, b)
Definition: linkTestLib0.cpp:5
Mother of all ROOT objects.
Definition: TObject.h:58
static UInt_t AssignID(TObject *obj)
static function returning the ID assigned to obj If the object is not yet referenced, its kIsReferenced bit is set and its fUniqueID set to the current number of referenced objects so far.
Definition: TProcessID.cxx:128
static UInt_t GetObjectCount()
Return the current referenced object count fgNumber is incremented every time a new object is referen...
Definition: TProcessID.cxx:279
void Add(TObject *obj)
Definition: TObjArray.h:75
static Bool_t IsValid(TProcessID *pid)
static function. return kTRUE if pid is a valid TProcessID
Definition: TProcessID.cxx:315
TObject * At(Int_t idx) const
Definition: TObjArray.h:167
const Bool_t kTRUE
Definition: Rtypes.h:91
virtual void SetTitle(const char *title="")
Change (i.e. set) the title of the TNamed.
Definition: TNamed.cxx:152
TObject * obj
This class stores a (key,value) pair using an external hash.
Definition: TExMap.h:35
virtual void Warning(const char *method, const char *msgfmt,...) const
Issue warning message.
Definition: TObject.cxx:904
UInt_t Hash(ECaseCompare cmp=kExact) const
Return hash value.
Definition: TString.cxx:592