blob: 3aa0b6b0ac408b95c5921d50181a3a26d4ae4bc2 [file] [log] [blame]
Raphael Isemann80814282020-01-24 08:23:27 +01001//===-- DataBufferHeap.cpp ------------------------------------------------===//
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006//
7//===----------------------------------------------------------------------===//
8
Zachary Turner666cc0b2017-03-04 01:30:05 +00009#include "lldb/Utility/DataBufferHeap.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000010
Eugene Zelenko34ede342016-03-03 00:51:40 +000011
Chris Lattner30fdc8d2010-06-08 16:52:24 +000012using namespace lldb_private;
13
Chris Lattner30fdc8d2010-06-08 16:52:24 +000014// Default constructor
Kate Stoneb9c1b512016-09-06 20:57:50 +000015DataBufferHeap::DataBufferHeap() : m_data() {}
Chris Lattner30fdc8d2010-06-08 16:52:24 +000016
Adrian Prantl05097242018-04-30 16:49:04 +000017// Initialize this class with "n" characters and fill the buffer with "ch".
Kate Stoneb9c1b512016-09-06 20:57:50 +000018DataBufferHeap::DataBufferHeap(lldb::offset_t n, uint8_t ch) : m_data() {
19 if (n < m_data.max_size())
20 m_data.assign(n, ch);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000021}
22
Adrian Prantl05097242018-04-30 16:49:04 +000023// Initialize this class with a copy of the "n" bytes from the "bytes" buffer.
Kate Stoneb9c1b512016-09-06 20:57:50 +000024DataBufferHeap::DataBufferHeap(const void *src, lldb::offset_t src_len)
25 : m_data() {
26 CopyData(src, src_len);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000027}
28
Adrian Prantl05097242018-04-30 16:49:04 +000029// Virtual destructor since this class inherits from a pure virtual base class.
Eugene Zelenko34ede342016-03-03 00:51:40 +000030DataBufferHeap::~DataBufferHeap() = default;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000031
Adrian Prantl05097242018-04-30 16:49:04 +000032// Return a pointer to the bytes owned by this object, or nullptr if the object
33// contains no bytes.
Kate Stoneb9c1b512016-09-06 20:57:50 +000034uint8_t *DataBufferHeap::GetBytes() {
35 return (m_data.empty() ? nullptr : m_data.data());
Chris Lattner30fdc8d2010-06-08 16:52:24 +000036}
37
Adrian Prantl05097242018-04-30 16:49:04 +000038// Return a const pointer to the bytes owned by this object, or nullptr if the
39// object contains no bytes.
Kate Stoneb9c1b512016-09-06 20:57:50 +000040const uint8_t *DataBufferHeap::GetBytes() const {
41 return (m_data.empty() ? nullptr : m_data.data());
Chris Lattner30fdc8d2010-06-08 16:52:24 +000042}
43
Chris Lattner30fdc8d2010-06-08 16:52:24 +000044// Return the number of bytes this object currently contains.
Kate Stoneb9c1b512016-09-06 20:57:50 +000045uint64_t DataBufferHeap::GetByteSize() const { return m_data.size(); }
Chris Lattner30fdc8d2010-06-08 16:52:24 +000046
Adrian Prantl05097242018-04-30 16:49:04 +000047// Sets the number of bytes that this object should be able to contain. This
48// can be used prior to copying data into the buffer.
Kate Stoneb9c1b512016-09-06 20:57:50 +000049uint64_t DataBufferHeap::SetByteSize(uint64_t new_size) {
50 m_data.resize(new_size);
51 return m_data.size();
Chris Lattner30fdc8d2010-06-08 16:52:24 +000052}
53
Kate Stoneb9c1b512016-09-06 20:57:50 +000054void DataBufferHeap::CopyData(const void *src, uint64_t src_len) {
Jonas Devlieghere24374ae2019-05-23 05:12:11 +000055 const uint8_t *src_u8 = static_cast<const uint8_t *>(src);
Kate Stoneb9c1b512016-09-06 20:57:50 +000056 if (src && src_len > 0)
57 m_data.assign(src_u8, src_u8 + src_len);
58 else
59 m_data.clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +000060}
61
Kate Stoneb9c1b512016-09-06 20:57:50 +000062void DataBufferHeap::AppendData(const void *src, uint64_t src_len) {
Jonas Devlieghere24374ae2019-05-23 05:12:11 +000063 m_data.insert(m_data.end(), static_cast<const uint8_t *>(src),
64 static_cast<const uint8_t *>(src) + src_len);
Greg Clayton27407872014-07-12 00:24:33 +000065}
66
Kate Stoneb9c1b512016-09-06 20:57:50 +000067void DataBufferHeap::Clear() {
68 buffer_t empty;
69 m_data.swap(empty);
Greg Clayton57ee3062013-07-11 22:46:58 +000070}