LLVM API Documentation

FileOutputBuffer.h
Go to the documentation of this file.
00001 //=== FileOutputBuffer.h - File Output Buffer -------------------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // Utility for creating a in-memory buffer that will be written to a file.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #ifndef LLVM_SUPPORT_FILEOUTPUTBUFFER_H
00015 #define LLVM_SUPPORT_FILEOUTPUTBUFFER_H
00016 
00017 #include "llvm/ADT/SmallString.h"
00018 #include "llvm/ADT/StringRef.h"
00019 #include "llvm/Support/DataTypes.h"
00020 #include "llvm/Support/FileSystem.h"
00021 
00022 namespace llvm {
00023 /// FileOutputBuffer - This interface provides simple way to create an in-memory
00024 /// buffer which will be written to a file. During the lifetime of these
00025 /// objects, the content or existence of the specified file is undefined. That
00026 /// is, creating an OutputBuffer for a file may immediately remove the file.
00027 /// If the FileOutputBuffer is committed, the target file's content will become
00028 /// the buffer content at the time of the commit.  If the FileOutputBuffer is
00029 /// not committed, the file will be deleted in the FileOutputBuffer destructor.
00030 class FileOutputBuffer {
00031 public:
00032 
00033   enum  {
00034     F_executable = 1  /// set the 'x' bit on the resulting file
00035   };
00036 
00037   /// Factory method to create an OutputBuffer object which manages a read/write
00038   /// buffer of the specified size. When committed, the buffer will be written
00039   /// to the file at the specified path.
00040   static std::error_code create(StringRef FilePath, size_t Size,
00041                                 std::unique_ptr<FileOutputBuffer> &Result,
00042                                 unsigned Flags = 0);
00043 
00044   /// Returns a pointer to the start of the buffer.
00045   uint8_t *getBufferStart() {
00046     return (uint8_t*)Region->data();
00047   }
00048 
00049   /// Returns a pointer to the end of the buffer.
00050   uint8_t *getBufferEnd() {
00051     return (uint8_t*)Region->data() + Region->size();
00052   }
00053 
00054   /// Returns size of the buffer.
00055   size_t getBufferSize() const {
00056     return Region->size();
00057   }
00058 
00059   /// Returns path where file will show up if buffer is committed.
00060   StringRef getPath() const {
00061     return FinalPath;
00062   }
00063 
00064   /// Flushes the content of the buffer to its file and deallocates the
00065   /// buffer.  If commit() is not called before this object's destructor
00066   /// is called, the file is deleted in the destructor. The optional parameter
00067   /// is used if it turns out you want the file size to be smaller than
00068   /// initially requested.
00069   std::error_code commit(int64_t NewSmallerSize = -1);
00070 
00071   /// If this object was previously committed, the destructor just deletes
00072   /// this object.  If this object was not committed, the destructor
00073   /// deallocates the buffer and the target file is never written.
00074   ~FileOutputBuffer();
00075 
00076 private:
00077   FileOutputBuffer(const FileOutputBuffer &) LLVM_DELETED_FUNCTION;
00078   FileOutputBuffer &operator=(const FileOutputBuffer &) LLVM_DELETED_FUNCTION;
00079 
00080   FileOutputBuffer(std::unique_ptr<llvm::sys::fs::mapped_file_region> R,
00081                    StringRef Path, StringRef TempPath);
00082 
00083   std::unique_ptr<llvm::sys::fs::mapped_file_region> Region;
00084   SmallString<128>    FinalPath;
00085   SmallString<128>    TempPath;
00086 };
00087 } // end namespace llvm
00088 
00089 #endif