Logo ROOT   6.08/07
Reference Guide
TMapFile.h
Go to the documentation of this file.
1 // @(#)root/io:$Id$
2 // Author: Fons Rademakers 08/07/97
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 #ifndef ROOT_TMapFile
13 #define ROOT_TMapFile
14 
15 #ifdef WIN32
16 #include "Windows4Root.h"
17 #endif
18 #ifndef ROOT_TObject
19 #include "TObject.h"
20 #endif
21 #ifndef ROOT_TROOT
22 #include "TROOT.h"
23 #endif
24 #if !defined(__MMPRIVATE_H) && !defined(__CINT__)
25 #include "mmprivate.h"
26 // Undef a few common words, see ROOT-8475
27 #undef PTR
28 #undef PARAMS
29 #undef HEAP
30 #undef BLOCK
31 #undef BLOCKSIZE
32 #undef BLOCKIFY
33 #undef ADDRESS
34 #undef ADDR2UINT
35 #undef RESIDUAL
36 #endif
37 
38 
39 class TBrowser;
40 class TDirectory;
41 class TList;
42 class TMapRec;
43 
44 class TMapFile : public TObject {
45 
46 friend class TMapRec;
47 
48 private:
49  Int_t fFd; ///< Descriptor of mapped file
50  Int_t fVersion; ///< ROOT version (or -1 for shadow map file)
51  char *fName; ///< Name of mapped file
52  char *fTitle; ///< Title of mapped file
53  char *fOption; ///< Directory creation options
54  void *fMmallocDesc; ///< Pointer to mmalloc descriptor
55  ULong_t fBaseAddr; ///< Base address of mapped memory region
56  Int_t fSize; ///< Original start size of memory mapped region
57  TMapRec *fFirst; ///< List of streamed objects is shared memory
58  TMapRec *fLast; ///< Last object in list of shared objects
59  Long_t fOffset; ///< Offset in bytes for region mapped by reader
60  TDirectory *fDirectory; ///< Pointer to directory associated to this mapfile
61  TList *fBrowseList; ///< List of KeyMapFile objects
62  Bool_t fWritable; ///< TRUE if mapped file opened in RDWR mode
63  Int_t fSemaphore; ///< Modification semaphore (or getpid() for WIN32)
64  ULong_t fhSemaphore; ///< HANDLE of WIN32 Mutex object to implement semaphore
65  TObject *fGetting; ///< Don't deadlock in update mode, when from Get() Add() is called
66  Int_t fWritten; ///< Number of objects written sofar
67  Double_t fSumBuffer; ///< Sum of buffer sizes of objects written sofar
68  Double_t fSum2Buffer; ///< Sum of squares of buffer sizes of objects written so far
69 
70  static Long_t fgMapAddress; ///< Map to this address, set address via SetMapAddress()
71  static void *fgMmallocDesc; ///< Used in Close() and operator delete()
72 
73 protected:
74  TMapFile();
75  TMapFile(const char *name, const char *title, Option_t *option, Int_t size, TMapFile *&newMapFile);
76  TMapFile(const TMapFile &f, Long_t offset = 0);
77  void operator=(const TMapFile &rhs); // not implemented
78 
80  void InitDirectory();
81  TObject *Remove(TObject *obj, Bool_t lock);
82  TObject *Remove(const char *name, Bool_t lock);
83  void SumBuffer(Int_t bufsize);
85 
86  void CreateSemaphore(Int_t pid=0);
89  void DeleteSemaphore();
90 
91  static void *MapToAddress();
92 
93 public:
94  enum { kDefaultMapSize = 0x80000 }; // default size of mapped heap is 500 KB
95 
96  // Should both be protected (waiting for cint)
97  virtual ~TMapFile();
98  void operator delete(void *vp);
99 
100  void Browse(TBrowser *b);
101  void Close(Option_t *option = "");
102  void *GetBaseAddr() const { return (void *)fBaseAddr; }
103  void *GetBreakval() const;
104  TDirectory *GetDirectory() const {return fDirectory;}
105  Int_t GetFd() const { return fFd; }
106  void *GetMmallocDesc() const { return fMmallocDesc; }
107  const char *GetName() const { return fName; }
108  Int_t GetSize() const { return fSize; }
109  const char *GetOption() const { return fOption; }
110  const char *GetTitle() const { return fTitle; }
111  TMapRec *GetFirst() const { return (TMapRec*)((Long_t) fFirst + fOffset); }
112  TMapRec *GetLast() const { return (TMapRec*)((Long_t) fLast + fOffset); }
113  Bool_t IsFolder() const;
114  Bool_t IsWritable() const { return fWritable; }
115  void *OrgAddress(void *addr) const { return (void *)((Long_t)addr - fOffset); }
116  void Print(Option_t *option="") const;
117  void ls(Option_t *option="") const;
118  Bool_t cd(const char *path = 0);
119 
120  void Add(const TObject *obj, const char *name = "");
121  void Update(TObject *obj = 0);
122  TObject *Remove(TObject *obj) { return Remove(obj, kTRUE); }
123  TObject *Remove(const char *name) { return Remove(name, kTRUE); }
124  void RemoveAll();
125  TObject *Get(const char *name, TObject *retObj = 0);
126 
127  static TMapFile *Create(const char *name, Option_t *option="READ", Int_t size=kDefaultMapSize, const char *title="");
128  static TMapFile *WhichMapFile(void *addr);
129  static void SetMapAddress(Long_t addr);
130 
131  ClassDef(TMapFile,0) // Memory mapped directory structure
132 };
133 
134 
135 
136 /**
137 \class TMapRec
138 \ingroup IO
139 
140 Keep track of an object in the mapped file.
141 
142 A TMapFile contains a list of TMapRec objects which keep track of
143 the actual objects stored in the mapped file.
144 */
145 
146 class TMapRec {
147 
148 friend class TMapFile;
149 
150 private:
151  char *fName; ///< Object name
152  char *fClassName; ///< Class name
153  TObject *fObject; ///< Pointer to original object
154  void *fBuffer; ///< Buffer containing object of class name
155  Int_t fBufSize; ///< Buffer size
156  TMapRec *fNext; ///< Next MapRec in list
157 
158  TMapRec(const TMapRec&); // Not implemented.
159  TMapRec &operator=(const TMapRec&); // Not implemented.
160 
161 public:
162  TMapRec(const char *name, const TObject *obj, Int_t size, void *buf);
163  ~TMapRec();
164  const char *GetName(Long_t offset = 0) const { return (char *)((Long_t) fName + offset); }
165  const char *GetClassName(Long_t offset = 0) const { return (char *)((Long_t) fClassName + offset); }
166  void *GetBuffer(Long_t offset = 0) const { return (void *)((Long_t) fBuffer + offset); }
167  Int_t GetBufSize() const { return fBufSize; }
168  TObject *GetObject() const;
169  TMapRec *GetNext(Long_t offset = 0) const { return (TMapRec *)((Long_t) fNext + offset); }
170 };
171 
172 ////////////////////////////////////////////////////////////////////////////////
173 /// Return the current location in the memory region for this malloc heap which
174 /// represents the end of memory in use. Returns 0 if map file was closed.
175 
176 inline void *TMapFile::GetBreakval() const
177 {
178  if (!fMmallocDesc) return 0;
179  return (void *)((struct mdesc *)fMmallocDesc)->breakval;
180 }
181 
182 ////////////////////////////////////////////////////////////////////////////////
183 
184 inline TMapFile *TMapFile::WhichMapFile(void *addr)
185 {
186  if (!gROOT || !gROOT->GetListOfMappedFiles()) return 0;
187 
188  TObjLink *lnk = ((TList *)gROOT->GetListOfMappedFiles())->LastLink();
189  while (lnk) {
190  TMapFile *mf = (TMapFile*)lnk->GetObject();
191  if (!mf) return 0;
192  if ((ULong_t)addr >= mf->fBaseAddr + mf->fOffset &&
193  (ULong_t)addr < (ULong_t)mf->GetBreakval() + mf->fOffset)
194  return mf;
195  lnk = lnk->Prev();
196  }
197  return 0;
198 }
199 
200 R__EXTERN void *gMmallocDesc; //is initialized in TClass.cxx
201 
202 #endif
void * GetBreakval() const
Return the current location in the memory region for this malloc heap which represents the end of mem...
Definition: TMapFile.h:176
TMapRec * fNext
Next MapRec in list.
Definition: TMapFile.h:156
TObject * Remove(TObject *obj)
Definition: TMapFile.h:122
void * GetBaseAddr() const
Definition: TMapFile.h:102
This class implements a shared memory region mapped to a file.
Definition: TMapFile.h:44
void * GetBuffer(Long_t offset=0) const
Definition: TMapFile.h:166
static TMapFile * Create(const char *name, Option_t *option="READ", Int_t size=kDefaultMapSize, const char *title="")
Create a memory mapped file.
Definition: TMapFile.cxx:1104
static void SetMapAddress(Long_t addr)
Set preferred map address.
Definition: TMapFile.cxx:1141
static Long_t fgMapAddress
Map to this address, set address via SetMapAddress()
Definition: TMapFile.h:70
R__EXTERN void * gMmallocDesc
Definition: TMapFile.h:200
const char * GetOption() const
Definition: TMapFile.h:109
TMapFile * FindShadowMapFile()
Returns shadow map file.
Definition: TMapFile.cxx:965
const char Option_t
Definition: RtypesCore.h:62
virtual ~TMapFile()
TMapFiles may not be deleted, since we want to keep the complete TMapFile object in the mapped file f...
Definition: TMapFile.cxx:521
void operator=(const TMapFile &rhs)
Int_t fSize
Original start size of memory mapped region.
Definition: TMapFile.h:56
#define gROOT
Definition: TROOT.h:364
Int_t fVersion
ROOT version (or -1 for shadow map file)
Definition: TMapFile.h:50
char * fTitle
Title of mapped file.
Definition: TMapFile.h:52
Int_t ReleaseSemaphore()
Release semaphore. Returns 0 if OK, -1 on error.
Definition: TMapFile.cxx:888
void CreateSemaphore(Int_t pid=0)
Create semaphore used for synchronizing access to shared memory.
Definition: TMapFile.cxx:788
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
Int_t fBufSize
Buffer size.
Definition: TMapFile.h:155
void * OrgAddress(void *addr) const
Definition: TMapFile.h:115
void Update(TObject *obj=0)
Update an object (or all objects, if obj == 0) in shared memory.
Definition: TMapFile.cxx:600
static void * MapToAddress()
Return the base address at which we would like the next TMapFile&#39;s mapped data to start...
Definition: TMapFile.cxx:1163
TObject * fGetting
Don&#39;t deadlock in update mode, when from Get() Add() is called.
Definition: TMapFile.h:65
TObject * Get(const char *name, TObject *retObj=0)
Return pointer to object retrieved from shared memory.
Definition: TMapFile.cxx:741
void * fMmallocDesc
Pointer to mmalloc descriptor.
Definition: TMapFile.h:54
const char * GetName() const
Returns name of object.
Definition: TMapFile.h:107
Int_t GetBestBuffer()
Return the best buffer size for objects in this file.
Definition: TMapFile.cxx:1083
Int_t fWritten
Number of objects written sofar.
Definition: TMapFile.h:66
TObject * fObject
Pointer to original object.
Definition: TMapFile.h:153
TMapRec * fLast
Last object in list of shared objects.
Definition: TMapFile.h:58
Double_t fSum2Buffer
Sum of squares of buffer sizes of objects written so far.
Definition: TMapFile.h:68
void InitDirectory()
Create the directory associated to this mapfile.
Definition: TMapFile.cxx:546
#define ClassDef(name, id)
Definition: Rtypes.h:254
const char * GetClassName(Long_t offset=0) const
Definition: TMapFile.h:165
ULong_t fBaseAddr
Base address of mapped memory region.
Definition: TMapFile.h:55
Double_t fSumBuffer
Sum of buffer sizes of objects written sofar.
Definition: TMapFile.h:67
Int_t GetBufSize() const
Definition: TMapFile.h:167
ULong_t fhSemaphore
HANDLE of WIN32 Mutex object to implement semaphore.
Definition: TMapFile.h:64
Int_t GetFd() const
Definition: TMapFile.h:105
void Add(const TObject *obj, const char *name="")
Add an object to the list of objects to be stored in shared memory.
Definition: TMapFile.cxx:561
const char * GetName(Long_t offset=0) const
Definition: TMapFile.h:164
Bool_t fWritable
TRUE if mapped file opened in RDWR mode.
Definition: TMapFile.h:62
A doubly linked list.
Definition: TList.h:47
Using a TBrowser one can browse all ROOT objects.
Definition: TBrowser.h:41
void * fBuffer
Buffer containing object of class name.
Definition: TMapFile.h:154
Int_t AcquireSemaphore()
Acquire semaphore. Returns 0 if OK, -1 on error.
Definition: TMapFile.cxx:843
char * fOption
Directory creation options.
Definition: TMapFile.h:53
void SumBuffer(Int_t bufsize)
Increment statistics for buffer sizes of objects in this file.
Definition: TMapFile.cxx:1069
TObject * Remove(const char *name)
Definition: TMapFile.h:123
Int_t fFd
Descriptor of mapped file.
Definition: TMapFile.h:49
TMapRec * GetFirst() const
Definition: TMapFile.h:111
long Long_t
Definition: RtypesCore.h:50
static void * fgMmallocDesc
Used in Close() and operator delete()
Definition: TMapFile.h:71
TMapRec * GetLast() const
Definition: TMapFile.h:112
Bool_t IsFolder() const
Returns kTRUE in case object is a folder (i.e. contains browsable lists).
Definition: TMapFile.cxx:998
double f(double x)
TObject * Remove(TObject *obj, Bool_t lock)
Remove object from shared memory.
Definition: TMapFile.cxx:640
double Double_t
Definition: RtypesCore.h:55
void * GetMmallocDesc() const
Definition: TMapFile.h:106
Describe directory structure in memory.
Definition: TDirectory.h:44
unsigned long ULong_t
Definition: RtypesCore.h:51
Keep track of an object in the mapped file.
Definition: TMapFile.h:146
Int_t fSemaphore
Modification semaphore (or getpid() for WIN32)
Definition: TMapFile.h:63
Mother of all ROOT objects.
Definition: TObject.h:37
TMapRec * fFirst
List of streamed objects is shared memory.
Definition: TMapFile.h:57
#define R__EXTERN
Definition: DllImport.h:27
char * fClassName
Class name.
Definition: TMapFile.h:152
void RemoveAll()
Remove all objects from shared memory.
Definition: TMapFile.cxx:716
Int_t GetSize() const
Definition: TMapFile.h:108
char * fName
Object name.
Definition: TMapFile.h:151
void DeleteSemaphore()
Delete the semaphore.
Definition: TMapFile.cxx:818
Bool_t cd(const char *path=0)
Cd to associated directory.
Definition: TMapFile.cxx:1034
you should not use this method at all Int_t Int_t Double_t Double_t Double_t Int_t Double_t Double_t Double_t Double_t b
Definition: TRolke.cxx:630
Long_t fOffset
Offset in bytes for region mapped by reader.
Definition: TMapFile.h:59
void Print(Option_t *option="") const
Print some info about the mapped file.
Definition: TMapFile.cxx:981
Bool_t IsWritable() const
Definition: TMapFile.h:114
void Browse(TBrowser *b)
Browse contents of TMapFile.
Definition: TMapFile.cxx:1007
const char * GetTitle() const
Returns title of object.
Definition: TMapFile.h:110
TList * fBrowseList
List of KeyMapFile objects.
Definition: TMapFile.h:61
void Close(Option_t *option="")
Close a mapped file.
Definition: TMapFile.cxx:920
const Bool_t kTRUE
Definition: Rtypes.h:91
TMapFile()
Default ctor. Does not much except setting some basic values.
Definition: TMapFile.cxx:170
TDirectory * fDirectory
Pointer to directory associated to this mapfile.
Definition: TMapFile.h:60
char * fName
Name of mapped file.
Definition: TMapFile.h:51
static TMapFile * WhichMapFile(void *addr)
Definition: TMapFile.h:184
friend class TMapRec
Definition: TMapFile.h:46
TMapRec * GetNext(Long_t offset=0) const
Definition: TMapFile.h:169
char name[80]
Definition: TGX11.cxx:109
TDirectory * GetDirectory() const
Definition: TMapFile.h:104
void ls(Option_t *option="") const
List contents of TMapFile.
Definition: TMapFile.cxx:1044