blob: 2f6654d4c9a774870fc3d62aebeaa2ef02550174 [file] [log] [blame]
Zachary Turner3f4a4b32017-02-24 18:56:49 +00001//===--- DataBufferLLVM.cpp -------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Zachary Turner666cc0b2017-03-04 01:30:05 +000010#include "lldb/Utility/DataBufferLLVM.h"
Zachary Turner3f4a4b32017-02-24 18:56:49 +000011
Zachary Turner666cc0b2017-03-04 01:30:05 +000012#include "llvm/ADT/Twine.h"
Zachary Turner3f4a4b32017-02-24 18:56:49 +000013#include "llvm/Support/FileSystem.h"
14#include "llvm/Support/MemoryBuffer.h"
15
16using namespace lldb_private;
17
18DataBufferLLVM::DataBufferLLVM(std::unique_ptr<llvm::MemoryBuffer> MemBuffer)
19 : Buffer(std::move(MemBuffer)) {
20 assert(Buffer != nullptr &&
21 "Cannot construct a DataBufferLLVM with a null buffer");
22}
23
24DataBufferLLVM::~DataBufferLLVM() {}
25
26std::shared_ptr<DataBufferLLVM>
Zachary Turner666cc0b2017-03-04 01:30:05 +000027DataBufferLLVM::CreateFromPath(const llvm::Twine &Path, uint64_t Size,
Zachary Turner3f4a4b32017-02-24 18:56:49 +000028 uint64_t Offset) {
29 // If the file resides non-locally, pass the volatile flag so that we don't
30 // mmap it.
31 bool Volatile = !llvm::sys::fs::is_local(Path);
32
33 auto Buffer = llvm::MemoryBuffer::getFileSlice(Path, Size, Offset, Volatile);
34 if (!Buffer)
35 return nullptr;
36 return std::shared_ptr<DataBufferLLVM>(
37 new DataBufferLLVM(std::move(*Buffer)));
38}
39
Zachary Turner3f4a4b32017-02-24 18:56:49 +000040uint8_t *DataBufferLLVM::GetBytes() {
41 return const_cast<uint8_t *>(GetBuffer());
42}
43
44const uint8_t *DataBufferLLVM::GetBytes() const { return GetBuffer(); }
45
46lldb::offset_t DataBufferLLVM::GetByteSize() const {
47 return Buffer->getBufferSize();
48}
49
50const uint8_t *DataBufferLLVM::GetBuffer() const {
51 return reinterpret_cast<const uint8_t *>(Buffer->getBufferStart());
52}