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 namespace mcld {
18
19 //===--------------------------------------------------------------------===//
20 // MemoryArea
21 //===--------------------------------------------------------------------===//
MemoryArea(llvm::StringRef pFilename)22 MemoryArea::MemoryArea(llvm::StringRef pFilename) {
23 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer> > buffer_or_error =
24 llvm::MemoryBuffer::getFile(pFilename,
25 /*FileSize*/ -1,
26 /*RequiresNullTerminator*/ false);
27 if (!buffer_or_error) {
28 fatal(diag::fatal_cannot_read_input) << pFilename.str();
29 }
30 m_pMemoryBuffer = std::move(buffer_or_error.get());
31 }
32
MemoryArea(const char * pMemBuffer,size_t pSize)33 MemoryArea::MemoryArea(const char* pMemBuffer, size_t pSize) {
34 llvm::StringRef mem(pMemBuffer, pSize);
35 m_pMemoryBuffer =
36 llvm::MemoryBuffer::getMemBuffer(mem,
37 /*BufferName*/ "NaN",
38 /*RequiresNullTerminator*/ false);
39 }
40
request(size_t pOffset,size_t pLength)41 llvm::StringRef MemoryArea::request(size_t pOffset, size_t pLength) {
42 return llvm::StringRef(m_pMemoryBuffer->getBufferStart() + pOffset, pLength);
43 }
44
size() const45 size_t MemoryArea::size() const {
46 return m_pMemoryBuffer->getBufferSize();
47 }
48
49 } // namespace mcld
50