//===- MemoryArea.cpp -----------------------------------------------------===//
//
//                     The MCLinker Project
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include <mcld/Support/MemoryArea.h>
#include <mcld/Support/MsgHandling.h>

#include <llvm/Support/ErrorOr.h>

#include <cassert>
#include <system_error>

using namespace mcld;

//===--------------------------------------------------------------------===//
// MemoryArea
//===--------------------------------------------------------------------===//
MemoryArea::MemoryArea(llvm::StringRef pFilename)
{
  llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> buffer_or_error =
      llvm::MemoryBuffer::getFile(pFilename, /*FileSize*/ -1,
                                  /*RequiresNullTerminator*/ false);
  if (std::error_code ec = buffer_or_error.getError()) {
    (void) ec;
    fatal(diag::fatal_cannot_read_input) << pFilename.str();
  }
  m_pMemoryBuffer = std::move(buffer_or_error.get());
}

MemoryArea::MemoryArea(const char* pMemBuffer, size_t pSize)
{
  llvm::StringRef mem(pMemBuffer, pSize);
  llvm::MemoryBuffer* buffer =
      llvm::MemoryBuffer::getMemBuffer(mem, /*BufferName*/ "NaN",
                                       /*RequiresNullTerminator*/ false);
  assert(buffer != NULL);
  m_pMemoryBuffer.reset(buffer);
}

llvm::StringRef MemoryArea::request(size_t pOffset, size_t pLength)
{
  return llvm::StringRef(m_pMemoryBuffer->getBufferStart() + pOffset, pLength);
}

size_t MemoryArea::size() const
{
  return m_pMemoryBuffer->getBufferSize();
}