Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
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};
45  Long64_t fSize{0};
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;
56  Long64_t fDefaultBlockSize = fgDefaultBlockSize;
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 
73  void ResetObjects(TDirectoryFile *, TFileMergeInfo *) const;
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 = "",
91  Int_t compress = ROOT::RCompressionSetting::EDefaults::kUseCompiledDefault, Long64_t defBlockSize = 0LL);
92  TMemFile(const char *name, char *buffer, Long64_t size, Option_t *option = "", const char *ftitle = "",
93  Int_t compress = ROOT::RCompressionSetting::EDefaults::kUseCompiledDefault, Long64_t defBlockSize = 0LL);
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