blob: cd1bb3a3328a8e669850726347439d4a2130364e [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 Turner7f6a7a32017-03-06 23:42:14 +000027DataBufferLLVM::CreateSliceFromPath(const llvm::Twine &Path, uint64_t Size,
28 uint64_t Offset, bool Private) {
Zachary Turner3f4a4b32017-02-24 18:56:49 +000029 // If the file resides non-locally, pass the volatile flag so that we don't
30 // mmap it.
Zachary Turner7f6a7a32017-03-06 23:42:14 +000031 if (!Private)
32 Private = !llvm::sys::fs::is_local(Path);
Zachary Turner3f4a4b32017-02-24 18:56:49 +000033
Zachary Turner7f6a7a32017-03-06 23:42:14 +000034 auto Buffer = llvm::MemoryBuffer::getFileSlice(Path, Size, Offset, Private);
35 if (!Buffer)
36 return nullptr;
37 return std::shared_ptr<DataBufferLLVM>(
38 new DataBufferLLVM(std::move(*Buffer)));
39}
40
41std::shared_ptr<DataBufferLLVM>
42DataBufferLLVM::CreateFromPath(const llvm::Twine &Path, bool NullTerminate, bool Private) {
43 // If the file resides non-locally, pass the volatile flag so that we don't
44 // mmap it.
45 if (!Private)
46 Private = !llvm::sys::fs::is_local(Path);
47
48 auto Buffer = llvm::MemoryBuffer::getFile(Path, -1, NullTerminate, Private);
Zachary Turner3f4a4b32017-02-24 18:56:49 +000049 if (!Buffer)
50 return nullptr;
51 return std::shared_ptr<DataBufferLLVM>(
52 new DataBufferLLVM(std::move(*Buffer)));
53}
54
Zachary Turner3f4a4b32017-02-24 18:56:49 +000055uint8_t *DataBufferLLVM::GetBytes() {
56 return const_cast<uint8_t *>(GetBuffer());
57}
58
59const uint8_t *DataBufferLLVM::GetBytes() const { return GetBuffer(); }
60
61lldb::offset_t DataBufferLLVM::GetByteSize() const {
62 return Buffer->getBufferSize();
63}
64
65const uint8_t *DataBufferLLVM::GetBuffer() const {
66 return reinterpret_cast<const uint8_t *>(Buffer->getBufferStart());
67}