LLVM  14.0.0git
FileOutputBuffer.h
Go to the documentation of this file.
1 //=== FileOutputBuffer.h - File Output Buffer -------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // Utility for creating a in-memory buffer that will be written to a file.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef LLVM_SUPPORT_FILEOUTPUTBUFFER_H
14 #define LLVM_SUPPORT_FILEOUTPUTBUFFER_H
15 
16 #include "llvm/ADT/StringRef.h"
17 #include "llvm/Support/DataTypes.h"
18 #include "llvm/Support/Error.h"
19 
20 namespace llvm {
21 /// FileOutputBuffer - This interface provides simple way to create an in-memory
22 /// buffer which will be written to a file. During the lifetime of these
23 /// objects, the content or existence of the specified file is undefined. That
24 /// is, creating an OutputBuffer for a file may immediately remove the file.
25 /// If the FileOutputBuffer is committed, the target file's content will become
26 /// the buffer content at the time of the commit. If the FileOutputBuffer is
27 /// not committed, the file will be deleted in the FileOutputBuffer destructor.
29 public:
30  enum {
31  /// Set the 'x' bit on the resulting file.
33 
34  /// Don't use mmap and instead write an in-memory buffer to a file when this
35  /// buffer is closed.
36  F_no_mmap = 2,
37  };
38 
39  /// Factory method to create an OutputBuffer object which manages a read/write
40  /// buffer of the specified size. When committed, the buffer will be written
41  /// to the file at the specified path.
42  ///
43  /// When F_modify is specified and \p FilePath refers to an existing on-disk
44  /// file \p Size may be set to -1, in which case the entire file is used.
45  /// Otherwise, the file shrinks or grows as necessary based on the value of
46  /// \p Size. It is an error to specify F_modify and Size=-1 if \p FilePath
47  /// does not exist.
49  create(StringRef FilePath, size_t Size, unsigned Flags = 0);
50 
51  /// Returns a pointer to the start of the buffer.
52  virtual uint8_t *getBufferStart() const = 0;
53 
54  /// Returns a pointer to the end of the buffer.
55  virtual uint8_t *getBufferEnd() const = 0;
56 
57  /// Returns size of the buffer.
58  virtual size_t getBufferSize() const = 0;
59 
60  /// Returns path where file will show up if buffer is committed.
61  StringRef getPath() const { return FinalPath; }
62 
63  /// Flushes the content of the buffer to its file and deallocates the
64  /// buffer. If commit() is not called before this object's destructor
65  /// is called, the file is deleted in the destructor. The optional parameter
66  /// is used if it turns out you want the file size to be smaller than
67  /// initially requested.
68  virtual Error commit() = 0;
69 
70  /// If this object was previously committed, the destructor just deletes
71  /// this object. If this object was not committed, the destructor
72  /// deallocates the buffer and the target file is never written.
73  virtual ~FileOutputBuffer() {}
74 
75  /// This removes the temporary file (unless it already was committed)
76  /// but keeps the memory mapping alive.
77  virtual void discard() {}
78 
79 protected:
81 
82  std::string FinalPath;
83 };
84 } // end namespace llvm
85 
86 #endif
llvm::Check::Size
@ Size
Definition: FileCheck.h:73
llvm
This file implements support for optimizing divisions by a constant.
Definition: AllocatorList.h:23
StringRef.h
Error.h
llvm::FileOutputBuffer::getBufferEnd
virtual uint8_t * getBufferEnd() const =0
Returns a pointer to the end of the buffer.
llvm::Expected
Tagged union holding either a T or a Error.
Definition: APFloat.h:42
llvm::FileOutputBuffer::F_executable
@ F_executable
Set the 'x' bit on the resulting file.
Definition: FileOutputBuffer.h:32
llvm::FileOutputBuffer::getPath
StringRef getPath() const
Returns path where file will show up if buffer is committed.
Definition: FileOutputBuffer.h:61
llvm::FileOutputBuffer::FileOutputBuffer
FileOutputBuffer(StringRef Path)
Definition: FileOutputBuffer.h:80
llvm::FileOutputBuffer::F_no_mmap
@ F_no_mmap
Don't use mmap and instead write an in-memory buffer to a file when this buffer is closed.
Definition: FileOutputBuffer.h:36
llvm::FileOutputBuffer
FileOutputBuffer - This interface provides simple way to create an in-memory buffer which will be wri...
Definition: FileOutputBuffer.h:28
llvm::FileOutputBuffer::FinalPath
std::string FinalPath
Definition: FileOutputBuffer.h:82
llvm::FileOutputBuffer::discard
virtual void discard()
This removes the temporary file (unless it already was committed) but keeps the memory mapping alive.
Definition: FileOutputBuffer.h:77
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
llvm::FileOutputBuffer::create
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...
Definition: FileOutputBuffer.cpp:158
llvm::Error
Lightweight error class with error context and mandatory checking.
Definition: Error.h:157
llvm::FileOutputBuffer::getBufferStart
virtual uint8_t * getBufferStart() const =0
Returns a pointer to the start of the buffer.
DataTypes.h
llvm::FileOutputBuffer::getBufferSize
virtual size_t getBufferSize() const =0
Returns size of the buffer.
llvm::FileOutputBuffer::commit
virtual Error commit()=0
Flushes the content of the buffer to its file and deallocates the buffer.
llvm::FileOutputBuffer::~FileOutputBuffer
virtual ~FileOutputBuffer()
If this object was previously committed, the destructor just deletes this object.
Definition: FileOutputBuffer.h:73