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;
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;
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
85 enum class EMode {
86 kCreate,
88 kUpdate,
89 kRead
90 };
91
92 bool NeedsToWrite(EMode mode) const { return mode != EMode::kRead; }
94
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
unsigned char UChar_t
Unsigned Character 1 byte (unsigned char)
Definition RtypesCore.h:52
long Long_t
Signed long integer 4 bytes (long). Size depends on architecture.
Definition RtypesCore.h:68
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
Option_t Option_t option
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void data
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h offset
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h Atom_t Int_t ULong_t ULong_t unsigned char prop_list Atom_t Atom_t Atom_t Time_t UChar_t len
Option_t Option_t TPoint TPoint const char mode
char name[80]
Definition TGX11.cxx:110
Buffer base class used for serializing objects.
Definition TBuffer.h:43
A ROOT file is structured in Directories (like a file system).
A ROOT file is an on-disk file, usually with extension .root, that stores objects in a file-system-li...
Definition TFile.h:131
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:586
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:675
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:683
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:501
Int_t SysClose(Int_t fd) override
Close the mem file.
Definition TMemFile.cxx:603
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:611
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:701
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:693
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:510
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