Stephen Hines | 87f3465 | 2014-02-14 18:00:16 -0800 | [diff] [blame] | 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 | //===----------------------------------------------------------------------===// |
Stephen Hines | 37b74a3 | 2014-11-26 18:48:20 -0800 | [diff] [blame] | 9 | #ifndef MCLD_SUPPORT_FILEOUTPUTBUFFER_H_ |
| 10 | #define MCLD_SUPPORT_FILEOUTPUTBUFFER_H_ |
Stephen Hines | 87f3465 | 2014-02-14 18:00:16 -0800 | [diff] [blame] | 11 | |
Stephen Hines | 37b74a3 | 2014-11-26 18:48:20 -0800 | [diff] [blame] | 12 | #include "mcld/Support/MemoryRegion.h" |
| 13 | |
Stephen Hines | 87f3465 | 2014-02-14 18:00:16 -0800 | [diff] [blame] | 14 | #include <llvm/ADT/StringRef.h> |
| 15 | #include <llvm/Support/DataTypes.h> |
| 16 | #include <llvm/Support/FileSystem.h> |
Stephen Hines | 37b74a3 | 2014-11-26 18:48:20 -0800 | [diff] [blame] | 17 | |
Stephen Hines | 0dea6bc | 2014-07-15 18:33:32 -0700 | [diff] [blame] | 18 | #include <system_error> |
Stephen Hines | 87f3465 | 2014-02-14 18:00:16 -0800 | [diff] [blame] | 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 { |
Stephen Hines | 37b74a3 | 2014-11-26 18:48:20 -0800 | [diff] [blame] | 27 | public: |
Stephen Hines | 87f3465 | 2014-02-14 18:00:16 -0800 | [diff] [blame] | 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. |
Stephen Hines | 0dea6bc | 2014-07-15 18:33:32 -0700 | [diff] [blame] | 31 | static std::error_code create(FileHandle& pFileHandle, |
| 32 | size_t pSize, |
| 33 | std::unique_ptr<FileOutputBuffer>& pResult); |
Stephen Hines | 87f3465 | 2014-02-14 18:00:16 -0800 | [diff] [blame] | 34 | |
| 35 | /// Returns a pointer to the start of the buffer. |
| 36 | uint8_t* getBufferStart() { |
Stephen Hines | 37b74a3 | 2014-11-26 18:48:20 -0800 | [diff] [blame] | 37 | return reinterpret_cast<uint8_t*>(m_pRegion->data()); |
Stephen Hines | 87f3465 | 2014-02-14 18:00:16 -0800 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | /// Returns a pointer to the end of the buffer. |
| 41 | uint8_t* getBufferEnd() { |
Stephen Hines | 37b74a3 | 2014-11-26 18:48:20 -0800 | [diff] [blame] | 42 | return reinterpret_cast<uint8_t*>(m_pRegion->data()) + m_pRegion->size(); |
Stephen Hines | 87f3465 | 2014-02-14 18:00:16 -0800 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | /// Returns size of the buffer. |
Stephen Hines | 37b74a3 | 2014-11-26 18:48:20 -0800 | [diff] [blame] | 46 | size_t getBufferSize() const { return m_pRegion->size(); } |
Stephen Hines | 87f3465 | 2014-02-14 18:00:16 -0800 | [diff] [blame] | 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 | |
Stephen Hines | 37b74a3 | 2014-11-26 18:48:20 -0800 | [diff] [blame] | 55 | private: |
| 56 | FileOutputBuffer(const FileOutputBuffer&); |
| 57 | FileOutputBuffer& operator=(const FileOutputBuffer&); |
Stephen Hines | 87f3465 | 2014-02-14 18:00:16 -0800 | [diff] [blame] | 58 | |
| 59 | FileOutputBuffer(llvm::sys::fs::mapped_file_region* pRegion, |
| 60 | FileHandle& pFileHandle); |
| 61 | |
Stephen Hines | 0dea6bc | 2014-07-15 18:33:32 -0700 | [diff] [blame] | 62 | std::unique_ptr<llvm::sys::fs::mapped_file_region> m_pRegion; |
Stephen Hines | 87f3465 | 2014-02-14 18:00:16 -0800 | [diff] [blame] | 63 | FileHandle& m_FileHandle; |
| 64 | }; |
| 65 | |
Stephen Hines | 37b74a3 | 2014-11-26 18:48:20 -0800 | [diff] [blame] | 66 | } // namespace mcld |
Stephen Hines | 87f3465 | 2014-02-14 18:00:16 -0800 | [diff] [blame] | 67 | |
Stephen Hines | 37b74a3 | 2014-11-26 18:48:20 -0800 | [diff] [blame] | 68 | #endif // MCLD_SUPPORT_FILEOUTPUTBUFFER_H_ |