Logo ROOT   6.18/05
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
23private:
24 struct TMemBlock {
25 private:
26 TMemBlock(const TMemBlock&); // Not implemented
27 TMemBlock &operator=(const TMemBlock&); // Not implemented.
28 public:
29 TMemBlock();
30 TMemBlock(Long64_t size, TMemBlock *previous = 0);
31 TMemBlock(UChar_t* externalBuffer, Long64_t size);
32 ~TMemBlock();
33
34 void CreateNext(Long64_t size);
35
40 };
41 TMemBlock fBlockList; ///< Collection of memory blocks of size fgDefaultBlockSize
42 const ExternalDataPtr_t fExternalData; ///< shared file data / content
43 Long64_t fSize; ///< Total file size (sum of the size of the chunks)
44 Long64_t fSysOffset; ///< Seek offset in file
45 TMemBlock *fBlockSeek; ///< Pointer to the block we seeked to.
46 Long64_t fBlockOffset; ///< Seek offset within the block
47
48 constexpr static Long64_t fgDefaultBlockSize = 2 * 1024 * 1024;
50
51 Long64_t MemRead(Int_t fd, void *buf, Long64_t len) const;
52
53 // Overload TFile interfaces.
54 Int_t SysOpen(const char *pathname, Int_t flags, UInt_t mode);
56 Int_t SysReadImpl(Int_t fd, void *buf, Long64_t len);
57 Int_t SysWriteImpl(Int_t fd, const void *buf, Long64_t len);
58 Int_t SysRead(Int_t fd, void *buf, Int_t len);
59 Int_t SysWrite(Int_t fd, const void *buf, Int_t len);
60 Long64_t SysSeek(Int_t fd, Long64_t offset, Int_t whence);
61 Int_t SysStat(Int_t fd, Long_t *id, Long64_t *size, Long_t *flags, Long_t *modtime);
63
65
66 enum class EMode {
67 kCreate,
68 kRecreate,
69 kUpdate,
70 kRead
71 };
72
73 bool NeedsToWrite(EMode mode) const { return mode != EMode::kRead; }
74 bool NeedsExistingFile(EMode mode) const { return mode == EMode::kUpdate || mode == EMode::kRead; }
75
76 EMode ParseOption(Option_t *option);
77
78 TMemFile &operator=(const TMemFile&); // Not implemented.
79
80public:
81 TMemFile(const char *name, Option_t *option = "", const char *ftitle = "",
83 TMemFile(const char *name, char *buffer, Long64_t size, Option_t *option = "", const char *ftitle = "",
85 TMemFile(const char *name, ExternalDataPtr_t data);
86 TMemFile(const char *name, std::unique_ptr<TBufferFile> buffer);
87 TMemFile(const TMemFile &orig);
88 virtual ~TMemFile();
89
90 virtual Long64_t CopyTo(void *to, Long64_t maxsize) const;
91 virtual void CopyTo(TBuffer &tobuf) const;
92 virtual Long64_t GetSize() const;
93
95 void ResetErrno() const;
96
97 virtual void Print(Option_t *option="") const;
98
99 ClassDef(TMemFile, 0) // A ROOT file that reads/writes on a chunk of memory
100};
101
102#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
long Long_t
Definition: RtypesCore.h:50
long long Long64_t
Definition: RtypesCore.h:69
const char Option_t
Definition: RtypesCore.h:62
#define ClassDef(name, id)
Definition: Rtypes.h:326
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
const ExternalDataPtr_t fExternalData
shared file data / content
Definition: TMemFile.h:42
bool NeedsExistingFile(EMode mode) const
Definition: TMemFile.h:74
TMemFile & operator=(const TMemFile &)
Long64_t fDefaultBlockSize
Definition: TMemFile.h:49
Int_t SysRead(Int_t fd, void *buf, Int_t len)
Read specified number of bytes from current offset into the buffer.
Definition: TMemFile.cxx:498
Int_t SysStat(Int_t fd, Long_t *id, Long64_t *size, Long_t *flags, Long_t *modtime)
Perform a stat on the file; see TFile::SysStat().
Definition: TMemFile.cxx:680
Int_t SysClose(Int_t fd)
Close the mem file.
Definition: TMemFile.cxx:600
Long64_t fBlockOffset
Seek offset within the block.
Definition: TMemFile.h:46
EMode ParseOption(Option_t *option)
Parse option strings and set fOption.
Definition: TMemFile.cxx:95
TMemBlock * fBlockSeek
Pointer to the block we seeked to.
Definition: TMemFile.h:45
static constexpr Long64_t fgDefaultBlockSize
Definition: TMemFile.h:48
Long64_t fSize
Total file size (sum of the size of the chunks)
Definition: TMemFile.h:43
TMemBlock fBlockList
Collection of memory blocks of size fgDefaultBlockSize.
Definition: TMemFile.h:41
Long64_t MemRead(Int_t fd, void *buf, Long64_t len) const
std::shared_ptr< const std::vector< char > > ExternalDataPtr_t
Definition: TMemFile.h:21
Long64_t fSysOffset
Seek offset in file.
Definition: TMemFile.h:44
bool NeedsToWrite(EMode mode) const
Definition: TMemFile.h:73
Int_t SysSync(Int_t fd)
Sync remaining data to disk.
Definition: TMemFile.cxx:690
Int_t SysWriteImpl(Int_t fd, const void *buf, Long64_t len)
Write a buffer into the file.
Definition: TMemFile.cxx:608
Int_t SysWrite(Int_t fd, const void *buf, Int_t len)
Write a buffer into the file.
Definition: TMemFile.cxx:672
virtual void Print(Option_t *option="") const
Print all objects in the file.
Definition: TMemFile.cxx:308
void ResetErrno() const
Simply calls TSystem::ResetErrno().
Definition: TMemFile.cxx:698
Int_t SysOpen(const char *pathname, Int_t flags, UInt_t mode)
Open a file in 'MemFile'.
Definition: TMemFile.cxx:583
virtual Long64_t GetSize() const
Return the current size of the memory file.
Definition: TMemFile.cxx:300
void ResetObjects(TDirectoryFile *, TFileMergeInfo *) const
Wipe all the data from the permanent buffer but keep, the in-memory object alive.
Definition: TMemFile.cxx:397
void ResetAfterMerge(TFileMergeInfo *)
Wipe all the data from the permanent buffer but keep, the in-memory object alive.
Definition: TMemFile.cxx:330
virtual ~TMemFile()
Close and clean-up file.
Definition: TMemFile.cxx:246
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:440
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:265
Long64_t SysSeek(Int_t fd, Long64_t offset, Int_t whence)
Seek to a specified position in the file.
Definition: TMemFile.cxx:507
TMemFile(const char *name, Option_t *option="", const char *ftitle="", Int_t compress=ROOT::RCompressionSetting::EDefaults::kUseGeneralPurpose, Long64_t defBlockSize=0LL)
Usual Constructor.
Definition: TMemFile.cxx:171
@ kUseGeneralPurpose
Use the recommended general-purpose setting; moderate read / write speed and compression ratio.
Definition: Compression.h:53
UChar_t * fBuffer
Definition: TMemFile.h:38
~TMemBlock()
Usual destructors. Delete the block memory.
Definition: TMemFile.cxx:79
TMemBlock * fNext
Definition: TMemFile.h:37
TMemBlock(const TMemBlock &)
void CreateNext(Long64_t size)
Definition: TMemFile.cxx:87
TMemBlock()
Default constructor.
Definition: TMemFile.cxx:47
TMemBlock * fPrevious
Definition: TMemFile.h:36
TMemBlock & operator=(const TMemBlock &)
Long64_t fSize
Definition: TMemFile.h:39