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 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//---------------------------------------------------------------------- 26DataBufferHeap::DataBufferHeap (lldb::offset_t n, uint8_t ch) : 27 m_data() 28{ 29 if (n < m_data.max_size()) 30 m_data.assign (n, ch); 31} 32 33//---------------------------------------------------------------------- 34// Initialize this class with a copy of the "n" bytes from the "bytes" 35// buffer. 36//---------------------------------------------------------------------- 37DataBufferHeap::DataBufferHeap (const void *src, lldb::offset_t src_len) : 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//---------------------------------------------------------------------- 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//---------------------------------------------------------------------- 78uint64_t 79DataBufferHeap::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//---------------------------------------------------------------------- 89uint64_t 90DataBufferHeap::SetByteSize (uint64_t new_size) 91{ 92 m_data.resize(new_size); 93 return m_data.size(); 94} 95 96void 97DataBufferHeap::CopyData (const void *src, uint64_t src_len) 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 106void 107DataBufferHeap::Clear() 108{ 109 buffer_t empty; 110 m_data.swap(empty); 111} 112