Logo ROOT  
Reference Guide
TMemFile.h
Go to the documentation of this file.
1 // @(#)root/io:$Id$
2 // Author: Philippe Canal, May 2011
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2009, 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_TMemFile
13 #define ROOT_TMemFile
14 
15 #include "TFile.h"
16 #include <vector>
17 #include <memory>
18 
19 class TMemFile : public TFile {
20 public:
21  using ExternalDataPtr_t = std::shared_ptr<const std::vector<char>>;
22  /// A read-only memory range which we do not control.
23  struct ZeroCopyView_t {
24  const char *fStart;
25  const size_t fSize;
26  explicit ZeroCopyView_t(const char * start, const size_t size) : fStart(start), fSize(size) {}
27  };
28 
29 protected:
30  struct TMemBlock {
31  private:
32  TMemBlock(const TMemBlock&) = delete; // Not implemented
33  TMemBlock &operator=(const TMemBlock&) = delete; // Not implemented.
34  public:
35  TMemBlock() = default;
36  TMemBlock(Long64_t size, TMemBlock *previous = nullptr);
37  TMemBlock(UChar_t* externalBuffer, Long64_t size);
38  ~TMemBlock();
39 
40  void CreateNext(Long64_t size);
41 
42  TMemBlock *fPrevious{nullptr};
43  TMemBlock *fNext{nullptr};
44  UChar_t *fBuffer{nullptr};
46  };
47  TMemBlock fBlockList; ///< Collection of memory blocks of size fgDefaultBlockSize
48  ExternalDataPtr_t fExternalData; ///< shared file data / content
49  Bool_t fIsOwnedByROOT{kFALSE}; ///< if this is a C-style memory region
50  Long64_t fSize{0}; ///< Total file size (sum of the size of the chunks)
51  Long64_t fSysOffset{0}; ///< Seek offset in file
52  TMemBlock *fBlockSeek{nullptr}; ///< Pointer to the block we seeked to.
53  Long64_t fBlockOffset{0}; ///< Seek offset within the block
54 
55  constexpr static Long64_t fgDefaultBlockSize = 2 * 1024 * 1024;
57 
58  Bool_t IsExternalData() const { return !fIsOwnedByROOT; }
59 
60  Long64_t MemRead(Int_t fd, void *buf, Long64_t len) const;
61 
62  // Overload TFile interfaces.
63  Int_t SysOpen(const char *pathname, Int_t flags, UInt_t mode) override;
64  Int_t SysClose(Int_t fd) override;
65  Int_t SysReadImpl(Int_t fd, void *buf, Long64_t len);
66  Int_t SysWriteImpl(Int_t fd, const void *buf, Long64_t len);
67  Int_t SysRead(Int_t fd, void *buf, Int_t len) override;
68  Int_t SysWrite(Int_t fd, const void *buf, Int_t len) override;
69  Long64_t SysSeek(Int_t fd, Long64_t offset, Int_t whence) override;
70  Int_t SysStat(Int_t fd, Long_t *id, Long64_t *size, Long_t *flags, Long_t *modtime) override;
71  Int_t SysSync(Int_t fd) override;
72 
74 
75  enum class EMode {
76  kCreate,
77  kRecreate,
78  kUpdate,
79  kRead
80  };
81 
82  bool NeedsToWrite(EMode mode) const { return mode != EMode::kRead; }
83  bool NeedsExistingFile(EMode mode) const { return mode == EMode::kUpdate || mode == EMode::kRead; }
84 
85  EMode ParseOption(Option_t *option);
86 
87  TMemFile &operator=(const TMemFile&) = delete; // Not implemented.
88 
89 public:
90  TMemFile(const char *name, Option_t *option = "", const char *ftitle = "",
92  TMemFile(const char *name, char *buffer, Long64_t size, Option_t *option = "", const char *ftitle = "",
94  TMemFile(const char *name, ExternalDataPtr_t data);
95  TMemFile(const char *name, const ZeroCopyView_t &datarange);
96  TMemFile(const char *name, std::unique_ptr<TBufferFile> buffer);
97  TMemFile(const TMemFile &orig);
98  virtual ~TMemFile();
99 
100  virtual Long64_t CopyTo(void *to, Long64_t maxsize) const;
101  virtual void CopyTo(TBuffer &tobuf) const;
102  Long64_t GetSize() const override;
103 
104  void ResetAfterMerge(TFileMergeInfo *) override;
105  void ResetErrno() const override;
106 
107  void Print(Option_t *option="") const override;
108 
109  ClassDefOverride(TMemFile, 0) // A ROOT file that reads/writes on a chunk of memory
110 };
111 
112 #endif
TMemFile::TMemBlock::TMemBlock
TMemBlock(const TMemBlock &)=delete
TFileMergeInfo
Definition: TFileMergeInfo.h:42
TMemFile::ZeroCopyView_t
A read-only memory range which we do not control.
Definition: TMemFile.h:23
TMemFile::fBlockList
TMemBlock fBlockList
Collection of memory blocks of size fgDefaultBlockSize.
Definition: TMemFile.h:47
TMemFile::EMode::kCreate
@ kCreate
TMemFile::fBlockSeek
TMemBlock * fBlockSeek
Pointer to the block we seeked to.
Definition: TMemFile.h:52
TMemFile::EMode::kRead
@ kRead
TMemFile::TMemBlock::fBuffer
UChar_t * fBuffer
Definition: TMemFile.h:44
TMemFile::TMemBlock::fPrevious
TMemBlock * fPrevious
Definition: TMemFile.h:42
TMemFile::fSysOffset
Long64_t fSysOffset
Seek offset in file.
Definition: TMemFile.h:51
Option_t
const char Option_t
Definition: RtypesCore.h:66
ROOT::RCompressionSetting::EDefaults::kUseCompiledDefault
@ kUseCompiledDefault
Use the compile-time default setting.
Definition: Compression.h:50
TMemFile::ResetAfterMerge
void ResetAfterMerge(TFileMergeInfo *) override
Wipe all the data from the permanent buffer but keep, the in-memory object alive.
Definition: TMemFile.cxx:320
tobuf
void tobuf(char *&buf, Bool_t x)
Definition: Bytes.h:55
Long64_t
long long Long64_t
Definition: RtypesCore.h:73
TMemFile
A TMemFile is like a normal TFile except that it reads and writes only from memory.
Definition: TMemFile.h:19
TMemFile::CopyTo
virtual Long64_t CopyTo(void *to, Long64_t maxsize) const
Copy the binary representation of the TMemFile into the memory area starting at 'to' and of length at...
Definition: TMemFile.cxx:255
TMemFile::fBlockOffset
Long64_t fBlockOffset
Seek offset within the block.
Definition: TMemFile.h:53
TBuffer
Buffer base class used for serializing objects.
Definition: TBuffer.h:43
ClassDefOverride
#define ClassDefOverride(name, id)
Definition: Rtypes.h:329
TMemFile::fDefaultBlockSize
Long64_t fDefaultBlockSize
Definition: TMemFile.h:56
TMemFile::fgDefaultBlockSize
constexpr static Long64_t fgDefaultBlockSize
Definition: TMemFile.h:55
TMemFile::operator=
TMemFile & operator=(const TMemFile &)=delete
TFile.h
bool
TMemFile::fExternalData
ExternalDataPtr_t fExternalData
shared file data / content
Definition: TMemFile.h:48
TMemFile::SysClose
Int_t SysClose(Int_t fd) override
Close the mem file.
Definition: TMemFile.cxx:590
TMemFile::ZeroCopyView_t::fStart
const char * fStart
Definition: TMemFile.h:24
TMemFile::TMemBlock::operator=
TMemBlock & operator=(const TMemBlock &)=delete
TMemFile::GetSize
Long64_t GetSize() const override
Return the current size of the memory file.
Definition: TMemFile.cxx:290
TMemFile::IsExternalData
Bool_t IsExternalData() const
Definition: TMemFile.h:58
TMemFile::EMode
EMode
Definition: TMemFile.h:75
TMemFile::fIsOwnedByROOT
Bool_t fIsOwnedByROOT
if this is a C-style memory region
Definition: TMemFile.h:49
TMemFile::SysWrite
Int_t SysWrite(Int_t fd, const void *buf, Int_t len) override
Write a buffer into the file.
Definition: TMemFile.cxx:662
TMemFile::SysRead
Int_t SysRead(Int_t fd, void *buf, Int_t len) override
Read specified number of bytes from current offset into the buffer.
Definition: TMemFile.cxx:488
TMemFile::SysReadImpl
Int_t SysReadImpl(Int_t fd, void *buf, Long64_t len)
Read specified number of bytes from current offset into the buffer.
Definition: TMemFile.cxx:430
TMemFile::TMemBlock::TMemBlock
TMemBlock()=default
TMemFile::SysWriteImpl
Int_t SysWriteImpl(Int_t fd, const void *buf, Long64_t len)
Write a buffer into the file.
Definition: TMemFile.cxx:598
kFALSE
const Bool_t kFALSE
Definition: RtypesCore.h:92
TMemFile::ExternalDataPtr_t
std::shared_ptr< const std::vector< char > > ExternalDataPtr_t
Definition: TMemFile.h:21
TMemFile::SysOpen
Int_t SysOpen(const char *pathname, Int_t flags, UInt_t mode) override
Open a file in 'MemFile'.
Definition: TMemFile.cxx:573
TMemFile::ZeroCopyView_t::ZeroCopyView_t
ZeroCopyView_t(const char *start, const size_t size)
Definition: TMemFile.h:26
Long_t
long Long_t
Definition: RtypesCore.h:54
TMemFile::~TMemFile
virtual ~TMemFile()
Close and clean-up file.
Definition: TMemFile.cxx:236
TMemFile::TMemBlock::CreateNext
void CreateNext(Long64_t size)
Definition: TMemFile.cxx:80
TMemFile::TMemBlock::fNext
TMemBlock * fNext
Definition: TMemFile.h:43
TMemFile::NeedsExistingFile
bool NeedsExistingFile(EMode mode) const
Definition: TMemFile.h:83
TFile
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format.
Definition: TFile.h:54
TMemFile::ParseOption
EMode ParseOption(Option_t *option)
Parse option strings and set fOption.
Definition: TMemFile.cxx:88
TMemFile::TMemBlock
Definition: TMemFile.h:30
unsigned int
TMemFile::ZeroCopyView_t::fSize
const size_t fSize
Definition: TMemFile.h:25
TMemFile::ResetErrno
void ResetErrno() const override
Simply calls TSystem::ResetErrno().
Definition: TMemFile.cxx:688
TMemFile::MemRead
Long64_t MemRead(Int_t fd, void *buf, Long64_t len) const
TMemFile::Print
void Print(Option_t *option="") const override
Print all objects in the file.
Definition: TMemFile.cxx:298
TMemFile::TMemFile
TMemFile(const char *name, Option_t *option="", const char *ftitle="", Int_t compress=ROOT::RCompressionSetting::EDefaults::kUseCompiledDefault, Long64_t defBlockSize=0LL)
Usual Constructor.
Definition: TMemFile.cxx:162
TMemFile::EMode::kRecreate
@ kRecreate
TMemFile::SysSync
Int_t SysSync(Int_t fd) override
Sync remaining data to disk.
Definition: TMemFile.cxx:680
TMemFile::NeedsToWrite
bool NeedsToWrite(EMode mode) const
Definition: TMemFile.h:82
UChar_t
unsigned char UChar_t
Definition: RtypesCore.h:38
TMemFile::EMode::kUpdate
@ kUpdate
TMemFile::TMemBlock::fSize
Long64_t fSize
Definition: TMemFile.h:45
name
char name[80]
Definition: TGX11.cxx:110
TMemFile::SysSeek
Long64_t SysSeek(Int_t fd, Long64_t offset, Int_t whence) override
Seek to a specified position in the file.
Definition: TMemFile.cxx:497
TMemFile::SysStat
Int_t SysStat(Int_t fd, Long_t *id, Long64_t *size, Long_t *flags, Long_t *modtime) override
Perform a stat on the file; see TFile::SysStat().
Definition: TMemFile.cxx:670
TMemFile::TMemBlock::~TMemBlock
~TMemBlock()
Usual destructors. Delete the block memory.
Definition: TMemFile.cxx:72
TDirectoryFile
A ROOT file is structured in Directories (like a file system).
Definition: TDirectoryFile.h:32
TMemFile::fSize
Long64_t fSize
Total file size (sum of the size of the chunks)
Definition: TMemFile.h:50
TMemFile::ResetObjects
void ResetObjects(TDirectoryFile *, TFileMergeInfo *) const
Wipe all the data from the permanent buffer but keep, the in-memory object alive.
Definition: TMemFile.cxx:387
int