blob: aa1c3d37ae62c831d4c78447072b34a1a425a672 [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- 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
Zachary Turner666cc0b2017-03-04 01:30:05 +000010#include "lldb/Utility/DataBufferHeap.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000011
Eugene Zelenko34ede342016-03-03 00:51:40 +000012// C Includes
13// C++ Includes
14// Other libraries and framework includes
15// Project includes
16
Chris Lattner30fdc8d2010-06-08 16:52:24 +000017using namespace lldb_private;
18
19//----------------------------------------------------------------------
20// Default constructor
21//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +000022DataBufferHeap::DataBufferHeap() : m_data() {}
Chris Lattner30fdc8d2010-06-08 16:52:24 +000023
24//----------------------------------------------------------------------
25// Initialize this class with "n" characters and fill the buffer
26// with "ch".
27//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +000028DataBufferHeap::DataBufferHeap(lldb::offset_t n, uint8_t ch) : m_data() {
29 if (n < m_data.max_size())
30 m_data.assign(n, ch);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000031}
32
33//----------------------------------------------------------------------
34// Initialize this class with a copy of the "n" bytes from the "bytes"
35// buffer.
36//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +000037DataBufferHeap::DataBufferHeap(const void *src, lldb::offset_t src_len)
38 : m_data() {
39 CopyData(src, src_len);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000040}
41
42//----------------------------------------------------------------------
43// Virtual destructor since this class inherits from a pure virtual
44// base class.
45//----------------------------------------------------------------------
Eugene Zelenko34ede342016-03-03 00:51:40 +000046DataBufferHeap::~DataBufferHeap() = default;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000047
48//----------------------------------------------------------------------
Eugene Zelenko34ede342016-03-03 00:51:40 +000049// Return a pointer to the bytes owned by this object, or nullptr if
Chris Lattner30fdc8d2010-06-08 16:52:24 +000050// the object contains no bytes.
51//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +000052uint8_t *DataBufferHeap::GetBytes() {
53 return (m_data.empty() ? nullptr : m_data.data());
Chris Lattner30fdc8d2010-06-08 16:52:24 +000054}
55
56//----------------------------------------------------------------------
Eugene Zelenko34ede342016-03-03 00:51:40 +000057// Return a const pointer to the bytes owned by this object, or nullptr
Chris Lattner30fdc8d2010-06-08 16:52:24 +000058// if the object contains no bytes.
59//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +000060const uint8_t *DataBufferHeap::GetBytes() const {
61 return (m_data.empty() ? nullptr : m_data.data());
Chris Lattner30fdc8d2010-06-08 16:52:24 +000062}
63
64//----------------------------------------------------------------------
65// Return the number of bytes this object currently contains.
66//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +000067uint64_t DataBufferHeap::GetByteSize() const { return m_data.size(); }
Chris Lattner30fdc8d2010-06-08 16:52:24 +000068
Chris Lattner30fdc8d2010-06-08 16:52:24 +000069//----------------------------------------------------------------------
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//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +000073uint64_t DataBufferHeap::SetByteSize(uint64_t new_size) {
74 m_data.resize(new_size);
75 return m_data.size();
Chris Lattner30fdc8d2010-06-08 16:52:24 +000076}
77
Kate Stoneb9c1b512016-09-06 20:57:50 +000078void 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);
82 else
83 m_data.clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +000084}
85
Kate Stoneb9c1b512016-09-06 20:57:50 +000086void 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);
Greg Clayton27407872014-07-12 00:24:33 +000089}
90
Kate Stoneb9c1b512016-09-06 20:57:50 +000091void DataBufferHeap::Clear() {
92 buffer_t empty;
93 m_data.swap(empty);
Greg Clayton57ee3062013-07-11 22:46:58 +000094}