blob: 984b36e54153e2426f5a450917c4da040e50c6b5 [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) :
Greg Clayton46a44262013-07-24 18:17:35 +000027 m_data()
Chris Lattner30fdc8d2010-06-08 16:52:24 +000028{
Greg Clayton46a44262013-07-24 18:17:35 +000029 if (n < m_data.max_size())
30 m_data.assign (n, ch);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000031}
32
33//----------------------------------------------------------------------
34// Initialize this class with a copy of the "n" bytes from the "bytes"
35// buffer.
36//----------------------------------------------------------------------
Greg Claytonfaac1112013-03-14 18:31:44 +000037DataBufferHeap::DataBufferHeap (const void *src, lldb::offset_t src_len) :
Chris Lattner30fdc8d2010-06-08 16:52:24 +000038 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//----------------------------------------------------------------------
47DataBufferHeap::~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//----------------------------------------------------------------------
55uint8_t *
56DataBufferHeap::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//----------------------------------------------------------------------
67const uint8_t *
68DataBufferHeap::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 Claytonfaac1112013-03-14 18:31:44 +000078uint64_t
Chris Lattner30fdc8d2010-06-08 16:52:24 +000079DataBufferHeap::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 Claytonfaac1112013-03-14 18:31:44 +000089uint64_t
90DataBufferHeap::SetByteSize (uint64_t new_size)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000091{
92 m_data.resize(new_size);
93 return m_data.size();
94}
95
96void
Greg Claytonfaac1112013-03-14 18:31:44 +000097DataBufferHeap::CopyData (const void *src, uint64_t src_len)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000098{
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 Clayton57ee3062013-07-11 22:46:58 +0000106void
Greg Clayton27407872014-07-12 00:24:33 +0000107DataBufferHeap::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
112void
Greg Clayton57ee3062013-07-11 22:46:58 +0000113DataBufferHeap::Clear()
114{
115 buffer_t empty;
116 m_data.swap(empty);
117}