summaryrefslogtreecommitdiff
path: root/lib/Support/FileOutputBuffer.cpp
blob: ed8c89817e8823e8501cbfe1cf1851e93eddc991 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
//===- FileOutputBuffer.cpp ------------------------------------------------===//
//
//                     The MCLinker Project
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include <mcld/Support/FileOutputBuffer.h>
#include <mcld/Support/FileHandle.h>
#include <mcld/Support/Path.h>

using namespace mcld;
using llvm::sys::fs::mapped_file_region;

FileOutputBuffer::FileOutputBuffer(llvm::sys::fs::mapped_file_region* pRegion,
                                   FileHandle& pFileHandle)
  : m_pRegion(pRegion), m_FileHandle(pFileHandle)
{
}

FileOutputBuffer::~FileOutputBuffer()
{
  // Unmap buffer, letting OS flush dirty pages to file on disk.
  m_pRegion.reset(0);
}

llvm::error_code FileOutputBuffer::create(FileHandle& pFileHandle,
    size_t pSize, llvm::OwningPtr<FileOutputBuffer>& pResult)
{
  llvm::error_code EC;
  llvm::OwningPtr<mapped_file_region> mapped_file(new mapped_file_region(
      pFileHandle.handler(),
      false,
      mapped_file_region::readwrite,
      pSize,
      0,
      EC));

  if (EC)
    return EC;

  pResult.reset(new FileOutputBuffer(mapped_file.get(), pFileHandle));
  if (pResult)
    mapped_file.take();
  return llvm::error_code::success();
}

MemoryRegion FileOutputBuffer::request(size_t pOffset, size_t pLength)
{
  if (pOffset > getBufferSize() || (pOffset + pLength) > getBufferSize())
    return MemoryRegion();
  return MemoryRegion(getBufferStart() + pOffset, pLength);
}

llvm::StringRef FileOutputBuffer::getPath() const
{
  return m_FileHandle.path().native();
}