ROOT  6.06/09
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 #endif
27 
28 
29 class TBrowser;
30 class TDirectory;
31 class TList;
32 class TMapRec;
33 
34 class TMapFile : public TObject {
35 
36 friend class TMapRec;
37 
38 private:
39  Int_t fFd; ///< Descriptor of mapped file
40  Int_t fVersion; ///< ROOT version (or -1 for shadow map file)
41  char *fName; ///< Name of mapped file
42  char *fTitle; ///< Title of mapped file
43  char *fOption; ///< Directory creation options
44  void *fMmallocDesc; ///< Pointer to mmalloc descriptor
45  ULong_t fBaseAddr; ///< Base address of mapped memory region
46  Int_t fSize; ///< Original start size of memory mapped region
47  TMapRec *fFirst; ///< List of streamed objects is shared memory
48  TMapRec *fLast; ///< Last object in list of shared objects
49  Long_t fOffset; ///< Offset in bytes for region mapped by reader
50  TDirectory *fDirectory; ///< Pointer to directory associated to this mapfile
51  TList *fBrowseList; ///< List of KeyMapFile objects
52  Bool_t fWritable; ///< TRUE if mapped file opened in RDWR mode
53  Int_t fSemaphore; ///< Modification semaphore (or getpid() for WIN32)
54  ULong_t fhSemaphore; ///< HANDLE of WIN32 Mutex object to implement semaphore
55  TObject *fGetting; ///< Don't deadlock in update mode, when from Get() Add() is called
56  Int_t fWritten; ///< Number of objects written sofar
57  Double_t fSumBuffer; ///< Sum of buffer sizes of objects written sofar
58  Double_t fSum2Buffer; ///< Sum of squares of buffer sizes of objects written so far
59 
60  static Long_t fgMapAddress; ///< Map to this address, set address via SetMapAddress()
61  static void *fgMmallocDesc; ///< Used in Close() and operator delete()
62 
63 protected:
64  TMapFile();
65  TMapFile(const char *name, const char *title, Option_t *option, Int_t size, TMapFile *&newMapFile);
66  TMapFile(const TMapFile &f, Long_t offset = 0);
67  void operator=(const TMapFile &rhs); // not implemented
68 
70  void InitDirectory();
71  TObject *Remove(TObject *obj, Bool_t lock);
72  TObject *Remove(const char *name, Bool_t lock);
73  void SumBuffer(Int_t bufsize);
75 
76  void CreateSemaphore(Int_t pid=0);
79  void DeleteSemaphore();
80 
81  static void *MapToAddress();
82 
83 public:
84  enum { kDefaultMapSize = 0x80000 }; // default size of mapped heap is 500 KB
85 
86  // Should both be protected (waiting for cint)
87  virtual ~TMapFile();
88  void operator delete(void *vp);
89 
90  void Browse(TBrowser *b);
91  void Close(Option_t *option = "");
92  void *GetBaseAddr() const { return (void *)fBaseAddr; }
93  void *GetBreakval() const;
94  TDirectory *GetDirectory() const {return fDirectory;}
95  Int_t GetFd() const { return fFd; }
96  void *GetMmallocDesc() const { return fMmallocDesc; }
97  const char *GetName() const { return fName; }
98  Int_t GetSize() const { return fSize; }
99  const char *GetOption() const { return fOption; }
100  const char *GetTitle() const { return fTitle; }
101  TMapRec *GetFirst() const { return (TMapRec*)((Long_t) fFirst + fOffset); }
102  TMapRec *GetLast() const { return (TMapRec*)((Long_t) fLast + fOffset); }
103  Bool_t IsFolder() const;
104  Bool_t IsWritable() const { return fWritable; }
105  void *OrgAddress(void *addr) const { return (void *)((Long_t)addr - fOffset); }
106  void Print(Option_t *option="") const;
107  void ls(Option_t *option="") const;
108  Bool_t cd(const char *path = 0);
109 
110  void Add(const TObject *obj, const char *name = "");
111  void Update(TObject *obj = 0);
112  TObject *Remove(TObject *obj) { return Remove(obj, kTRUE); }
113  TObject *Remove(const char *name) { return Remove(name, kTRUE); }
114  void RemoveAll();
115  TObject *Get(const char *name, TObject *retObj = 0);
116 
117  static TMapFile *Create(const char *name, Option_t *option="READ", Int_t size=kDefaultMapSize, const char *title="");
118  static TMapFile *WhichMapFile(void *addr);
119  static void SetMapAddress(Long_t addr);
120 
121  ClassDef(TMapFile,0) // Memory mapped directory structure
122 };
123 
124 
125 
126 /**
127 \class TMapRec
128 \ingroup IO
129 
130 Keep track of an object in the mapped file.
131 
132 A TMapFile contains a list of TMapRec objects which keep track of
133 the actual objects stored in the mapped file.
134 */
135 
136 class TMapRec {
137 
138 friend class TMapFile;
139 
140 private:
141  char *fName; ///< Object name
142  char *fClassName; ///< Class name
143  TObject *fObject; ///< Pointer to original object
144  void *fBuffer; ///< Buffer containing object of class name
145  Int_t fBufSize; ///< Buffer size
146  TMapRec *fNext; ///< Next MapRec in list
147 
148  TMapRec(const TMapRec&); // Not implemented.
149  TMapRec &operator=(const TMapRec&); // Not implemented.
150 
151 public:
152  TMapRec(const char *name, const TObject *obj, Int_t size, void *buf);
153  ~TMapRec();
154  const char *GetName(Long_t offset = 0) const { return (char *)((Long_t) fName + offset); }
155  const char *GetClassName(Long_t offset = 0) const { return (char *)((Long_t) fClassName + offset); }
156  void *GetBuffer(Long_t offset = 0) const { return (void *)((Long_t) fBuffer + offset); }
157  Int_t GetBufSize() const { return fBufSize; }
158  TObject *GetObject() const;
159  TMapRec *GetNext(Long_t offset = 0) const { return (TMapRec *)((Long_t) fNext + offset); }
160 };
161 
162 ////////////////////////////////////////////////////////////////////////////////
163 /// Return the current location in the memory region for this malloc heap which
164 /// represents the end of memory in use. Returns 0 if map file was closed.
165 
166 inline void *TMapFile::GetBreakval() const
167 {
168  if (!fMmallocDesc) return 0;
169  return (void *)((struct mdesc *)fMmallocDesc)->breakval;
170 }
171 
172 ////////////////////////////////////////////////////////////////////////////////
173 
174 inline TMapFile *TMapFile::WhichMapFile(void *addr)
175 {
176  if (!gROOT || !gROOT->GetListOfMappedFiles()) return 0;
177 
178  TObjLink *lnk = ((TList *)gROOT->GetListOfMappedFiles())->LastLink();
179  while (lnk) {
180  TMapFile *mf = (TMapFile*)lnk->GetObject();
181  if (!mf) return 0;
182  if ((ULong_t)addr >= mf->fBaseAddr + mf->fOffset &&
183  (ULong_t)addr < (ULong_t)mf->GetBreakval() + mf->fOffset)
184  return mf;
185  lnk = lnk->Prev();
186  }
187  return 0;
188 }
189 
190 R__EXTERN void *gMmallocDesc; //is initialized in TClass.cxx
191 
192 #endif
TMapRec * GetLast() const
Definition: TMapFile.h:102
TMapRec * fNext
Next MapRec in list.
Definition: TMapFile.h:146
void * GetMmallocDesc() const
Definition: TMapFile.h:96
TObject * Remove(TObject *obj)
Definition: TMapFile.h:112
This class implements a shared memory region mapped to a file.
Definition: TMapFile.h:34
void Print(Option_t *option="") const
Print some info about the mapped file.
Definition: TMapFile.cxx:981
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:60
R__EXTERN void * gMmallocDesc
Definition: TMapFile.h:190
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:46
const char * GetTitle() const
Returns title of object.
Definition: TMapFile.h:100
Int_t GetFd() const
Definition: TMapFile.h:95
#define gROOT
Definition: TROOT.h:340
Int_t fVersion
ROOT version (or -1 for shadow map file)
Definition: TMapFile.h:40
char * fTitle
Title of mapped file.
Definition: TMapFile.h:42
Int_t ReleaseSemaphore()
Release semaphore. Returns 0 if OK, -1 on error.
Definition: TMapFile.cxx:888
~TMapRec()
Destructor.
Definition: TMapFile.cxx:145
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:145
TDirectory * GetDirectory() const
Definition: TMapFile.h:94
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's mapped data to start...
Definition: TMapFile.cxx:1163
TObject * fGetting
Don't deadlock in update mode, when from Get() Add() is called.
Definition: TMapFile.h:55
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:44
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:56
TObject * fObject
Pointer to original object.
Definition: TMapFile.h:143
TMapRec * fLast
Last object in list of shared objects.
Definition: TMapFile.h:48
Double_t fSum2Buffer
Sum of squares of buffer sizes of objects written so far.
Definition: TMapFile.h:58
void InitDirectory()
Create the directory associated to this mapfile.
Definition: TMapFile.cxx:546
#define ClassDef(name, id)
Definition: Rtypes.h:254
ULong_t fBaseAddr
Base address of mapped memory region.
Definition: TMapFile.h:45
Double_t fSumBuffer
Sum of buffer sizes of objects written sofar.
Definition: TMapFile.h:57
ULong_t fhSemaphore
HANDLE of WIN32 Mutex object to implement semaphore.
Definition: TMapFile.h:54
const char * GetOption() const
Definition: TMapFile.h:99
Bool_t IsWritable() const
Definition: TMapFile.h:104
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
void * OrgAddress(void *addr) const
Definition: TMapFile.h:105
Bool_t fWritable
TRUE if mapped file opened in RDWR mode.
Definition: TMapFile.h:52
A doubly linked list.
Definition: TList.h:47
void ls(Option_t *option="") const
List contents of TMapFile.
Definition: TMapFile.cxx:1044
Using a TBrowser one can browse all ROOT objects.
Definition: TBrowser.h:41
void * fBuffer
Buffer containing object of class name.
Definition: TMapFile.h:144
void * GetBuffer(Long_t offset=0) const
Definition: TMapFile.h:156
TMapRec(const TMapRec &)
TObject * GetObject() const
This method returns a pointer to the original object.
Definition: TMapFile.cxx:157
Int_t AcquireSemaphore()
Acquire semaphore. Returns 0 if OK, -1 on error.
Definition: TMapFile.cxx:843
void * GetBaseAddr() const
Definition: TMapFile.h:92
TMapRec * GetNext(Long_t offset=0) const
Definition: TMapFile.h:159
char * fOption
Directory creation options.
Definition: TMapFile.h:43
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:113
Int_t fFd
Descriptor of mapped file.
Definition: TMapFile.h:39
const char * GetName() const
Returns name of object.
Definition: TMapFile.h:97
const char * GetClassName(Long_t offset=0) const
Definition: TMapFile.h:155
long Long_t
Definition: RtypesCore.h:50
static void * fgMmallocDesc
Used in Close() and operator delete()
Definition: TMapFile.h:61
void * GetBreakval() const
Return the current location in the memory region for this malloc heap which represents the end of mem...
Definition: TMapFile.h:166
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
Describe directory structure in memory.
Definition: TDirectory.h:41
unsigned long ULong_t
Definition: RtypesCore.h:51
Bool_t IsFolder() const
Returns kTRUE in case object is a folder (i.e. contains browsable lists).
Definition: TMapFile.cxx:998
Keep track of an object in the mapped file.
Definition: TMapFile.h:136
Int_t fSemaphore
Modification semaphore (or getpid() for WIN32)
Definition: TMapFile.h:53
#define name(a, b)
Definition: linkTestLib0.cpp:5
Mother of all ROOT objects.
Definition: TObject.h:58
TMapRec * fFirst
List of streamed objects is shared memory.
Definition: TMapFile.h:47
#define R__EXTERN
Definition: DllImport.h:27
const char * GetName(Long_t offset=0) const
Definition: TMapFile.h:154
char * fClassName
Class name.
Definition: TMapFile.h:142
void RemoveAll()
Remove all objects from shared memory.
Definition: TMapFile.cxx:716
char * fName
Object name.
Definition: TMapFile.h:141
void DeleteSemaphore()
Delete the semaphore.
Definition: TMapFile.cxx:818
Bool_t cd(const char *path=0)
Cd to associated directory.
Definition: TMapFile.cxx:1034
Long_t fOffset
Offset in bytes for region mapped by reader.
Definition: TMapFile.h:49
void Browse(TBrowser *b)
Browse contents of TMapFile.
Definition: TMapFile.cxx:1007
TList * fBrowseList
List of KeyMapFile objects.
Definition: TMapFile.h:51
void Close(Option_t *option="")
Close a mapped file.
Definition: TMapFile.cxx:920
const Bool_t kTRUE
Definition: Rtypes.h:91
TObject * obj
TDirectory * fDirectory
Pointer to directory associated to this mapfile.
Definition: TMapFile.h:50
char * fName
Name of mapped file.
Definition: TMapFile.h:41
static TMapFile * WhichMapFile(void *addr)
Definition: TMapFile.h:174
Int_t GetBufSize() const
Definition: TMapFile.h:157
Int_t GetSize() const
Definition: TMapFile.h:98
TMapRec * GetFirst() const
Definition: TMapFile.h:101
TMapRec & operator=(const TMapRec &)