Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 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/Core/DataBufferHeap.h" |
| 11 | |
| 12 | using namespace lldb_private; |
| 13 | |
| 14 | //---------------------------------------------------------------------- |
| 15 | // Default constructor |
| 16 | //---------------------------------------------------------------------- |
| 17 | DataBufferHeap::DataBufferHeap () : |
| 18 | m_data() |
| 19 | { |
| 20 | } |
| 21 | |
| 22 | //---------------------------------------------------------------------- |
| 23 | // Initialize this class with "n" characters and fill the buffer |
| 24 | // with "ch". |
| 25 | //---------------------------------------------------------------------- |
Greg Clayton | faac111 | 2013-03-14 18:31:44 +0000 | [diff] [blame] | 26 | DataBufferHeap::DataBufferHeap (lldb::offset_t n, uint8_t ch) : |
Greg Clayton | 46a4426 | 2013-07-24 18:17:35 +0000 | [diff] [blame] | 27 | m_data() |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 28 | { |
Greg Clayton | 46a4426 | 2013-07-24 18:17:35 +0000 | [diff] [blame] | 29 | if (n < m_data.max_size()) |
| 30 | m_data.assign (n, ch); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | //---------------------------------------------------------------------- |
| 34 | // Initialize this class with a copy of the "n" bytes from the "bytes" |
| 35 | // buffer. |
| 36 | //---------------------------------------------------------------------- |
Greg Clayton | faac111 | 2013-03-14 18:31:44 +0000 | [diff] [blame] | 37 | DataBufferHeap::DataBufferHeap (const void *src, lldb::offset_t src_len) : |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 38 | m_data() |
| 39 | { |
| 40 | CopyData (src, src_len); |
| 41 | } |
| 42 | |
| 43 | //---------------------------------------------------------------------- |
| 44 | // Virtual destructor since this class inherits from a pure virtual |
| 45 | // base class. |
| 46 | //---------------------------------------------------------------------- |
| 47 | DataBufferHeap::~DataBufferHeap () |
| 48 | { |
| 49 | } |
| 50 | |
| 51 | //---------------------------------------------------------------------- |
| 52 | // Return a pointer to the bytes owned by this object, or NULL if |
| 53 | // the object contains no bytes. |
| 54 | //---------------------------------------------------------------------- |
| 55 | uint8_t * |
| 56 | DataBufferHeap::GetBytes () |
| 57 | { |
| 58 | if (m_data.empty()) |
| 59 | return NULL; |
| 60 | return &m_data[0]; |
| 61 | } |
| 62 | |
| 63 | //---------------------------------------------------------------------- |
| 64 | // Return a const pointer to the bytes owned by this object, or NULL |
| 65 | // if the object contains no bytes. |
| 66 | //---------------------------------------------------------------------- |
| 67 | const uint8_t * |
| 68 | DataBufferHeap::GetBytes () const |
| 69 | { |
| 70 | if (m_data.empty()) |
| 71 | return NULL; |
| 72 | return &m_data[0]; |
| 73 | } |
| 74 | |
| 75 | //---------------------------------------------------------------------- |
| 76 | // Return the number of bytes this object currently contains. |
| 77 | //---------------------------------------------------------------------- |
Greg Clayton | faac111 | 2013-03-14 18:31:44 +0000 | [diff] [blame] | 78 | uint64_t |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 79 | DataBufferHeap::GetByteSize () const |
| 80 | { |
| 81 | return m_data.size(); |
| 82 | } |
| 83 | |
| 84 | |
| 85 | //---------------------------------------------------------------------- |
| 86 | // Sets the number of bytes that this object should be able to |
| 87 | // contain. This can be used prior to copying data into the buffer. |
| 88 | //---------------------------------------------------------------------- |
Greg Clayton | faac111 | 2013-03-14 18:31:44 +0000 | [diff] [blame] | 89 | uint64_t |
| 90 | DataBufferHeap::SetByteSize (uint64_t new_size) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 91 | { |
| 92 | m_data.resize(new_size); |
| 93 | return m_data.size(); |
| 94 | } |
| 95 | |
| 96 | void |
Greg Clayton | faac111 | 2013-03-14 18:31:44 +0000 | [diff] [blame] | 97 | DataBufferHeap::CopyData (const void *src, uint64_t src_len) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 98 | { |
| 99 | const uint8_t *src_u8 = (const uint8_t *)src; |
| 100 | if (src && src_len > 0) |
| 101 | m_data.assign (src_u8, src_u8 + src_len); |
| 102 | else |
| 103 | m_data.clear(); |
| 104 | } |
| 105 | |
Greg Clayton | 57ee306 | 2013-07-11 22:46:58 +0000 | [diff] [blame] | 106 | void |
Greg Clayton | 2740787 | 2014-07-12 00:24:33 +0000 | [diff] [blame^] | 107 | DataBufferHeap::AppendData (const void *src, uint64_t src_len) |
| 108 | { |
| 109 | m_data.insert(m_data.end(), (uint8_t *)src, (uint8_t *)src + src_len); |
| 110 | } |
| 111 | |
| 112 | void |
Greg Clayton | 57ee306 | 2013-07-11 22:46:58 +0000 | [diff] [blame] | 113 | DataBufferHeap::Clear() |
| 114 | { |
| 115 | buffer_t empty; |
| 116 | m_data.swap(empty); |
| 117 | } |