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
19class TMemFile : public TFile {
20public:
21 using ExternalDataPtr_t = std::shared_ptr<const std::vector<char>>;
22 /// A read-only memory range which we do not control.
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
29protected:
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
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
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
89public:
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
void tobuf(char *&buf, Bool_t x)
Definition: Bytes.h:57
int Int_t
Definition: RtypesCore.h:41
unsigned char UChar_t
Definition: RtypesCore.h:34
unsigned int UInt_t
Definition: RtypesCore.h:42
const Bool_t kFALSE
Definition: RtypesCore.h:88
long Long_t
Definition: RtypesCore.h:50
bool Bool_t
Definition: RtypesCore.h:59
long long Long64_t
Definition: RtypesCore.h:69
const char Option_t
Definition: RtypesCore.h:62
#define ClassDefOverride(name, id)
Definition: Rtypes.h:330
char name[80]
Definition: TGX11.cxx:109
Buffer base class used for serializing objects.
Definition: TBuffer.h:42
A ROOT file is structured in Directories (like a file system).
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format.
Definition: TFile.h:48
A TMemFile is like a normal TFile except that it reads and writes only from memory.
Definition: TMemFile.h:19
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:572
ExternalDataPtr_t fExternalData
shared file data / content
Definition: TMemFile.h:48
bool NeedsExistingFile(EMode mode) const
Definition: TMemFile.h:83
Long64_t fDefaultBlockSize
Definition: TMemFile.h:56
Bool_t fIsOwnedByROOT
if this is a C-style memory region
Definition: TMemFile.h:49
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:53
Int_t SysWrite(Int_t fd, const void *buf, Int_t len) override
Write a buffer into the file.
Definition: TMemFile.cxx:661
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:52
static constexpr Long64_t fgDefaultBlockSize
Definition: TMemFile.h:55
Long64_t fSize
Total file size (sum of the size of the chunks)
Definition: TMemFile.h:50
TMemBlock fBlockList
Collection of memory blocks of size fgDefaultBlockSize.
Definition: TMemFile.h:47
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:669
std::shared_ptr< const std::vector< char > > ExternalDataPtr_t
Definition: TMemFile.h:21
Long64_t fSysOffset
Seek offset in file.
Definition: TMemFile.h:51
Bool_t IsExternalData() const
Definition: TMemFile.h:58
bool NeedsToWrite(EMode mode) const
Definition: TMemFile.h:82
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:487
Int_t SysClose(Int_t fd) override
Close the mem file.
Definition: TMemFile.cxx:589
Int_t SysWriteImpl(Int_t fd, const void *buf, Long64_t len)
Write a buffer into the file.
Definition: TMemFile.cxx:597
void ResetObjects(TDirectoryFile *, TFileMergeInfo *) const
Wipe all the data from the permanent buffer but keep, the in-memory object alive.
Definition: TMemFile.cxx:386
void ResetErrno() const override
Simply calls TSystem::ResetErrno().
Definition: TMemFile.cxx:687
virtual ~TMemFile()
Close and clean-up file.
Definition: TMemFile.cxx:235
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:429
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:679
void ResetAfterMerge(TFileMergeInfo *) override
Wipe all the data from the permanent buffer but keep, the in-memory object alive.
Definition: TMemFile.cxx:319
Long64_t SysSeek(Int_t fd, Long64_t offset, Int_t whence) override
Seek to a specified position in the file.
Definition: TMemFile.cxx:496
@ kUseCompiledDefault
Use the compile-time default setting.
Definition: Compression.h:50
UChar_t * fBuffer
Definition: TMemFile.h:44
~TMemBlock()
Usual destructors. Delete the block memory.
Definition: TMemFile.cxx:71
TMemBlock * fNext
Definition: TMemFile.h:43
void CreateNext(Long64_t size)
Definition: TMemFile.cxx:79
TMemBlock * fPrevious
Definition: TMemFile.h:42
TMemBlock & operator=(const TMemBlock &)=delete
TMemBlock(const TMemBlock &)=delete
Long64_t fSize
Definition: TMemFile.h:45
A read-only memory range which we do not control.
Definition: TMemFile.h:23
const char * fStart
Definition: TMemFile.h:24
const size_t fSize
Definition: TMemFile.h:25
ZeroCopyView_t(const char *start, const size_t size)
Definition: TMemFile.h:26