1 //===- FileOutputBuffer.cpp ------------------------------------------------===// 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 #include <mcld/Support/FileOutputBuffer.h> 10 #include <mcld/Support/FileHandle.h> 11 #include <mcld/Support/Path.h> 12 13 using namespace mcld; 14 using llvm::sys::fs::mapped_file_region; 15 16 FileOutputBuffer::FileOutputBuffer(llvm::sys::fs::mapped_file_region* pRegion, 17 FileHandle& pFileHandle) 18 : m_pRegion(pRegion), m_FileHandle(pFileHandle) 19 { 20 } 21 22 FileOutputBuffer::~FileOutputBuffer() 23 { 24 // Unmap buffer, letting OS flush dirty pages to file on disk. 25 m_pRegion.reset(0); 26 } 27 28 std::error_code FileOutputBuffer::create(FileHandle& pFileHandle, 29 size_t pSize, std::unique_ptr<FileOutputBuffer>& pResult) 30 { 31 std::error_code ec; 32 std::unique_ptr<mapped_file_region> mapped_file(new mapped_file_region( 33 pFileHandle.handler(), 34 false, 35 mapped_file_region::readwrite, 36 pSize, 37 0, 38 ec)); 39 40 if (ec) 41 return ec; 42 43 pResult.reset(new FileOutputBuffer(mapped_file.get(), pFileHandle)); 44 if (pResult) 45 mapped_file.release(); 46 return std::error_code(); 47 } 48 49 MemoryRegion FileOutputBuffer::request(size_t pOffset, size_t pLength) 50 { 51 if (pOffset > getBufferSize() || (pOffset + pLength) > getBufferSize()) 52 return MemoryRegion(); 53 return MemoryRegion(getBufferStart() + pOffset, pLength); 54 } 55 56 llvm::StringRef FileOutputBuffer::getPath() const 57 { 58 return m_FileHandle.path().native(); 59 } 60