Logo ROOT  
Reference Guide
Loading...
Searching...
No Matches
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
19class TMemFile;
20
21namespace ROOT::Internal {
22
23void DumpBin(const TMemFile &file, FILE *out);
24
25}
26
27class TMemFile : public TFile {
28 friend void ROOT::Internal::DumpBin(const TMemFile &file, FILE *out);
29
30public:
31 using ExternalDataPtr_t = std::shared_ptr<const std::vector<char>>;
32 /// A read-only memory range which we do not control.
34 const char *fStart;
35 const size_t fSize;
36 explicit ZeroCopyView_t(const char * start, const size_t size) : fStart(start), fSize(size) {}
37 };
38
39protected:
40 struct TMemBlock {
41 private:
42 TMemBlock(const TMemBlock&) = delete; // Not implemented
43 TMemBlock &operator=(const TMemBlock&) = delete; // Not implemented.
44 public:
45 TMemBlock() = default;
46 TMemBlock(Long64_t size, TMemBlock *previous = nullptr);
47 TMemBlock(UChar_t* externalBuffer, Long64_t size);
48 ~TMemBlock();
49
51
53 TMemBlock *fNext{nullptr};
54 UChar_t *fBuffer{nullptr};
56 };
57 TMemBlock fBlockList; ///< Collection of memory blocks of size fgDefaultBlockSize
58 ExternalDataPtr_t fExternalData; ///< shared file data / content
59 Bool_t fIsOwnedByROOT{kFALSE}; ///< if this is a C-style memory region
60 Long64_t fSize{0}; ///< Total file size (sum of the size of the chunks)
61 Long64_t fSysOffset{0}; ///< Seek offset in file
62 TMemBlock *fBlockSeek{nullptr}; ///< Pointer to the block we seeked to.
63 Long64_t fBlockOffset{0}; ///< Seek offset within the block
64
65 constexpr static Long64_t fgDefaultBlockSize = 2 * 1024 * 1024;
67
69
70 Long64_t MemRead(Int_t fd, void *buf, Long64_t len) const;
71
72 // Overload TFile interfaces.
73 Int_t SysOpen(const char *pathname, Int_t flags, UInt_t mode) override;
74 Int_t SysClose(Int_t fd) override;
75 Int_t SysReadImpl(Int_t fd, void *buf, Long64_t len);
76 Int_t SysWriteImpl(Int_t fd, const void *buf, Long64_t len);
77 Int_t SysRead(Int_t fd, void *buf, Int_t len) override;
78 Int_t SysWrite(Int_t fd, const void *buf, Int_t len) override;
79 Long64_t SysSeek(Int_t fd, Long64_t offset, Int_t whence) override;
80 Int_t SysStat(Int_t fd, Long_t *id, Long64_t *size, Long_t *flags, Long_t *modtime) override;
81 Int_t SysSync(Int_t fd) override;
82
84
91
92 bool NeedsToWrite(EMode mode) const { return mode != EMode::kRead; }
93 bool NeedsExistingFile(EMode mode) const { return mode == EMode::kUpdate || mode == EMode::kRead; }
94
95 EMode ParseOption(Option_t *option);
96
97 TMemFile &operator=(const TMemFile&) = delete; // Not implemented.
98
99public:
100 TMemFile(const char *name, Option_t *option = "", const char *ftitle = "",
102 TMemFile(const char *name, char *buffer, Long64_t size, Option_t *option = "", const char *ftitle = "",
104 TMemFile(const char *name, ExternalDataPtr_t data);
105 TMemFile(const char *name, const ZeroCopyView_t &datarange);
106 TMemFile(const char *name, std::unique_ptr<TBufferFile> buffer);
107 TMemFile(const TMemFile &orig);
108 ~TMemFile() override;
109
110 virtual Long64_t CopyTo(void *to, Long64_t maxsize) const;
111 virtual void CopyTo(TBuffer &tobuf) const;
112 Long64_t GetSize() const override;
113
114 void ResetAfterMerge(TFileMergeInfo *) override;
115 void ResetErrno() const override;
116
117 void Print(Option_t *option="") const override;
118
119 ClassDefOverride(TMemFile, 0) // A ROOT file that reads/writes on a chunk of memory
120};
121
122#endif
void tobuf(char *&buf, Bool_t x)
Definition Bytes.h:55
size_t size(const MatrixT &matrix)
retrieve the size of a square matrix
start
Definition Rotated.cxx:223
int Int_t
Signed integer 4 bytes (int).
Definition RtypesCore.h:59
unsigned char UChar_t
Unsigned Character 1 byte (unsigned char).
Definition RtypesCore.h:52
unsigned int UInt_t
Unsigned integer 4 bytes (unsigned int).
Definition RtypesCore.h:60
long Long_t
Signed long integer 4 bytes (long). Size depends on architecture.
Definition RtypesCore.h:68
bool Bool_t
Boolean (0=false, 1=true) (bool).
Definition RtypesCore.h:77
constexpr Bool_t kFALSE
Definition RtypesCore.h:108
long long Long64_t
Portable signed long integer 8 bytes.
Definition RtypesCore.h:83
const char Option_t
Option string (const char).
Definition RtypesCore.h:80
#define ClassDefOverride(name, id)
Definition Rtypes.h:348
char name[80]
Definition TGX11.cxx:148
Buffer base class used for serializing objects.
Definition TBuffer.h:43
A class to pass information from the TFileMerger to the objects being merged.
friend class TDirectoryFile
Definition TFile.h:131
TFile(const TFile &)=delete
A TMemFile is like a normal TFile except that it reads and writes only from memory.
Definition TMemFile.h:27
TMemFile & operator=(const TMemFile &)=delete
Int_t SysOpen(const char *pathname, Int_t flags, UInt_t mode) override
Open a file in 'MemFile'.
Definition TMemFile.cxx:588
ExternalDataPtr_t fExternalData
shared file data / content
Definition TMemFile.h:58
bool NeedsExistingFile(EMode mode) const
Definition TMemFile.h:93
Long64_t fDefaultBlockSize
Definition TMemFile.h:66
Bool_t fIsOwnedByROOT
if this is a C-style memory region
Definition TMemFile.h:59
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:161
Long64_t GetSize() const override
Return the current size of the memory file.
Definition TMemFile.cxx:289
Long64_t fBlockOffset
Seek offset within the block.
Definition TMemFile.h:63
Int_t SysWrite(Int_t fd, const void *buf, Int_t len) override
Write a buffer into the file.
Definition TMemFile.cxx:677
EMode ParseOption(Option_t *option)
Parse option strings and set fOption.
Definition TMemFile.cxx:87
TMemBlock * fBlockSeek
Pointer to the block we seeked to.
Definition TMemFile.h:62
static constexpr Long64_t fgDefaultBlockSize
Definition TMemFile.h:65
Long64_t fSize
Total file size (sum of the size of the chunks).
Definition TMemFile.h:60
TMemBlock fBlockList
Collection of memory blocks of size fgDefaultBlockSize.
Definition TMemFile.h:57
Long64_t MemRead(Int_t fd, void *buf, Long64_t len) const
void Print(Option_t *option="") const override
Print all objects in the file.
Definition TMemFile.cxx:297
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:685
Long64_t fSysOffset
Seek offset in file.
Definition TMemFile.h:61
Bool_t IsExternalData() const
Definition TMemFile.h:68
~TMemFile() override
Close and clean-up file.
Definition TMemFile.cxx:235
bool NeedsToWrite(EMode mode) const
Definition TMemFile.h:92
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:503
Int_t SysClose(Int_t fd) override
Close the mem file.
Definition TMemFile.cxx:605
std::shared_ptr< const std::vector< char > > ExternalDataPtr_t
Definition TMemFile.h:31
Int_t SysWriteImpl(Int_t fd, const void *buf, Long64_t len)
Write a buffer into the file.
Definition TMemFile.cxx:613
void ResetObjects(TDirectoryFile *, TFileMergeInfo *) const
Wipe all the data from the permanent buffer but keep, the in-memory object alive.
Definition TMemFile.cxx:400
void ResetErrno() const override
Simply calls TSystem::ResetErrno().
Definition TMemFile.cxx:703
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:443
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:254
Int_t SysSync(Int_t fd) override
Sync remaining data to disk.
Definition TMemFile.cxx:695
void ResetAfterMerge(TFileMergeInfo *) override
Wipe all the data from the permanent buffer but keep, the in-memory object alive.
Definition TMemFile.cxx:339
Long64_t SysSeek(Int_t fd, Long64_t offset, Int_t whence) override
Seek to a specified position in the file.
Definition TMemFile.cxx:512
void DumpBin(const TMemFile &file, FILE *out)
Writes the contents of the TMemFile to the given file.
Definition TMemFile.cxx:325
@ kUseCompiledDefault
Use the compile-time default setting.
Definition Compression.h:53
UChar_t * fBuffer
Definition TMemFile.h:54
~TMemBlock()
Usual destructors. Delete the block memory.
Definition TMemFile.cxx:71
TMemBlock * fNext
Definition TMemFile.h:53
void CreateNext(Long64_t size)
Definition TMemFile.cxx:79
TMemBlock * fPrevious
Definition TMemFile.h:52
TMemBlock & operator=(const TMemBlock &)=delete
TMemBlock(const TMemBlock &)=delete
A read-only memory range which we do not control.
Definition TMemFile.h:33
const char * fStart
Definition TMemFile.h:34
ZeroCopyView_t(const char *start, const size_t size)
Definition TMemFile.h:36