blob: 74893767d14d315ed479bad9bd1f43101fdc1236 [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
10#include "lldb/Core/DataBufferHeap.h"
11
12using namespace lldb_private;
13
14//----------------------------------------------------------------------
15// Default constructor
16//----------------------------------------------------------------------
17DataBufferHeap::DataBufferHeap () :
18 m_data()
19{
20}
21
22//----------------------------------------------------------------------
23// Initialize this class with "n" characters and fill the buffer
24// with "ch".
25//----------------------------------------------------------------------
Greg Claytonfaac1112013-03-14 18:31:44 +000026DataBufferHeap::DataBufferHeap (lldb::offset_t n, uint8_t ch) :
Chris Lattner30fdc8d2010-06-08 16:52:24 +000027 m_data(n, ch)
28{
29}
30
31//----------------------------------------------------------------------
32// Initialize this class with a copy of the "n" bytes from the "bytes"
33// buffer.
34//----------------------------------------------------------------------
Greg Claytonfaac1112013-03-14 18:31:44 +000035DataBufferHeap::DataBufferHeap (const void *src, lldb::offset_t src_len) :
Chris Lattner30fdc8d2010-06-08 16:52:24 +000036 m_data()
37{
38 CopyData (src, src_len);
39}
40
41//----------------------------------------------------------------------
42// Virtual destructor since this class inherits from a pure virtual
43// base class.
44//----------------------------------------------------------------------
45DataBufferHeap::~DataBufferHeap ()
46{
47}
48
49//----------------------------------------------------------------------
50// Return a pointer to the bytes owned by this object, or NULL if
51// the object contains no bytes.
52//----------------------------------------------------------------------
53uint8_t *
54DataBufferHeap::GetBytes ()
55{
56 if (m_data.empty())
57 return NULL;
58 return &m_data[0];
59}
60
61//----------------------------------------------------------------------
62// Return a const pointer to the bytes owned by this object, or NULL
63// if the object contains no bytes.
64//----------------------------------------------------------------------
65const uint8_t *
66DataBufferHeap::GetBytes () const
67{
68 if (m_data.empty())
69 return NULL;
70 return &m_data[0];
71}
72
73//----------------------------------------------------------------------
74// Return the number of bytes this object currently contains.
75//----------------------------------------------------------------------
Greg Claytonfaac1112013-03-14 18:31:44 +000076uint64_t
Chris Lattner30fdc8d2010-06-08 16:52:24 +000077DataBufferHeap::GetByteSize () const
78{
79 return m_data.size();
80}
81
82
83//----------------------------------------------------------------------
84// Sets the number of bytes that this object should be able to
85// contain. This can be used prior to copying data into the buffer.
86//----------------------------------------------------------------------
Greg Claytonfaac1112013-03-14 18:31:44 +000087uint64_t
88DataBufferHeap::SetByteSize (uint64_t new_size)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000089{
90 m_data.resize(new_size);
91 return m_data.size();
92}
93
94void
Greg Claytonfaac1112013-03-14 18:31:44 +000095DataBufferHeap::CopyData (const void *src, uint64_t src_len)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000096{
97 const uint8_t *src_u8 = (const uint8_t *)src;
98 if (src && src_len > 0)
99 m_data.assign (src_u8, src_u8 + src_len);
100 else
101 m_data.clear();
102}
103
Greg Clayton57ee3062013-07-11 22:46:58 +0000104void
105DataBufferHeap::Clear()
106{
107 buffer_t empty;
108 m_data.swap(empty);
109}