Back to home page

EIC code displayed by LXR

 
 

    


Warning, file /include/root/TMemFile.h was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 // @(#)root/io:$Id$
0002 // Author: Philippe Canal, May 2011
0003 
0004 /*************************************************************************
0005  * Copyright (C) 1995-2009, Rene Brun and Fons Rademakers.               *
0006  * All rights reserved.                                                  *
0007  *                                                                       *
0008  * For the licensing terms see $ROOTSYS/LICENSE.                         *
0009  * For the list of contributors see $ROOTSYS/README/CREDITS.             *
0010  *************************************************************************/
0011 
0012 #ifndef ROOT_TMemFile
0013 #define ROOT_TMemFile
0014 
0015 #include "TFile.h"
0016 #include <vector>
0017 #include <memory>
0018 
0019 class TMemFile : public TFile {
0020 public:
0021    using ExternalDataPtr_t = std::shared_ptr<const std::vector<char>>;
0022    /// A read-only memory range which we do not control.
0023    struct ZeroCopyView_t {
0024       const char *fStart;
0025       const size_t fSize;
0026       explicit ZeroCopyView_t(const char * start, const size_t size) : fStart(start), fSize(size) {}
0027    };
0028 
0029 protected:
0030    struct TMemBlock {
0031    private:
0032       TMemBlock(const TMemBlock&) = delete;            // Not implemented
0033       TMemBlock &operator=(const TMemBlock&) = delete; // Not implemented.
0034    public:
0035       TMemBlock() = default;
0036       TMemBlock(Long64_t size, TMemBlock *previous = nullptr);
0037       TMemBlock(UChar_t* externalBuffer, Long64_t size);
0038       ~TMemBlock();
0039 
0040       void CreateNext(Long64_t size);
0041 
0042       TMemBlock *fPrevious{nullptr};
0043       TMemBlock *fNext{nullptr};
0044       UChar_t   *fBuffer{nullptr};
0045       Long64_t   fSize{0};
0046    };
0047    TMemBlock    fBlockList;               ///< Collection of memory blocks of size fgDefaultBlockSize
0048    ExternalDataPtr_t fExternalData;       ///< shared file data / content
0049    Bool_t       fIsOwnedByROOT{kFALSE};   ///< if this is a C-style memory region
0050    Long64_t     fSize{0};                 ///< Total file size (sum of the size of the chunks)
0051    Long64_t     fSysOffset{0};            ///< Seek offset in file
0052    TMemBlock   *fBlockSeek{nullptr};      ///< Pointer to the block we seeked to.
0053    Long64_t     fBlockOffset{0};          ///< Seek offset within the block
0054 
0055    constexpr static Long64_t fgDefaultBlockSize = 2 * 1024 * 1024;
0056    Long64_t fDefaultBlockSize = fgDefaultBlockSize;
0057 
0058    Bool_t IsExternalData() const { return !fIsOwnedByROOT; }
0059 
0060    Long64_t MemRead(Int_t fd, void *buf, Long64_t len) const;
0061 
0062    // Overload TFile interfaces.
0063    Int_t    SysOpen(const char *pathname, Int_t flags, UInt_t mode) override;
0064    Int_t    SysClose(Int_t fd) override;
0065    Int_t    SysReadImpl(Int_t fd, void *buf, Long64_t len);
0066    Int_t    SysWriteImpl(Int_t fd, const void *buf, Long64_t len);
0067    Int_t    SysRead(Int_t fd, void *buf, Int_t len) override;
0068    Int_t    SysWrite(Int_t fd, const void *buf, Int_t len) override;
0069    Long64_t SysSeek(Int_t fd, Long64_t offset, Int_t whence) override;
0070    Int_t    SysStat(Int_t fd, Long_t *id, Long64_t *size, Long_t *flags, Long_t *modtime) override;
0071    Int_t    SysSync(Int_t fd) override;
0072 
0073    void ResetObjects(TDirectoryFile *, TFileMergeInfo *) const;
0074 
0075    enum class EMode {
0076       kCreate,
0077       kRecreate,
0078       kUpdate,
0079       kRead
0080    };
0081 
0082    bool NeedsToWrite(EMode mode) const { return mode != EMode::kRead; }
0083    bool NeedsExistingFile(EMode mode) const { return mode == EMode::kUpdate || mode == EMode::kRead; }
0084 
0085    EMode ParseOption(Option_t *option);
0086 
0087    TMemFile &operator=(const TMemFile&) = delete; // Not implemented.
0088 
0089 public:
0090    TMemFile(const char *name, Option_t *option = "", const char *ftitle = "",
0091             Int_t compress = ROOT::RCompressionSetting::EDefaults::kUseCompiledDefault, Long64_t defBlockSize = 0LL);
0092    TMemFile(const char *name, char *buffer, Long64_t size, Option_t *option = "", const char *ftitle = "",
0093             Int_t compress = ROOT::RCompressionSetting::EDefaults::kUseCompiledDefault, Long64_t defBlockSize = 0LL);
0094    TMemFile(const char *name, ExternalDataPtr_t data);
0095    TMemFile(const char *name, const ZeroCopyView_t &datarange);
0096    TMemFile(const char *name, std::unique_ptr<TBufferFile> buffer);
0097    TMemFile(const TMemFile &orig);
0098    ~TMemFile() override;
0099 
0100    virtual Long64_t CopyTo(void *to, Long64_t maxsize) const;
0101    virtual void     CopyTo(TBuffer &tobuf) const;
0102            Long64_t GetSize() const override;
0103 
0104            void ResetAfterMerge(TFileMergeInfo *) override;
0105            void ResetErrno() const override;
0106 
0107            void        Print(Option_t *option="") const override;
0108 
0109    ClassDefOverride(TMemFile, 0) // A ROOT file that reads/writes on a chunk of memory
0110 };
0111 
0112 #endif