Home | History | Annotate | Download | only in Core
      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 //----------------------------------------------------------------------
     26 DataBufferHeap::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 //----------------------------------------------------------------------
     37 DataBufferHeap::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 //----------------------------------------------------------------------
     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 //----------------------------------------------------------------------
     78 uint64_t
     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 //----------------------------------------------------------------------
     89 uint64_t
     90 DataBufferHeap::SetByteSize (uint64_t new_size)
     91 {
     92     m_data.resize(new_size);
     93     return m_data.size();
     94 }
     95 
     96 void
     97 DataBufferHeap::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 
    106 void
    107 DataBufferHeap::Clear()
    108 {
    109     buffer_t empty;
    110     m_data.swap(empty);
    111 }
    112