1*0b57cec5SDimitry Andric //===-- DataBufferHeap.cpp ------------------------------------------------===//
2*0b57cec5SDimitry Andric //
3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*0b57cec5SDimitry Andric //
7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
8*0b57cec5SDimitry Andric
9*0b57cec5SDimitry Andric #include "lldb/Utility/DataBufferHeap.h"
10*0b57cec5SDimitry Andric
11*0b57cec5SDimitry Andric
12*0b57cec5SDimitry Andric using namespace lldb_private;
13*0b57cec5SDimitry Andric
14*0b57cec5SDimitry Andric // Default constructor
DataBufferHeap()15*0b57cec5SDimitry Andric DataBufferHeap::DataBufferHeap() : m_data() {}
16*0b57cec5SDimitry Andric
17*0b57cec5SDimitry Andric // Initialize this class with "n" characters and fill the buffer with "ch".
DataBufferHeap(lldb::offset_t n,uint8_t ch)18*0b57cec5SDimitry Andric DataBufferHeap::DataBufferHeap(lldb::offset_t n, uint8_t ch) : m_data() {
19*0b57cec5SDimitry Andric if (n < m_data.max_size())
20*0b57cec5SDimitry Andric m_data.assign(n, ch);
21*0b57cec5SDimitry Andric }
22*0b57cec5SDimitry Andric
23*0b57cec5SDimitry Andric // Initialize this class with a copy of the "n" bytes from the "bytes" buffer.
DataBufferHeap(const void * src,lldb::offset_t src_len)24*0b57cec5SDimitry Andric DataBufferHeap::DataBufferHeap(const void *src, lldb::offset_t src_len)
25*0b57cec5SDimitry Andric : m_data() {
26*0b57cec5SDimitry Andric CopyData(src, src_len);
27*0b57cec5SDimitry Andric }
28*0b57cec5SDimitry Andric
29*0b57cec5SDimitry Andric // Virtual destructor since this class inherits from a pure virtual base class.
30*0b57cec5SDimitry Andric DataBufferHeap::~DataBufferHeap() = default;
31*0b57cec5SDimitry Andric
32*0b57cec5SDimitry Andric // Return a pointer to the bytes owned by this object, or nullptr if the object
33*0b57cec5SDimitry Andric // contains no bytes.
GetBytes()34*0b57cec5SDimitry Andric uint8_t *DataBufferHeap::GetBytes() {
35*0b57cec5SDimitry Andric return (m_data.empty() ? nullptr : m_data.data());
36*0b57cec5SDimitry Andric }
37*0b57cec5SDimitry Andric
38*0b57cec5SDimitry Andric // Return a const pointer to the bytes owned by this object, or nullptr if the
39*0b57cec5SDimitry Andric // object contains no bytes.
GetBytes() const40*0b57cec5SDimitry Andric const uint8_t *DataBufferHeap::GetBytes() const {
41*0b57cec5SDimitry Andric return (m_data.empty() ? nullptr : m_data.data());
42*0b57cec5SDimitry Andric }
43*0b57cec5SDimitry Andric
44*0b57cec5SDimitry Andric // Return the number of bytes this object currently contains.
GetByteSize() const45*0b57cec5SDimitry Andric uint64_t DataBufferHeap::GetByteSize() const { return m_data.size(); }
46*0b57cec5SDimitry Andric
47*0b57cec5SDimitry Andric // Sets the number of bytes that this object should be able to contain. This
48*0b57cec5SDimitry Andric // can be used prior to copying data into the buffer.
SetByteSize(uint64_t new_size)49*0b57cec5SDimitry Andric uint64_t DataBufferHeap::SetByteSize(uint64_t new_size) {
50*0b57cec5SDimitry Andric m_data.resize(new_size);
51*0b57cec5SDimitry Andric return m_data.size();
52*0b57cec5SDimitry Andric }
53*0b57cec5SDimitry Andric
CopyData(const void * src,uint64_t src_len)54*0b57cec5SDimitry Andric void DataBufferHeap::CopyData(const void *src, uint64_t src_len) {
55*0b57cec5SDimitry Andric const uint8_t *src_u8 = static_cast<const uint8_t *>(src);
56*0b57cec5SDimitry Andric if (src && src_len > 0)
57*0b57cec5SDimitry Andric m_data.assign(src_u8, src_u8 + src_len);
58*0b57cec5SDimitry Andric else
59*0b57cec5SDimitry Andric m_data.clear();
60*0b57cec5SDimitry Andric }
61*0b57cec5SDimitry Andric
AppendData(const void * src,uint64_t src_len)62*0b57cec5SDimitry Andric void DataBufferHeap::AppendData(const void *src, uint64_t src_len) {
63*0b57cec5SDimitry Andric m_data.insert(m_data.end(), static_cast<const uint8_t *>(src),
64*0b57cec5SDimitry Andric static_cast<const uint8_t *>(src) + src_len);
65*0b57cec5SDimitry Andric }
66*0b57cec5SDimitry Andric
Clear()67*0b57cec5SDimitry Andric void DataBufferHeap::Clear() {
68*0b57cec5SDimitry Andric buffer_t empty;
69*0b57cec5SDimitry Andric m_data.swap(empty);
70*0b57cec5SDimitry Andric }
71