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