Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
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 #include "TObject.h"
19 #include "TROOT.h"
20 
21 class TBrowser;
22 class TDirectory;
23 class TList;
24 class TMapRec;
25 
26 class TMapFile : public TObject {
27 
28 friend class TMapRec;
29 
30 private:
31  Int_t fFd; ///< Descriptor of mapped file
32  Int_t fVersion; ///< ROOT version (or -1 for shadow map file)
33  char *fName; ///< Name of mapped file
34  char *fTitle; ///< Title of mapped file
35  char *fOption; ///< Directory creation options
36  void *fMmallocDesc; ///< Pointer to mmalloc descriptor
37  ULong_t fBaseAddr; ///< Base address of mapped memory region
38  Int_t fSize; ///< Original start size of memory mapped region
39  TMapRec *fFirst; ///< List of streamed objects is shared memory
40  TMapRec *fLast; ///< Last object in list of shared objects
41  Long_t fOffset; ///< Offset in bytes for region mapped by reader
42  TDirectory *fDirectory; ///< Pointer to directory associated to this mapfile
43  TList *fBrowseList; ///< List of KeyMapFile objects
44  Bool_t fWritable; ///< TRUE if mapped file opened in RDWR mode
45  Int_t fSemaphore; ///< Modification semaphore (or getpid() for WIN32)
46  ULong_t fhSemaphore; ///< HANDLE of WIN32 Mutex object to implement semaphore
47  TObject *fGetting; ///< Don't deadlock in update mode, when from Get() Add() is called
48  Int_t fWritten; ///< Number of objects written sofar
49  Double_t fSumBuffer; ///< Sum of buffer sizes of objects written sofar
50  Double_t fSum2Buffer; ///< Sum of squares of buffer sizes of objects written so far
51 
52  static Long_t fgMapAddress; ///< Map to this address, set address via SetMapAddress()
53  static void *fgMmallocDesc; ///< Used in Close() and operator delete()
54 
55 protected:
56  TMapFile();
57  TMapFile(const char *name, const char *title, Option_t *option, Int_t size, TMapFile *&newMapFile);
58  TMapFile(const TMapFile &f, Long_t offset = 0);
59  void operator=(const TMapFile &rhs); // not implemented
60 
61  TMapFile *FindShadowMapFile();
62  void InitDirectory();
63  TObject *Remove(TObject *obj, Bool_t lock);
64  TObject *Remove(const char *name, Bool_t lock);
65  void SumBuffer(Int_t bufsize);
66  Int_t GetBestBuffer();
67 
68  void CreateSemaphore(Int_t pid=0);
69  Int_t AcquireSemaphore();
70  Int_t ReleaseSemaphore();
71  void DeleteSemaphore();
72 
73  static void *MapToAddress();
74 
75 public:
76  enum { kDefaultMapSize = 0x80000 }; // default size of mapped heap is 500 KB
77 
78  // Should both be protected (waiting for cint)
79  virtual ~TMapFile();
80  void operator delete(void *vp);
81 
82  void Browse(TBrowser *b);
83  void Close(Option_t *option = "");
84  void *GetBaseAddr() const { return (void *)fBaseAddr; }
85  void *GetBreakval() const;
86  TDirectory *GetDirectory() const {return fDirectory;}
87  Int_t GetFd() const { return fFd; }
88  void *GetMmallocDesc() const { return fMmallocDesc; }
89  const char *GetName() const { return fName; }
90  Int_t GetSize() const { return fSize; }
91  const char *GetOption() const { return fOption; }
92  const char *GetTitle() const { return fTitle; }
93  TMapRec *GetFirst() const { return (TMapRec*)((Long_t) fFirst + fOffset); }
94  TMapRec *GetLast() const { return (TMapRec*)((Long_t) fLast + fOffset); }
95  Bool_t IsFolder() const;
96  Bool_t IsWritable() const { return fWritable; }
97  void *OrgAddress(void *addr) const { return (void *)((Long_t)addr - fOffset); }
98  void Print(Option_t *option="") const;
99  void ls(Option_t *option="") const;
100  Bool_t cd(const char *path = 0);
101 
102  void Add(const TObject *obj, const char *name = "");
103  void Update(TObject *obj = 0);
104  TObject *Remove(TObject *obj) { return Remove(obj, kTRUE); }
105  TObject *Remove(const char *name) { return Remove(name, kTRUE); }
106  void RemoveAll();
107  TObject *Get(const char *name, TObject *retObj = 0);
108 
109  static TMapFile *Create(const char *name, Option_t *option="READ", Int_t size=kDefaultMapSize, const char *title="");
110  static TMapFile *WhichMapFile(void *addr);
111  static void SetMapAddress(Long_t addr);
112 
113  ClassDef(TMapFile,0) // Memory mapped directory structure
114 };
115 
116 
117 
118 /**
119 \class TMapRec
120 \ingroup IO
121 
122 Keep track of an object in the mapped file.
123 
124 A TMapFile contains a list of TMapRec objects which keep track of
125 the actual objects stored in the mapped file.
126 */
127 
128 class TMapRec {
129 
130 friend class TMapFile;
131 
132 private:
133  char *fName; ///< Object name
134  char *fClassName; ///< Class name
135  TObject *fObject; ///< Pointer to original object
136  void *fBuffer; ///< Buffer containing object of class name
137  Int_t fBufSize; ///< Buffer size
138  TMapRec *fNext; ///< Next MapRec in list
139 
140  TMapRec(const TMapRec&); // Not implemented.
141  TMapRec &operator=(const TMapRec&); // Not implemented.
142 
143 public:
144  TMapRec(const char *name, const TObject *obj, Int_t size, void *buf);
145  ~TMapRec();
146  const char *GetName(Long_t offset = 0) const { return (char *)((Long_t) fName + offset); }
147  const char *GetClassName(Long_t offset = 0) const { return (char *)((Long_t) fClassName + offset); }
148  void *GetBuffer(Long_t offset = 0) const { return (void *)((Long_t) fBuffer + offset); }
149  Int_t GetBufSize() const { return fBufSize; }
150  TObject *GetObject() const;
151  TMapRec *GetNext(Long_t offset = 0) const { return (TMapRec *)((Long_t) fNext + offset); }
152 };
153 
154 #endif