FileOutputBuffer.h revision f33f6de54db174aa679a4b6d1e040d37e95541c0
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#ifdef ENABLE_UNITTEST 12#include <gtest.h> 13#endif 14 15#include <mcld/Support/MemoryRegion.h> 16#include <llvm/ADT/OwningPtr.h> 17#include <llvm/ADT/StringRef.h> 18#include <llvm/Support/DataTypes.h> 19#include <llvm/Support/FileSystem.h> 20#include <llvm/Support/system_error.h> 21 22namespace mcld { 23 24class FileHandle; 25 26/// FileOutputBuffer - This interface is borrowed from llvm bassically, and we 27/// may use ostream to emit output later. 28class FileOutputBuffer { 29public: 30 /// Factory method to create an OutputBuffer object which manages a read/write 31 /// buffer of the specified size. When committed, the buffer will be written 32 /// to the file at the specified path. 33 static llvm::error_code create(FileHandle& pFileHandle, 34 size_t pSize, 35 llvm::OwningPtr<FileOutputBuffer>& pResult); 36 37 /// Returns a pointer to the start of the buffer. 38 uint8_t* getBufferStart() { 39 return (uint8_t*)m_pRegion->data(); 40 } 41 42 /// Returns a pointer to the end of the buffer. 43 uint8_t* getBufferEnd() { 44 return (uint8_t*)m_pRegion->data() + m_pRegion->size(); 45 } 46 47 /// Returns size of the buffer. 48 size_t getBufferSize() const { 49 return m_pRegion->size(); 50 } 51 52 MemoryRegion request(size_t pOffset, size_t pLength); 53 54 /// Returns path where file will show up if buffer is committed. 55 llvm::StringRef getPath() const; 56 57 ~FileOutputBuffer(); 58 59private: 60 FileOutputBuffer(const FileOutputBuffer &); 61 FileOutputBuffer &operator=(const FileOutputBuffer &); 62 63 FileOutputBuffer(llvm::sys::fs::mapped_file_region* pRegion, 64 FileHandle& pFileHandle); 65 66 llvm::OwningPtr<llvm::sys::fs::mapped_file_region> m_pRegion; 67 FileHandle& m_FileHandle; 68}; 69 70} // namespace mcld 71 72#endif 73