1 //===- FileOutputBuffer.h -------------------------------------------------===// 2 // 3 // the mclinker project 4 // 5 // this file is distributed under the university of illinois open source 6 // license. see license.txt for details. 7 // 8 //===----------------------------------------------------------------------===// 9 #ifndef MCLD_SUPPORT_FILEOUTPUTBUFFER_H_ 10 #define MCLD_SUPPORT_FILEOUTPUTBUFFER_H_ 11 12 #include "mcld/Support/MemoryRegion.h" 13 14 #include <llvm/ADT/StringRef.h> 15 #include <llvm/Support/DataTypes.h> 16 #include <llvm/Support/FileSystem.h> 17 18 #include <system_error> 19 20 namespace mcld { 21 22 class FileHandle; 23 24 /// FileOutputBuffer - This interface is borrowed from llvm bassically, and we 25 /// may use ostream to emit output later. 26 class FileOutputBuffer { 27 public: 28 /// Factory method to create an OutputBuffer object which manages a read/write 29 /// buffer of the specified size. When committed, the buffer will be written 30 /// to the file at the specified path. 31 static std::error_code create(FileHandle& pFileHandle, 32 size_t pSize, 33 std::unique_ptr<FileOutputBuffer>& pResult); 34 35 /// Returns a pointer to the start of the buffer. getBufferStart()36 uint8_t* getBufferStart() { 37 return reinterpret_cast<uint8_t*>(m_pRegion->data()); 38 } 39 40 /// Returns a pointer to the end of the buffer. getBufferEnd()41 uint8_t* getBufferEnd() { 42 return reinterpret_cast<uint8_t*>(m_pRegion->data()) + m_pRegion->size(); 43 } 44 45 /// Returns size of the buffer. getBufferSize()46 size_t getBufferSize() const { return m_pRegion->size(); } 47 48 MemoryRegion request(size_t pOffset, size_t pLength); 49 50 /// Returns path where file will show up if buffer is committed. 51 llvm::StringRef getPath() const; 52 53 ~FileOutputBuffer(); 54 55 private: 56 FileOutputBuffer(const FileOutputBuffer&); 57 FileOutputBuffer& operator=(const FileOutputBuffer&); 58 59 FileOutputBuffer(llvm::sys::fs::mapped_file_region* pRegion, 60 FileHandle& pFileHandle); 61 62 std::unique_ptr<llvm::sys::fs::mapped_file_region> m_pRegion; 63 FileHandle& m_FileHandle; 64 }; 65 66 } // namespace mcld 67 68 #endif // MCLD_SUPPORT_FILEOUTPUTBUFFER_H_ 69