1 //===- MemoryArea.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/MemoryArea.h>
10 #include <mcld/Support/MsgHandling.h>
11
12 #include <llvm/Support/ErrorOr.h>
13
14 #include <cassert>
15 #include <system_error>
16
17 using namespace mcld;
18
19 //===--------------------------------------------------------------------===//
20 // MemoryArea
21 //===--------------------------------------------------------------------===//
MemoryArea(llvm::StringRef pFilename)22 MemoryArea::MemoryArea(llvm::StringRef pFilename)
23 {
24 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> buffer_or_error =
25 llvm::MemoryBuffer::getFile(pFilename, /*FileSize*/ -1,
26 /*RequiresNullTerminator*/ false);
27 if (std::error_code ec = buffer_or_error.getError()) {
28 (void) ec;
29 fatal(diag::fatal_cannot_read_input) << pFilename.str();
30 }
31 m_pMemoryBuffer = std::move(buffer_or_error.get());
32 }
33
MemoryArea(const char * pMemBuffer,size_t pSize)34 MemoryArea::MemoryArea(const char* pMemBuffer, size_t pSize)
35 {
36 llvm::StringRef mem(pMemBuffer, pSize);
37 llvm::MemoryBuffer* buffer =
38 llvm::MemoryBuffer::getMemBuffer(mem, /*BufferName*/ "NaN",
39 /*RequiresNullTerminator*/ false);
40 assert(buffer != NULL);
41 m_pMemoryBuffer.reset(buffer);
42 }
43
request(size_t pOffset,size_t pLength)44 llvm::StringRef MemoryArea::request(size_t pOffset, size_t pLength)
45 {
46 return llvm::StringRef(m_pMemoryBuffer->getBufferStart() + pOffset, pLength);
47 }
48
size() const49 size_t MemoryArea::size() const
50 {
51 return m_pMemoryBuffer->getBufferSize();
52 }
53