1 //===-- 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
10 #include "lldb/Utility/DataBufferHeap.h"
11
12
13 using namespace lldb_private;
14
15 //----------------------------------------------------------------------
16 // Default constructor
17 //----------------------------------------------------------------------
DataBufferHeap()18 DataBufferHeap::DataBufferHeap() : m_data() {}
19
20 //----------------------------------------------------------------------
21 // Initialize this class with "n" characters and fill the buffer with "ch".
22 //----------------------------------------------------------------------
DataBufferHeap(lldb::offset_t n,uint8_t ch)23 DataBufferHeap::DataBufferHeap(lldb::offset_t n, uint8_t ch) : m_data() {
24 if (n < m_data.max_size())
25 m_data.assign(n, ch);
26 }
27
28 //----------------------------------------------------------------------
29 // Initialize this class with a copy of the "n" bytes from the "bytes" buffer.
30 //----------------------------------------------------------------------
DataBufferHeap(const void * src,lldb::offset_t src_len)31 DataBufferHeap::DataBufferHeap(const void *src, lldb::offset_t src_len)
32 : m_data() {
33 CopyData(src, src_len);
34 }
35
36 //----------------------------------------------------------------------
37 // Virtual destructor since this class inherits from a pure virtual base class.
38 //----------------------------------------------------------------------
39 DataBufferHeap::~DataBufferHeap() = default;
40
41 //----------------------------------------------------------------------
42 // Return a pointer to the bytes owned by this object, or nullptr if the object
43 // contains no bytes.
44 //----------------------------------------------------------------------
GetBytes()45 uint8_t *DataBufferHeap::GetBytes() {
46 return (m_data.empty() ? nullptr : m_data.data());
47 }
48
49 //----------------------------------------------------------------------
50 // Return a const pointer to the bytes owned by this object, or nullptr if the
51 // object contains no bytes.
52 //----------------------------------------------------------------------
GetBytes() const53 const uint8_t *DataBufferHeap::GetBytes() const {
54 return (m_data.empty() ? nullptr : m_data.data());
55 }
56
57 //----------------------------------------------------------------------
58 // Return the number of bytes this object currently contains.
59 //----------------------------------------------------------------------
GetByteSize() const60 uint64_t DataBufferHeap::GetByteSize() const { return m_data.size(); }
61
62 //----------------------------------------------------------------------
63 // Sets the number of bytes that this object should be able to contain. This
64 // can be used prior to copying data into the buffer.
65 //----------------------------------------------------------------------
SetByteSize(uint64_t new_size)66 uint64_t DataBufferHeap::SetByteSize(uint64_t new_size) {
67 m_data.resize(new_size);
68 return m_data.size();
69 }
70
CopyData(const void * src,uint64_t src_len)71 void DataBufferHeap::CopyData(const void *src, uint64_t src_len) {
72 const uint8_t *src_u8 = (const uint8_t *)src;
73 if (src && src_len > 0)
74 m_data.assign(src_u8, src_u8 + src_len);
75 else
76 m_data.clear();
77 }
78
AppendData(const void * src,uint64_t src_len)79 void DataBufferHeap::AppendData(const void *src, uint64_t src_len) {
80 m_data.insert(m_data.end(), (const uint8_t *)src,
81 (const uint8_t *)src + src_len);
82 }
83
Clear()84 void DataBufferHeap::Clear() {
85 buffer_t empty;
86 m_data.swap(empty);
87 }
88