Raphael Isemann | 8081428 | 2020-01-24 08:23:27 +0100 | [diff] [blame] | 1 | //===-- DataBufferHeap.cpp ------------------------------------------------===// |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
Zachary Turner | 666cc0b | 2017-03-04 01:30:05 +0000 | [diff] [blame] | 9 | #include "lldb/Utility/DataBufferHeap.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 10 | |
Eugene Zelenko | 34ede34 | 2016-03-03 00:51:40 +0000 | [diff] [blame] | 11 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 12 | using namespace lldb_private; |
| 13 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 14 | // Default constructor |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 15 | DataBufferHeap::DataBufferHeap() : m_data() {} |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 16 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 17 | // Initialize this class with "n" characters and fill the buffer with "ch". |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 18 | DataBufferHeap::DataBufferHeap(lldb::offset_t n, uint8_t ch) : m_data() { |
| 19 | if (n < m_data.max_size()) |
| 20 | m_data.assign(n, ch); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 21 | } |
| 22 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 23 | // Initialize this class with a copy of the "n" bytes from the "bytes" buffer. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 24 | DataBufferHeap::DataBufferHeap(const void *src, lldb::offset_t src_len) |
| 25 | : m_data() { |
| 26 | CopyData(src, src_len); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 27 | } |
| 28 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 29 | // Virtual destructor since this class inherits from a pure virtual base class. |
Eugene Zelenko | 34ede34 | 2016-03-03 00:51:40 +0000 | [diff] [blame] | 30 | DataBufferHeap::~DataBufferHeap() = default; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 31 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 32 | // Return a pointer to the bytes owned by this object, or nullptr if the object |
| 33 | // contains no bytes. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 34 | uint8_t *DataBufferHeap::GetBytes() { |
| 35 | return (m_data.empty() ? nullptr : m_data.data()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 36 | } |
| 37 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 38 | // Return a const pointer to the bytes owned by this object, or nullptr if the |
| 39 | // object contains no bytes. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 40 | const uint8_t *DataBufferHeap::GetBytes() const { |
| 41 | return (m_data.empty() ? nullptr : m_data.data()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 42 | } |
| 43 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 44 | // Return the number of bytes this object currently contains. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 45 | uint64_t DataBufferHeap::GetByteSize() const { return m_data.size(); } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 46 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 47 | // 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 Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 49 | uint64_t DataBufferHeap::SetByteSize(uint64_t new_size) { |
| 50 | m_data.resize(new_size); |
| 51 | return m_data.size(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 52 | } |
| 53 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 54 | void DataBufferHeap::CopyData(const void *src, uint64_t src_len) { |
Jonas Devlieghere | 24374ae | 2019-05-23 05:12:11 +0000 | [diff] [blame] | 55 | const uint8_t *src_u8 = static_cast<const uint8_t *>(src); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 56 | if (src && src_len > 0) |
| 57 | m_data.assign(src_u8, src_u8 + src_len); |
| 58 | else |
| 59 | m_data.clear(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 60 | } |
| 61 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 62 | void DataBufferHeap::AppendData(const void *src, uint64_t src_len) { |
Jonas Devlieghere | 24374ae | 2019-05-23 05:12:11 +0000 | [diff] [blame] | 63 | m_data.insert(m_data.end(), static_cast<const uint8_t *>(src), |
| 64 | static_cast<const uint8_t *>(src) + src_len); |
Greg Clayton | 2740787 | 2014-07-12 00:24:33 +0000 | [diff] [blame] | 65 | } |
| 66 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 67 | void DataBufferHeap::Clear() { |
| 68 | buffer_t empty; |
| 69 | m_data.swap(empty); |
Greg Clayton | 57ee306 | 2013-07-11 22:46:58 +0000 | [diff] [blame] | 70 | } |