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