Zachary Turner | 3f4a4b3 | 2017-02-24 18:56:49 +0000 | [diff] [blame] | 1 | //===--- 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 Turner | 666cc0b | 2017-03-04 01:30:05 +0000 | [diff] [blame] | 10 | #include "lldb/Utility/DataBufferLLVM.h" |
Zachary Turner | 3f4a4b3 | 2017-02-24 18:56:49 +0000 | [diff] [blame] | 11 | |
Zachary Turner | 666cc0b | 2017-03-04 01:30:05 +0000 | [diff] [blame] | 12 | #include "llvm/ADT/Twine.h" |
Zachary Turner | 3f4a4b3 | 2017-02-24 18:56:49 +0000 | [diff] [blame] | 13 | #include "llvm/Support/FileSystem.h" |
| 14 | #include "llvm/Support/MemoryBuffer.h" |
| 15 | |
Zachary Turner | 4479ac1 | 2017-04-06 18:12:24 +0000 | [diff] [blame^] | 16 | #include <assert.h> // for assert |
| 17 | #include <type_traits> // for move |
| 18 | |
Zachary Turner | 3f4a4b3 | 2017-02-24 18:56:49 +0000 | [diff] [blame] | 19 | using namespace lldb_private; |
| 20 | |
| 21 | DataBufferLLVM::DataBufferLLVM(std::unique_ptr<llvm::MemoryBuffer> MemBuffer) |
| 22 | : Buffer(std::move(MemBuffer)) { |
| 23 | assert(Buffer != nullptr && |
| 24 | "Cannot construct a DataBufferLLVM with a null buffer"); |
| 25 | } |
| 26 | |
| 27 | DataBufferLLVM::~DataBufferLLVM() {} |
| 28 | |
| 29 | std::shared_ptr<DataBufferLLVM> |
Zachary Turner | 7f6a7a3 | 2017-03-06 23:42:14 +0000 | [diff] [blame] | 30 | DataBufferLLVM::CreateSliceFromPath(const llvm::Twine &Path, uint64_t Size, |
| 31 | uint64_t Offset, bool Private) { |
Zachary Turner | 3f4a4b3 | 2017-02-24 18:56:49 +0000 | [diff] [blame] | 32 | // If the file resides non-locally, pass the volatile flag so that we don't |
| 33 | // mmap it. |
Zachary Turner | 7f6a7a3 | 2017-03-06 23:42:14 +0000 | [diff] [blame] | 34 | if (!Private) |
| 35 | Private = !llvm::sys::fs::is_local(Path); |
Zachary Turner | 3f4a4b3 | 2017-02-24 18:56:49 +0000 | [diff] [blame] | 36 | |
Zachary Turner | 7f6a7a3 | 2017-03-06 23:42:14 +0000 | [diff] [blame] | 37 | auto Buffer = llvm::MemoryBuffer::getFileSlice(Path, Size, Offset, Private); |
| 38 | if (!Buffer) |
| 39 | return nullptr; |
| 40 | return std::shared_ptr<DataBufferLLVM>( |
| 41 | new DataBufferLLVM(std::move(*Buffer))); |
| 42 | } |
| 43 | |
| 44 | std::shared_ptr<DataBufferLLVM> |
| 45 | DataBufferLLVM::CreateFromPath(const llvm::Twine &Path, bool NullTerminate, bool Private) { |
| 46 | // If the file resides non-locally, pass the volatile flag so that we don't |
| 47 | // mmap it. |
| 48 | if (!Private) |
| 49 | Private = !llvm::sys::fs::is_local(Path); |
| 50 | |
| 51 | auto Buffer = llvm::MemoryBuffer::getFile(Path, -1, NullTerminate, Private); |
Zachary Turner | 3f4a4b3 | 2017-02-24 18:56:49 +0000 | [diff] [blame] | 52 | if (!Buffer) |
| 53 | return nullptr; |
| 54 | return std::shared_ptr<DataBufferLLVM>( |
| 55 | new DataBufferLLVM(std::move(*Buffer))); |
| 56 | } |
| 57 | |
Zachary Turner | 3f4a4b3 | 2017-02-24 18:56:49 +0000 | [diff] [blame] | 58 | uint8_t *DataBufferLLVM::GetBytes() { |
| 59 | return const_cast<uint8_t *>(GetBuffer()); |
| 60 | } |
| 61 | |
| 62 | const uint8_t *DataBufferLLVM::GetBytes() const { return GetBuffer(); } |
| 63 | |
| 64 | lldb::offset_t DataBufferLLVM::GetByteSize() const { |
| 65 | return Buffer->getBufferSize(); |
| 66 | } |
| 67 | |
| 68 | const uint8_t *DataBufferLLVM::GetBuffer() const { |
| 69 | return reinterpret_cast<const uint8_t *>(Buffer->getBufferStart()); |
| 70 | } |