1239310Sdim//=== FileOutputBuffer.h - File Output Buffer -------------------*- C++ -*-===// 2239310Sdim// 3353358Sdim// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4353358Sdim// See https://llvm.org/LICENSE.txt for license information. 5353358Sdim// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6239310Sdim// 7239310Sdim//===----------------------------------------------------------------------===// 8239310Sdim// 9239310Sdim// Utility for creating a in-memory buffer that will be written to a file. 10239310Sdim// 11239310Sdim//===----------------------------------------------------------------------===// 12239310Sdim 13239310Sdim#ifndef LLVM_SUPPORT_FILEOUTPUTBUFFER_H 14239310Sdim#define LLVM_SUPPORT_FILEOUTPUTBUFFER_H 15239310Sdim 16239310Sdim#include "llvm/ADT/SmallString.h" 17239310Sdim#include "llvm/ADT/StringRef.h" 18239310Sdim#include "llvm/Support/DataTypes.h" 19327952Sdim#include "llvm/Support/Error.h" 20249423Sdim#include "llvm/Support/FileSystem.h" 21239310Sdim 22239310Sdimnamespace llvm { 23239310Sdim/// FileOutputBuffer - This interface provides simple way to create an in-memory 24249423Sdim/// buffer which will be written to a file. During the lifetime of these 25239310Sdim/// objects, the content or existence of the specified file is undefined. That 26239310Sdim/// is, creating an OutputBuffer for a file may immediately remove the file. 27249423Sdim/// If the FileOutputBuffer is committed, the target file's content will become 28249423Sdim/// the buffer content at the time of the commit. If the FileOutputBuffer is 29239310Sdim/// not committed, the file will be deleted in the FileOutputBuffer destructor. 30239310Sdimclass FileOutputBuffer { 31239310Sdimpublic: 32341825Sdim enum { 33341825Sdim /// set the 'x' bit on the resulting file 34341825Sdim F_executable = 1, 35360784Sdim 36360784Sdim /// Don't use mmap and instead write an in-memory buffer to a file when this 37360784Sdim /// buffer is closed. 38360784Sdim F_no_mmap = 2, 39249423Sdim }; 40239310Sdim 41239310Sdim /// Factory method to create an OutputBuffer object which manages a read/write 42239310Sdim /// buffer of the specified size. When committed, the buffer will be written 43249423Sdim /// to the file at the specified path. 44341825Sdim /// 45341825Sdim /// When F_modify is specified and \p FilePath refers to an existing on-disk 46341825Sdim /// file \p Size may be set to -1, in which case the entire file is used. 47341825Sdim /// Otherwise, the file shrinks or grows as necessary based on the value of 48341825Sdim /// \p Size. It is an error to specify F_modify and Size=-1 if \p FilePath 49341825Sdim /// does not exist. 50327952Sdim static Expected<std::unique_ptr<FileOutputBuffer>> 51296417Sdim create(StringRef FilePath, size_t Size, unsigned Flags = 0); 52239310Sdim 53239310Sdim /// Returns a pointer to the start of the buffer. 54327952Sdim virtual uint8_t *getBufferStart() const = 0; 55249423Sdim 56239310Sdim /// Returns a pointer to the end of the buffer. 57327952Sdim virtual uint8_t *getBufferEnd() const = 0; 58249423Sdim 59239310Sdim /// Returns size of the buffer. 60327952Sdim virtual size_t getBufferSize() const = 0; 61249423Sdim 62239310Sdim /// Returns path where file will show up if buffer is committed. 63327952Sdim StringRef getPath() const { return FinalPath; } 64249423Sdim 65249423Sdim /// Flushes the content of the buffer to its file and deallocates the 66239310Sdim /// buffer. If commit() is not called before this object's destructor 67239310Sdim /// is called, the file is deleted in the destructor. The optional parameter 68239310Sdim /// is used if it turns out you want the file size to be smaller than 69239310Sdim /// initially requested. 70327952Sdim virtual Error commit() = 0; 71249423Sdim 72239310Sdim /// If this object was previously committed, the destructor just deletes 73239310Sdim /// this object. If this object was not committed, the destructor 74239310Sdim /// deallocates the buffer and the target file is never written. 75327952Sdim virtual ~FileOutputBuffer() {} 76239310Sdim 77344779Sdim /// This removes the temporary file (unless it already was committed) 78344779Sdim /// but keeps the memory mapping alive. 79344779Sdim virtual void discard() {} 80344779Sdim 81327952Sdimprotected: 82327952Sdim FileOutputBuffer(StringRef Path) : FinalPath(Path) {} 83249423Sdim 84327952Sdim std::string FinalPath; 85239310Sdim}; 86239310Sdim} // end namespace llvm 87239310Sdim 88239310Sdim#endif 89