1 //===-- DataBufferHeap.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/DataBufferHeap.h" 11 12 // C Includes 13 // C++ Includes 14 // Other libraries and framework includes 15 // Project includes 16 17 using namespace lldb_private; 18 19 //---------------------------------------------------------------------- 20 // Default constructor 21 //---------------------------------------------------------------------- 22 DataBufferHeap::DataBufferHeap() : m_data() {} 23 24 //---------------------------------------------------------------------- 25 // Initialize this class with "n" characters and fill the buffer with "ch". 26 //---------------------------------------------------------------------- 27 DataBufferHeap::DataBufferHeap(lldb::offset_t n, uint8_t ch) : m_data() { 28 if (n < m_data.max_size()) 29 m_data.assign(n, ch); 30 } 31 32 //---------------------------------------------------------------------- 33 // Initialize this class with a copy of the "n" bytes from the "bytes" buffer. 34 //---------------------------------------------------------------------- 35 DataBufferHeap::DataBufferHeap(const void *src, lldb::offset_t src_len) 36 : m_data() { 37 CopyData(src, src_len); 38 } 39 40 //---------------------------------------------------------------------- 41 // Virtual destructor since this class inherits from a pure virtual base class. 42 //---------------------------------------------------------------------- 43 DataBufferHeap::~DataBufferHeap() = default; 44 45 //---------------------------------------------------------------------- 46 // Return a pointer to the bytes owned by this object, or nullptr if the object 47 // contains no bytes. 48 //---------------------------------------------------------------------- 49 uint8_t *DataBufferHeap::GetBytes() { 50 return (m_data.empty() ? nullptr : m_data.data()); 51 } 52 53 //---------------------------------------------------------------------- 54 // Return a const pointer to the bytes owned by this object, or nullptr if the 55 // object contains no bytes. 56 //---------------------------------------------------------------------- 57 const uint8_t *DataBufferHeap::GetBytes() const { 58 return (m_data.empty() ? nullptr : m_data.data()); 59 } 60 61 //---------------------------------------------------------------------- 62 // Return the number of bytes this object currently contains. 63 //---------------------------------------------------------------------- 64 uint64_t DataBufferHeap::GetByteSize() const { return m_data.size(); } 65 66 //---------------------------------------------------------------------- 67 // Sets the number of bytes that this object should be able to contain. This 68 // can be used prior to copying data into the buffer. 69 //---------------------------------------------------------------------- 70 uint64_t DataBufferHeap::SetByteSize(uint64_t new_size) { 71 m_data.resize(new_size); 72 return m_data.size(); 73 } 74 75 void DataBufferHeap::CopyData(const void *src, uint64_t src_len) { 76 const uint8_t *src_u8 = (const uint8_t *)src; 77 if (src && src_len > 0) 78 m_data.assign(src_u8, src_u8 + src_len); 79 else 80 m_data.clear(); 81 } 82 83 void DataBufferHeap::AppendData(const void *src, uint64_t src_len) { 84 m_data.insert(m_data.end(), (const uint8_t *)src, 85 (const uint8_t *)src + src_len); 86 } 87 88 void DataBufferHeap::Clear() { 89 buffer_t empty; 90 m_data.swap(empty); 91 } 92