LLVM  8.0.0svn
FileOutputBuffer.h
Go to the documentation of this file.
1 //=== FileOutputBuffer.h - File Output Buffer -------------------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // Utility for creating a in-memory buffer that will be written to a file.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_SUPPORT_FILEOUTPUTBUFFER_H
15 #define LLVM_SUPPORT_FILEOUTPUTBUFFER_H
16 
17 #include "llvm/ADT/SmallString.h"
18 #include "llvm/ADT/StringRef.h"
19 #include "llvm/Support/DataTypes.h"
20 #include "llvm/Support/Error.h"
22 
23 namespace llvm {
24 /// FileOutputBuffer - This interface provides simple way to create an in-memory
25 /// buffer which will be written to a file. During the lifetime of these
26 /// objects, the content or existence of the specified file is undefined. That
27 /// is, creating an OutputBuffer for a file may immediately remove the file.
28 /// If the FileOutputBuffer is committed, the target file's content will become
29 /// the buffer content at the time of the commit. If the FileOutputBuffer is
30 /// not committed, the file will be deleted in the FileOutputBuffer destructor.
32 public:
33  enum {
34  /// set the 'x' bit on the resulting file
36 
37  /// the contents of the new file are initialized from the file that exists
38  /// at the location (if present). This allows in-place modification of an
39  /// existing file.
41  };
42 
43  /// Factory method to create an OutputBuffer object which manages a read/write
44  /// buffer of the specified size. When committed, the buffer will be written
45  /// to the file at the specified path.
46  ///
47  /// When F_modify is specified and \p FilePath refers to an existing on-disk
48  /// file \p Size may be set to -1, in which case the entire file is used.
49  /// Otherwise, the file shrinks or grows as necessary based on the value of
50  /// \p Size. It is an error to specify F_modify and Size=-1 if \p FilePath
51  /// does not exist.
53  create(StringRef FilePath, size_t Size, unsigned Flags = 0);
54 
55  /// Returns a pointer to the start of the buffer.
56  virtual uint8_t *getBufferStart() const = 0;
57 
58  /// Returns a pointer to the end of the buffer.
59  virtual uint8_t *getBufferEnd() const = 0;
60 
61  /// Returns size of the buffer.
62  virtual size_t getBufferSize() const = 0;
63 
64  /// Returns path where file will show up if buffer is committed.
65  StringRef getPath() const { return FinalPath; }
66 
67  /// Flushes the content of the buffer to its file and deallocates the
68  /// buffer. If commit() is not called before this object's destructor
69  /// is called, the file is deleted in the destructor. The optional parameter
70  /// is used if it turns out you want the file size to be smaller than
71  /// initially requested.
72  virtual Error commit() = 0;
73 
74  /// If this object was previously committed, the destructor just deletes
75  /// this object. If this object was not committed, the destructor
76  /// deallocates the buffer and the target file is never written.
77  virtual ~FileOutputBuffer() {}
78 
79  /// This removes the temporary file (unless it already was committed)
80  /// but keeps the memory mapping alive.
81  virtual void discard() {}
82 
83 protected:
85 
86  std::string FinalPath;
87 };
88 } // end namespace llvm
89 
90 #endif
virtual ~FileOutputBuffer()
If this object was previously committed, the destructor just deletes this object. ...
virtual uint8_t * getBufferEnd() const =0
Returns a pointer to the end of the buffer.
static Expected< std::unique_ptr< FileOutputBuffer > > create(StringRef FilePath, size_t Size, unsigned Flags=0)
Factory method to create an OutputBuffer object which manages a read/write buffer of the specified si...
This class represents lattice values for constants.
Definition: AllocatorList.h:24
StringRef getPath() const
Returns path where file will show up if buffer is committed.
FileOutputBuffer(StringRef Path)
virtual uint8_t * getBufferStart() const =0
Returns a pointer to the start of the buffer.
the contents of the new file are initialized from the file that exists at the location (if present)...
FileOutputBuffer - This interface provides simple way to create an in-memory buffer which will be wri...
Tagged union holding either a T or a Error.
Definition: CachePruning.h:23
set the &#39;x&#39; bit on the resulting file
virtual size_t getBufferSize() const =0
Returns size of the buffer.
uint32_t Size
Definition: Profile.cpp:47
virtual void discard()
This removes the temporary file (unless it already was committed) but keeps the memory mapping alive...
Lightweight error class with error context and mandatory checking.
Definition: Error.h:158
virtual Error commit()=0
Flushes the content of the buffer to its file and deallocates the buffer.
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49