LLVM 24.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"
19#include "llvm/Support/Error.h"
20
21namespace llvm {
22/// FileOutputBuffer - This interface provides simple way to create an in-memory
23/// buffer which will be written to a file. During the lifetime of these
24/// objects, the content or existence of the specified file is undefined. That
25/// is, creating an OutputBuffer for a file may immediately remove the file.
26/// If the FileOutputBuffer is committed, the target file's content will become
27/// the buffer content at the time of the commit. If the FileOutputBuffer is
28/// not committed, the file will be deleted in the FileOutputBuffer destructor.
30public:
31 enum {
32 /// Set the 'x' bit on the resulting file.
34
35 /// Build the output in an anonymous memory buffer and write it to the file
36 /// on commit() instead of mapping the file.
38 };
39
40 /// Factory method to create an OutputBuffer object which manages a read/write
41 /// buffer of the specified size. When committed, the buffer will be written
42 /// to the file at the specified path.
43 ///
44 /// When F_modify is specified and \p FilePath refers to an existing on-disk
45 /// file \p Size may be set to -1, in which case the entire file is used.
46 /// Otherwise, the file shrinks or grows as necessary based on the value of
47 /// \p Size. It is an error to specify F_modify and Size=-1 if \p FilePath
48 /// does not exist.
50 create(StringRef FilePath, size_t Size, unsigned Flags = 0);
51
52 /// Returns a pointer to the start of the buffer.
53 virtual uint8_t *getBufferStart() const = 0;
54
55 /// Returns a pointer to the end of the buffer.
56 virtual uint8_t *getBufferEnd() const = 0;
57
58 /// Returns size of the buffer.
59 virtual size_t getBufferSize() const = 0;
60
61 /// Returns path where file will show up if buffer is committed.
62 StringRef getPath() const { return FinalPath; }
63
64 /// Flushes the content of the buffer to its file and deallocates the
65 /// buffer. If commit() is not called before this object's destructor
66 /// is called, the file is deleted in the destructor. The optional parameter
67 /// is used if it turns out you want the file size to be smaller than
68 /// initially requested.
69 virtual Error commit() = 0;
70
71 /// If this object was previously committed, the destructor just deletes
72 /// this object. If this object was not committed, the destructor
73 /// deallocates the buffer and the target file is never written.
74 virtual ~FileOutputBuffer() = default;
75
76 /// This removes the temporary file (unless it already was committed)
77 /// but keeps the memory mapping alive.
78 virtual void discard() {}
79
80protected:
82
83 std::string FinalPath;
84};
85} // end namespace llvm
86
87#endif
#define LLVM_ABI
Definition Compiler.h:211
Lightweight error class with error context and mandatory checking.
Definition Error.h:159
Tagged union holding either a T or a Error.
Definition Error.h:485
virtual uint8_t * getBufferStart() const =0
Returns a pointer to the start of the buffer.
static LLVM_ABI 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...
virtual ~FileOutputBuffer()=default
If this object was previously committed, the destructor just deletes this object.
StringRef getPath() const
Returns path where file will show up if buffer is committed.
FileOutputBuffer(StringRef Path)
virtual uint8_t * getBufferEnd() const =0
Returns a pointer to the end of the buffer.
virtual Error commit()=0
Flushes the content of the buffer to its file and deallocates the buffer.
virtual size_t getBufferSize() const =0
Returns size of the buffer.
virtual void discard()
This removes the temporary file (unless it already was committed) but keeps the memory mapping alive.
@ F_in_memory
Build the output in an anonymous memory buffer and write it to the file on commit() instead of mappin...
@ F_executable
Set the 'x' bit on the resulting file.
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
This is an optimization pass for GlobalISel generic memory operations.