Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-09-17 09:30:49

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;
0020 
0021 namespace ROOT::Internal {
0022 
0023 void DumpBin(const TMemFile &file, FILE *out);
0024 
0025 }
0026 
0027 class TMemFile : public TFile {
0028    friend void ROOT::Internal::DumpBin(const TMemFile &file, FILE *out);
0029    
0030 public:
0031    using ExternalDataPtr_t = std::shared_ptr<const std::vector<char>>;
0032    /// A read-only memory range which we do not control.
0033    struct ZeroCopyView_t {
0034       const char *fStart;
0035       const size_t fSize;
0036       explicit ZeroCopyView_t(const char * start, const size_t size) : fStart(start), fSize(size) {}
0037    };
0038 
0039 protected:
0040    struct TMemBlock {
0041    private:
0042       TMemBlock(const TMemBlock&) = delete;            // Not implemented
0043       TMemBlock &operator=(const TMemBlock&) = delete; // Not implemented.
0044    public:
0045       TMemBlock() = default;
0046       TMemBlock(Long64_t size, TMemBlock *previous = nullptr);
0047       TMemBlock(UChar_t* externalBuffer, Long64_t size);
0048       ~TMemBlock();
0049 
0050       void CreateNext(Long64_t size);
0051 
0052       TMemBlock *fPrevious{nullptr};
0053       TMemBlock *fNext{nullptr};
0054       UChar_t   *fBuffer{nullptr};
0055       Long64_t   fSize{0};
0056    };
0057    TMemBlock    fBlockList;               ///< Collection of memory blocks of size fgDefaultBlockSize
0058    ExternalDataPtr_t fExternalData;       ///< shared file data / content
0059    Bool_t       fIsOwnedByROOT{kFALSE};   ///< if this is a C-style memory region
0060    Long64_t     fSize{0};                 ///< Total file size (sum of the size of the chunks)
0061    Long64_t     fSysOffset{0};            ///< Seek offset in file
0062    TMemBlock   *fBlockSeek{nullptr};      ///< Pointer to the block we seeked to.
0063    Long64_t     fBlockOffset{0};          ///< Seek offset within the block
0064 
0065    constexpr static Long64_t fgDefaultBlockSize = 2 * 1024 * 1024;
0066    Long64_t fDefaultBlockSize = fgDefaultBlockSize;
0067 
0068    Bool_t IsExternalData() const { return !fIsOwnedByROOT; }
0069 
0070    Long64_t MemRead(Int_t fd, void *buf, Long64_t len) const;
0071 
0072    // Overload TFile interfaces.
0073    Int_t    SysOpen(const char *pathname, Int_t flags, UInt_t mode) override;
0074    Int_t    SysClose(Int_t fd) override;
0075    Int_t    SysReadImpl(Int_t fd, void *buf, Long64_t len);
0076    Int_t    SysWriteImpl(Int_t fd, const void *buf, Long64_t len);
0077    Int_t    SysRead(Int_t fd, void *buf, Int_t len) override;
0078    Int_t    SysWrite(Int_t fd, const void *buf, Int_t len) override;
0079    Long64_t SysSeek(Int_t fd, Long64_t offset, Int_t whence) override;
0080    Int_t    SysStat(Int_t fd, Long_t *id, Long64_t *size, Long_t *flags, Long_t *modtime) override;
0081    Int_t    SysSync(Int_t fd) override;
0082 
0083    void ResetObjects(TDirectoryFile *, TFileMergeInfo *) const;
0084 
0085    enum class EMode {
0086       kCreate,
0087       kRecreate,
0088       kUpdate,
0089       kRead
0090    };
0091 
0092    bool NeedsToWrite(EMode mode) const { return mode != EMode::kRead; }
0093    bool NeedsExistingFile(EMode mode) const { return mode == EMode::kUpdate || mode == EMode::kRead; }
0094 
0095    EMode ParseOption(Option_t *option);
0096 
0097    TMemFile &operator=(const TMemFile&) = delete; // Not implemented.
0098 
0099 public:
0100    TMemFile(const char *name, Option_t *option = "", const char *ftitle = "",
0101             Int_t compress = ROOT::RCompressionSetting::EDefaults::kUseCompiledDefault, Long64_t defBlockSize = 0LL);
0102    TMemFile(const char *name, char *buffer, Long64_t size, Option_t *option = "", const char *ftitle = "",
0103             Int_t compress = ROOT::RCompressionSetting::EDefaults::kUseCompiledDefault, Long64_t defBlockSize = 0LL);
0104    TMemFile(const char *name, ExternalDataPtr_t data);
0105    TMemFile(const char *name, const ZeroCopyView_t &datarange);
0106    TMemFile(const char *name, std::unique_ptr<TBufferFile> buffer);
0107    TMemFile(const TMemFile &orig);
0108    ~TMemFile() override;
0109 
0110    virtual Long64_t CopyTo(void *to, Long64_t maxsize) const;
0111    virtual void     CopyTo(TBuffer &tobuf) const;
0112            Long64_t GetSize() const override;
0113 
0114            void ResetAfterMerge(TFileMergeInfo *) override;
0115            void ResetErrno() const override;
0116 
0117            void        Print(Option_t *option="") const override;
0118 
0119    ClassDefOverride(TMemFile, 0) // A ROOT file that reads/writes on a chunk of memory
0120 };
0121 
0122 #endif