1//===- raw_mem_ostream.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/raw_mem_ostream.h>
10#include <mcld/Support/MsgHandling.h>
11#include <mcld/Support/MemoryRegion.h>
12#include <mcld/Support/MemoryArea.h>
13#include <mcld/Support/FileHandle.h>
14
15using namespace mcld;
16
17//===----------------------------------------------------------------------===//
18// raw_mem_ostream
19//===----------------------------------------------------------------------===//
20raw_mem_ostream::raw_mem_ostream(MemoryArea &pMemoryArea)
21  : m_MemoryArea(pMemoryArea), m_Position(0) {
22  if (NULL == m_MemoryArea.handler() ||
23      !(m_MemoryArea.handler()->isGood() &&
24        m_MemoryArea.handler()->isWritable())) {
25    fatal(diag::fatal_unwritable_output) << m_MemoryArea.handler()->path();
26  }
27}
28
29raw_mem_ostream::~raw_mem_ostream()
30{
31  flush();
32  m_MemoryArea.clear();
33}
34
35void raw_mem_ostream::write_impl(const char *pPtr, size_t pSize)
36{
37  MemoryRegion* region = m_MemoryArea.request(m_Position, pSize);
38  memcpy(region->start(), pPtr, pSize);
39  m_Position += pSize;
40}
41
42uint64_t raw_mem_ostream::current_pos() const
43{
44  return m_Position;
45}
46
47