Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 1 | //=== FileOutputBuffer.h - File Output Buffer -------------------*- C++ -*-===// |
| 2 | // |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame^] | 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 |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 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/SmallString.h" |
| 17 | #include "llvm/ADT/StringRef.h" |
| 18 | #include "llvm/Support/DataTypes.h" |
| 19 | #include "llvm/Support/Error.h" |
| 20 | #include "llvm/Support/FileSystem.h" |
| 21 | |
| 22 | namespace llvm { |
| 23 | /// FileOutputBuffer - This interface provides simple way to create an in-memory |
| 24 | /// buffer which will be written to a file. During the lifetime of these |
| 25 | /// objects, the content or existence of the specified file is undefined. That |
| 26 | /// is, creating an OutputBuffer for a file may immediately remove the file. |
| 27 | /// If the FileOutputBuffer is committed, the target file's content will become |
| 28 | /// the buffer content at the time of the commit. If the FileOutputBuffer is |
| 29 | /// not committed, the file will be deleted in the FileOutputBuffer destructor. |
| 30 | class FileOutputBuffer { |
| 31 | public: |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 32 | enum { |
| 33 | /// set the 'x' bit on the resulting file |
| 34 | F_executable = 1, |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 35 | }; |
| 36 | |
| 37 | /// Factory method to create an OutputBuffer object which manages a read/write |
| 38 | /// buffer of the specified size. When committed, the buffer will be written |
| 39 | /// to the file at the specified path. |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 40 | /// |
| 41 | /// When F_modify is specified and \p FilePath refers to an existing on-disk |
| 42 | /// file \p Size may be set to -1, in which case the entire file is used. |
| 43 | /// Otherwise, the file shrinks or grows as necessary based on the value of |
| 44 | /// \p Size. It is an error to specify F_modify and Size=-1 if \p FilePath |
| 45 | /// does not exist. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 46 | static Expected<std::unique_ptr<FileOutputBuffer>> |
| 47 | create(StringRef FilePath, size_t Size, unsigned Flags = 0); |
| 48 | |
| 49 | /// Returns a pointer to the start of the buffer. |
| 50 | virtual uint8_t *getBufferStart() const = 0; |
| 51 | |
| 52 | /// Returns a pointer to the end of the buffer. |
| 53 | virtual uint8_t *getBufferEnd() const = 0; |
| 54 | |
| 55 | /// Returns size of the buffer. |
| 56 | virtual size_t getBufferSize() const = 0; |
| 57 | |
| 58 | /// Returns path where file will show up if buffer is committed. |
| 59 | StringRef getPath() const { return FinalPath; } |
| 60 | |
| 61 | /// Flushes the content of the buffer to its file and deallocates the |
| 62 | /// buffer. If commit() is not called before this object's destructor |
| 63 | /// is called, the file is deleted in the destructor. The optional parameter |
| 64 | /// is used if it turns out you want the file size to be smaller than |
| 65 | /// initially requested. |
| 66 | virtual Error commit() = 0; |
| 67 | |
| 68 | /// If this object was previously committed, the destructor just deletes |
| 69 | /// this object. If this object was not committed, the destructor |
| 70 | /// deallocates the buffer and the target file is never written. |
| 71 | virtual ~FileOutputBuffer() {} |
| 72 | |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 73 | /// This removes the temporary file (unless it already was committed) |
| 74 | /// but keeps the memory mapping alive. |
| 75 | virtual void discard() {} |
| 76 | |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 77 | protected: |
| 78 | FileOutputBuffer(StringRef Path) : FinalPath(Path) {} |
| 79 | |
| 80 | std::string FinalPath; |
| 81 | }; |
| 82 | } // end namespace llvm |
| 83 | |
| 84 | #endif |