1 //===-- DataBufferHeap.cpp --------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "lldb/Utility/DataBufferHeap.h"
14 // Other libraries and framework includes
17 using namespace lldb_private;
19 //----------------------------------------------------------------------
20 // Default constructor
21 //----------------------------------------------------------------------
22 DataBufferHeap::DataBufferHeap() : m_data() {}
24 //----------------------------------------------------------------------
25 // Initialize this class with "n" characters and fill the buffer
27 //----------------------------------------------------------------------
28 DataBufferHeap::DataBufferHeap(lldb::offset_t n, uint8_t ch) : m_data() {
29 if (n < m_data.max_size())
33 //----------------------------------------------------------------------
34 // Initialize this class with a copy of the "n" bytes from the "bytes"
36 //----------------------------------------------------------------------
37 DataBufferHeap::DataBufferHeap(const void *src, lldb::offset_t src_len)
39 CopyData(src, src_len);
42 //----------------------------------------------------------------------
43 // Virtual destructor since this class inherits from a pure virtual
45 //----------------------------------------------------------------------
46 DataBufferHeap::~DataBufferHeap() = default;
48 //----------------------------------------------------------------------
49 // Return a pointer to the bytes owned by this object, or nullptr if
50 // the object contains no bytes.
51 //----------------------------------------------------------------------
52 uint8_t *DataBufferHeap::GetBytes() {
53 return (m_data.empty() ? nullptr : m_data.data());
56 //----------------------------------------------------------------------
57 // Return a const pointer to the bytes owned by this object, or nullptr
58 // if the object contains no bytes.
59 //----------------------------------------------------------------------
60 const uint8_t *DataBufferHeap::GetBytes() const {
61 return (m_data.empty() ? nullptr : m_data.data());
64 //----------------------------------------------------------------------
65 // Return the number of bytes this object currently contains.
66 //----------------------------------------------------------------------
67 uint64_t DataBufferHeap::GetByteSize() const { return m_data.size(); }
69 //----------------------------------------------------------------------
70 // Sets the number of bytes that this object should be able to
71 // contain. This can be used prior to copying data into the buffer.
72 //----------------------------------------------------------------------
73 uint64_t DataBufferHeap::SetByteSize(uint64_t new_size) {
74 m_data.resize(new_size);
78 void DataBufferHeap::CopyData(const void *src, uint64_t src_len) {
79 const uint8_t *src_u8 = (const uint8_t *)src;
80 if (src && src_len > 0)
81 m_data.assign(src_u8, src_u8 + src_len);
86 void DataBufferHeap::AppendData(const void *src, uint64_t src_len) {
87 m_data.insert(m_data.end(), (const uint8_t *)src,
88 (const uint8_t *)src + src_len);
91 void DataBufferHeap::Clear() {