Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:44:30

0001 //=== FileOutputBuffer.h - File Output Buffer -------------------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 //
0009 // Utility for creating a in-memory buffer that will be written to a file.
0010 //
0011 //===----------------------------------------------------------------------===//
0012 
0013 #ifndef LLVM_SUPPORT_FILEOUTPUTBUFFER_H
0014 #define LLVM_SUPPORT_FILEOUTPUTBUFFER_H
0015 
0016 #include "llvm/ADT/StringRef.h"
0017 #include "llvm/Support/DataTypes.h"
0018 #include "llvm/Support/Error.h"
0019 
0020 namespace llvm {
0021 /// FileOutputBuffer - This interface provides simple way to create an in-memory
0022 /// buffer which will be written to a file. During the lifetime of these
0023 /// objects, the content or existence of the specified file is undefined. That
0024 /// is, creating an OutputBuffer for a file may immediately remove the file.
0025 /// If the FileOutputBuffer is committed, the target file's content will become
0026 /// the buffer content at the time of the commit.  If the FileOutputBuffer is
0027 /// not committed, the file will be deleted in the FileOutputBuffer destructor.
0028 class FileOutputBuffer {
0029 public:
0030   enum {
0031     /// Set the 'x' bit on the resulting file.
0032     F_executable = 1,
0033 
0034     /// Don't use mmap and instead write an in-memory buffer to a file when this
0035     /// buffer is closed.
0036     F_no_mmap = 2,
0037   };
0038 
0039   /// Factory method to create an OutputBuffer object which manages a read/write
0040   /// buffer of the specified size. When committed, the buffer will be written
0041   /// to the file at the specified path.
0042   ///
0043   /// When F_modify is specified and \p FilePath refers to an existing on-disk
0044   /// file \p Size may be set to -1, in which case the entire file is used.
0045   /// Otherwise, the file shrinks or grows as necessary based on the value of
0046   /// \p Size.  It is an error to specify F_modify and Size=-1 if \p FilePath
0047   /// does not exist.
0048   static Expected<std::unique_ptr<FileOutputBuffer>>
0049   create(StringRef FilePath, size_t Size, unsigned Flags = 0);
0050 
0051   /// Returns a pointer to the start of the buffer.
0052   virtual uint8_t *getBufferStart() const = 0;
0053 
0054   /// Returns a pointer to the end of the buffer.
0055   virtual uint8_t *getBufferEnd() const = 0;
0056 
0057   /// Returns size of the buffer.
0058   virtual size_t getBufferSize() const = 0;
0059 
0060   /// Returns path where file will show up if buffer is committed.
0061   StringRef getPath() const { return FinalPath; }
0062 
0063   /// Flushes the content of the buffer to its file and deallocates the
0064   /// buffer.  If commit() is not called before this object's destructor
0065   /// is called, the file is deleted in the destructor. The optional parameter
0066   /// is used if it turns out you want the file size to be smaller than
0067   /// initially requested.
0068   virtual Error commit() = 0;
0069 
0070   /// If this object was previously committed, the destructor just deletes
0071   /// this object.  If this object was not committed, the destructor
0072   /// deallocates the buffer and the target file is never written.
0073   virtual ~FileOutputBuffer() = default;
0074 
0075   /// This removes the temporary file (unless it already was committed)
0076   /// but keeps the memory mapping alive.
0077   virtual void discard() {}
0078 
0079 protected:
0080   FileOutputBuffer(StringRef Path) : FinalPath(Path) {}
0081 
0082   std::string FinalPath;
0083 };
0084 } // end namespace llvm
0085 
0086 #endif