blob: 713c3c2814eafaad82ccf21536f5993158e29587 [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
Zachary Turner4479ac12017-04-06 18:12:24 +000016#include <assert.h> // for assert
17#include <type_traits> // for move
18
Zachary Turner3f4a4b32017-02-24 18:56:49 +000019using namespace lldb_private;
20
Pavel Labath50251fc2017-12-21 10:54:30 +000021DataBufferLLVM::DataBufferLLVM(
22 std::unique_ptr<llvm::WritableMemoryBuffer> MemBuffer)
Zachary Turner3f4a4b32017-02-24 18:56:49 +000023 : Buffer(std::move(MemBuffer)) {
24 assert(Buffer != nullptr &&
25 "Cannot construct a DataBufferLLVM with a null buffer");
26}
27
28DataBufferLLVM::~DataBufferLLVM() {}
29
30std::shared_ptr<DataBufferLLVM>
Zachary Turner7f6a7a32017-03-06 23:42:14 +000031DataBufferLLVM::CreateSliceFromPath(const llvm::Twine &Path, uint64_t Size,
Pavel Labath50251fc2017-12-21 10:54:30 +000032 uint64_t Offset) {
Zachary Turner3f4a4b32017-02-24 18:56:49 +000033 // If the file resides non-locally, pass the volatile flag so that we don't
34 // mmap it.
Pavel Labath50251fc2017-12-21 10:54:30 +000035 bool IsVolatile = !llvm::sys::fs::is_local(Path);
Zachary Turner3f4a4b32017-02-24 18:56:49 +000036
Pavel Labath50251fc2017-12-21 10:54:30 +000037 auto Buffer =
38 llvm::WritableMemoryBuffer::getFileSlice(Path, Size, Offset, IsVolatile);
Zachary Turner7f6a7a32017-03-06 23:42:14 +000039 if (!Buffer)
40 return nullptr;
41 return std::shared_ptr<DataBufferLLVM>(
42 new DataBufferLLVM(std::move(*Buffer)));
43}
44
45std::shared_ptr<DataBufferLLVM>
Pavel Labath50251fc2017-12-21 10:54:30 +000046DataBufferLLVM::CreateFromPath(const llvm::Twine &Path) {
Zachary Turner7f6a7a32017-03-06 23:42:14 +000047 // If the file resides non-locally, pass the volatile flag so that we don't
48 // mmap it.
Pavel Labath50251fc2017-12-21 10:54:30 +000049 bool IsVolatile = !llvm::sys::fs::is_local(Path);
Zachary Turner7f6a7a32017-03-06 23:42:14 +000050
Pavel Labath50251fc2017-12-21 10:54:30 +000051 auto Buffer = llvm::WritableMemoryBuffer::getFile(Path, -1, IsVolatile);
Zachary Turner3f4a4b32017-02-24 18:56:49 +000052 if (!Buffer)
53 return nullptr;
54 return std::shared_ptr<DataBufferLLVM>(
55 new DataBufferLLVM(std::move(*Buffer)));
56}
57
Zachary Turner3f4a4b32017-02-24 18:56:49 +000058uint8_t *DataBufferLLVM::GetBytes() {
Pavel Labath50251fc2017-12-21 10:54:30 +000059 return reinterpret_cast<uint8_t *>(Buffer->getBufferStart());
Zachary Turner3f4a4b32017-02-24 18:56:49 +000060}
61
Pavel Labath50251fc2017-12-21 10:54:30 +000062const uint8_t *DataBufferLLVM::GetBytes() const {
63 return reinterpret_cast<const uint8_t *>(Buffer->getBufferStart());
64}
Zachary Turner3f4a4b32017-02-24 18:56:49 +000065
66lldb::offset_t DataBufferLLVM::GetByteSize() const {
67 return Buffer->getBufferSize();
68}