TMapFile.h

Go to the documentation of this file.
00001 // @(#)root/io:$Id: TMapFile.h 23122 2008-04-10 14:56:30Z rdm $
00002 // Author: Fons Rademakers   08/07/97
00003 
00004 /*************************************************************************
00005  * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers.               *
00006  * All rights reserved.                                                  *
00007  *                                                                       *
00008  * For the licensing terms see $ROOTSYS/LICENSE.                         *
00009  * For the list of contributors see $ROOTSYS/README/CREDITS.             *
00010  *************************************************************************/
00011 
00012 #ifndef ROOT_TMapFile
00013 #define ROOT_TMapFile
00014 
00015 
00016 //////////////////////////////////////////////////////////////////////////
00017 //                                                                      //
00018 // TMapFile                                                             //
00019 //                                                                      //
00020 // This class implements a shared memory region mapped to a file.       //
00021 // Objects can be placed into this shared memory area using the Add()   //
00022 // member function. Whenever the mapped object(s) change(s) call        //
00023 // Update() to put a fresh copy in the shared memory. This extra        //
00024 // step is necessary since it is not possible to share objects with     //
00025 // virtual pointers between processes (the vtbl ptr points to the       //
00026 // originators unique address space and can not be used by the          //
00027 // consumer process(es)). Consumer processes can map the memory region  //
00028 // from this file and access the objects stored in it via the Get()     //
00029 // method (which returns a copy of the object stored in the shared      //
00030 // memory with correct vtbl ptr set). Only objects of classes with a    //
00031 // Streamer() member function defined can be shared.                    //
00032 //                                                                      //
00033 //////////////////////////////////////////////////////////////////////////
00034 
00035 #ifdef WIN32
00036 #include "Windows4Root.h"
00037 #endif
00038 #ifndef ROOT_TObject
00039 #include "TObject.h"
00040 #endif
00041 #ifndef ROOT_TROOT
00042 #include "TROOT.h"
00043 #endif
00044 #if !defined(__MMPRIVATE_H) && !defined(__CINT__)
00045 #include "mmprivate.h"
00046 #endif
00047 
00048 
00049 class TBrowser;
00050 class TDirectory;
00051 class TList;
00052 class TMapRec;
00053 
00054 class TMapFile : public TObject {
00055 
00056 friend class TMapRec;
00057 
00058 private:
00059    Int_t       fFd;             //Descriptor of mapped file
00060    Int_t       fVersion;        //ROOT version (or -1 for shadow map file)
00061    char       *fName;           //Name of mapped file
00062    char       *fTitle;          //Title of mapped file
00063    char       *fOption;         //Directory creation options
00064    void       *fMmallocDesc;    //Pointer to mmalloc descriptor
00065    ULong_t     fBaseAddr;       //Base address of mapped memory region
00066    Int_t       fSize;           //Original start size of memory mapped region
00067    TMapRec    *fFirst;          //List of streamed objects is shared memory
00068    TMapRec    *fLast;           //Last object in list of shared objects
00069    Long_t      fOffset;         //Offset in bytes for region mapped by reader
00070    TDirectory *fDirectory;      //Pointer to directory associated to this mapfile
00071    TList      *fBrowseList;     //List of KeyMapFile objects
00072    Bool_t      fWritable;       //TRUE if mapped file opened in RDWR mode
00073    Int_t       fSemaphore;      //Modification semaphore (or getpid() for WIN32)
00074    ULong_t     fhSemaphore;     //HANDLE of WIN32 Mutex object to implement semaphore
00075    TObject    *fGetting;        //Don't deadlock in update mode, when from Get() Add() is called
00076    Int_t       fWritten;        //Number of objects written sofar
00077    Double_t    fSumBuffer;      //Sum of buffer sizes of objects written sofar
00078    Double_t    fSum2Buffer;     //Sum of squares of buffer sizes of objects written so far
00079 
00080    static Long_t fgMapAddress;  //Map to this address, set address via SetMapAddress()
00081    static void  *fgMmallocDesc; //Used in Close() and operator delete()
00082 
00083 protected:
00084    TMapFile();
00085    TMapFile(const char *name, const char *title, Option_t *option, Int_t size, TMapFile *&newMapFile);
00086    TMapFile(const TMapFile &f, Long_t offset = 0);
00087    void       operator=(const TMapFile &rhs);  // not implemented
00088 
00089    TMapFile  *FindShadowMapFile();
00090    void       InitDirectory();
00091    TObject   *Remove(TObject *obj, Bool_t lock);
00092    TObject   *Remove(const char *name, Bool_t lock);
00093    void       SumBuffer(Int_t bufsize);
00094    Int_t      GetBestBuffer();
00095 
00096    void   CreateSemaphore(Int_t pid=0);
00097    Int_t  AcquireSemaphore();
00098    Int_t  ReleaseSemaphore();
00099    void   DeleteSemaphore();
00100 
00101    static void *MapToAddress();
00102 
00103 public:
00104    enum { kDefaultMapSize = 0x80000 }; // default size of mapped heap is 500 KB
00105 
00106    // Should both be protected (waiting for cint)
00107    virtual ~TMapFile();
00108    void     operator delete(void *vp);
00109 
00110    void          Browse(TBrowser *b);
00111    void          Close(Option_t *option = "");
00112    void         *GetBaseAddr() const { return (void *)fBaseAddr; }
00113    void         *GetBreakval() const;
00114    TDirectory   *GetDirectory() const {return fDirectory;}
00115    Int_t         GetFd() const { return fFd; }
00116    void         *GetMmallocDesc() const { return fMmallocDesc; }
00117    const char   *GetName() const { return fName; }
00118    Int_t         GetSize() const { return fSize; }
00119    const char   *GetOption() const { return fOption; }
00120    const char   *GetTitle() const { return fTitle; }
00121    TMapRec      *GetFirst() const { return (TMapRec*)((Long_t) fFirst + fOffset); }
00122    TMapRec      *GetLast() const { return (TMapRec*)((Long_t) fLast + fOffset); }
00123    Bool_t        IsFolder() const;
00124    Bool_t        IsWritable() const { return fWritable; }
00125    void         *OrgAddress(void *addr) const { return (void *)((Long_t)addr - fOffset); }
00126    void          Print(Option_t *option="") const;
00127    void          ls(Option_t *option="") const;
00128    Bool_t        cd(const char *path = 0);
00129 
00130    void          Add(const TObject *obj, const char *name = "");
00131    void          Update(TObject *obj = 0);
00132    TObject      *Remove(TObject *obj) { return Remove(obj, kTRUE); }
00133    TObject      *Remove(const char *name) { return Remove(name, kTRUE); }
00134    void          RemoveAll();
00135    TObject      *Get(const char *name, TObject *retObj = 0);
00136 
00137    static TMapFile *Create(const char *name, Option_t *option="READ", Int_t size=kDefaultMapSize, const char *title="");
00138    static TMapFile *WhichMapFile(void *addr);
00139    static void      SetMapAddress(Long_t addr);
00140 
00141    ClassDef(TMapFile,0)  // Memory mapped directory structure
00142 };
00143 
00144 
00145 
00146 //////////////////////////////////////////////////////////////////////////
00147 //                                                                      //
00148 // TMapRec                                                              //
00149 //                                                                      //
00150 // A TMapFile contains a list of TMapRec objects which keep track of    //
00151 // the actual objects stored in the mapped file.                        //
00152 //                                                                      //
00153 //////////////////////////////////////////////////////////////////////////
00154 
00155 class TMapRec {
00156 
00157 friend class TMapFile;
00158 
00159 private:
00160    char            *fName;       // object name
00161    char            *fClassName;  // class name
00162    TObject         *fObject;     // pointer to original object
00163    void            *fBuffer;     // buffer containing object of class name
00164    Int_t            fBufSize;    // buffer size
00165    TMapRec         *fNext;       // next MapRec in list
00166 
00167 public:
00168    TMapRec(const char *name, const TObject *obj, Int_t size, void *buf);
00169    ~TMapRec();
00170    const char   *GetName(Long_t offset = 0) const { return (char *)((Long_t) fName + offset); }
00171    const char   *GetClassName(Long_t offset = 0) const { return (char *)((Long_t) fClassName + offset); }
00172    void         *GetBuffer(Long_t offset = 0) const { return (void *)((Long_t) fBuffer + offset); }
00173    Int_t         GetBufSize() const { return fBufSize; }
00174    TObject      *GetObject() const;
00175    TMapRec      *GetNext(Long_t offset = 0) const { return (TMapRec *)((Long_t) fNext + offset); }
00176 };
00177 
00178 
00179 //______________________________________________________________________________
00180 inline void *TMapFile::GetBreakval() const
00181 {
00182    // Return the current location in the memory region for this malloc heap which
00183    // represents the end of memory in use. Returns 0 if map file was closed.
00184 
00185    if (!fMmallocDesc) return 0;
00186    return (void *)((struct mdesc *)fMmallocDesc)->breakval;
00187 }
00188 
00189 //______________________________________________________________________________
00190 inline TMapFile *TMapFile::WhichMapFile(void *addr)
00191 {
00192    if (!gROOT || !gROOT->GetListOfMappedFiles()) return 0;
00193 
00194    TObjLink *lnk = ((TList *)gROOT->GetListOfMappedFiles())->LastLink();
00195    while (lnk) {
00196       TMapFile *mf = (TMapFile*)lnk->GetObject();
00197       if (!mf) return 0;
00198       if ((ULong_t)addr >= mf->fBaseAddr + mf->fOffset &&
00199           (ULong_t)addr <  (ULong_t)mf->GetBreakval() + mf->fOffset)
00200          return mf;
00201       lnk = lnk->Prev();
00202    }
00203    return 0;
00204 }
00205 
00206 R__EXTERN void *gMmallocDesc;  //is initialized in TClass.cxx
00207 
00208 #endif

Generated on Tue Jul 5 14:27:01 2011 for ROOT_528-00b_version by  doxygen 1.5.1