Path: blob/main/contrib/llvm-project/lldb/source/Utility/DataBufferHeap.cpp
39587 views
//===-- DataBufferHeap.cpp ------------------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#include "lldb/Utility/DataBufferHeap.h"91011using namespace lldb_private;1213// Default constructor14DataBufferHeap::DataBufferHeap() : m_data() {}1516// Initialize this class with "n" characters and fill the buffer with "ch".17DataBufferHeap::DataBufferHeap(lldb::offset_t n, uint8_t ch) : m_data() {18if (n < m_data.max_size())19m_data.assign(n, ch);20}2122// Initialize this class with a copy of the "n" bytes from the "bytes" buffer.23DataBufferHeap::DataBufferHeap(const void *src, lldb::offset_t src_len)24: m_data() {25CopyData(src, src_len);26}2728DataBufferHeap::DataBufferHeap(const DataBuffer &data_buffer) : m_data() {29CopyData(data_buffer.GetBytes(), data_buffer.GetByteSize());30}3132// Virtual destructor since this class inherits from a pure virtual base class.33DataBufferHeap::~DataBufferHeap() = default;3435// Return a const pointer to the bytes owned by this object, or nullptr if the36// object contains no bytes.37const uint8_t *DataBufferHeap::GetBytesImpl() const {38return (m_data.empty() ? nullptr : m_data.data());39}4041// Return the number of bytes this object currently contains.42uint64_t DataBufferHeap::GetByteSize() const { return m_data.size(); }4344// Sets the number of bytes that this object should be able to contain. This45// can be used prior to copying data into the buffer.46uint64_t DataBufferHeap::SetByteSize(uint64_t new_size) {47if (new_size < m_data.max_size())48m_data.resize(new_size);49return m_data.size();50}5152void DataBufferHeap::CopyData(const void *src, uint64_t src_len) {53const uint8_t *src_u8 = static_cast<const uint8_t *>(src);54if (src && src_len > 0)55m_data.assign(src_u8, src_u8 + src_len);56else57m_data.clear();58}5960void DataBufferHeap::AppendData(const void *src, uint64_t src_len) {61m_data.insert(m_data.end(), static_cast<const uint8_t *>(src),62static_cast<const uint8_t *>(src) + src_len);63}6465void DataBufferHeap::Clear() {66buffer_t empty;67m_data.swap(empty);68}6970char DataBuffer::ID;71char WritableDataBuffer::ID;72char DataBufferUnowned::ID;73char DataBufferHeap::ID;747576