1 //===-- DataBufferHeap.h ----------------------------------------*- C++ -*-===// 2 // 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 6 // 7 //===----------------------------------------------------------------------===// 8 9 #ifndef LLDB_UTILITY_DATABUFFERHEAP_H 10 #define LLDB_UTILITY_DATABUFFERHEAP_H 11 12 #include "lldb/Utility/DataBuffer.h" 13 #include "lldb/lldb-types.h" 14 #include "llvm/ADT/StringRef.h" 15 16 #include <cstdint> 17 #include <vector> 18 19 namespace lldb_private { 20 21 /// \class DataBufferHeap DataBufferHeap.h "lldb/Core/DataBufferHeap.h" 22 /// A subclass of DataBuffer that stores a data buffer on the heap. 23 /// 24 /// This class keeps its data in a heap based buffer that is owned by the 25 /// object. This class is best used to store chunks of data that are created 26 /// or read from sources that can't intelligently and lazily fault new data 27 /// pages in. Large amounts of data that comes from files should probably use 28 /// DataBufferLLVM, which can intelligently determine when memory mapping is 29 /// optimal. 30 class DataBufferHeap : public WritableDataBuffer { 31 public: 32 /// Default constructor 33 /// 34 /// Initializes the heap based buffer with no bytes. 35 DataBufferHeap(); 36 37 /// Construct with size \a n and fill with \a ch. 38 /// 39 /// Initialize this class with \a n bytes and fills the buffer with \a ch. 40 /// 41 /// \param[in] n 42 /// The number of bytes that heap based buffer should contain. 43 /// 44 /// \param[in] ch 45 /// The character to use when filling the buffer initially. 46 DataBufferHeap(lldb::offset_t n, uint8_t ch); 47 48 /// Construct by making a copy of \a src_len bytes from \a src. 49 /// 50 /// \param[in] src 51 /// A pointer to the data to copy. 52 /// 53 /// \param[in] src_len 54 /// The number of bytes in \a src to copy. 55 DataBufferHeap(const void *src, lldb::offset_t src_len); 56 57 /// Construct by making a copy of a DataBuffer. 58 /// 59 /// \param[in] data_buffer 60 /// A read only data buffer to copy. 61 DataBufferHeap(const DataBuffer &data_buffer); 62 63 /// Destructor. 64 /// 65 /// Virtual destructor since this class inherits from a pure virtual base 66 /// class #DataBuffer. 67 ~DataBufferHeap() override; 68 69 /// \copydoc DataBuffer::GetBytes() const 70 const uint8_t *GetBytesImpl() const override; 71 72 /// \copydoc DataBuffer::GetByteSize() const 73 lldb::offset_t GetByteSize() const override; 74 75 /// Set the number of bytes in the data buffer. 76 /// 77 /// Sets the number of bytes that this object should be able to contain. 78 /// This can be used prior to copying data into the buffer. Note that this 79 /// zero-initializes up to \p byte_size bytes. 80 /// 81 /// \param[in] byte_size 82 /// The new size in bytes that this data buffer should attempt 83 /// to resize itself to. 84 /// 85 /// \return 86 /// The size in bytes after this heap buffer was resized. If 87 /// the resize failed the size will remain unchanged. 88 lldb::offset_t SetByteSize(lldb::offset_t byte_size); 89 90 /// Makes a copy of the \a src_len bytes in \a src. 91 /// 92 /// Copies the data in \a src into an internal buffer. 93 /// 94 /// \param[in] src 95 /// A pointer to the data to copy. 96 /// 97 /// \param[in] src_len 98 /// The number of bytes in \a src to copy. 99 void CopyData(const void *src, lldb::offset_t src_len); 100 void CopyData(llvm::StringRef src) { CopyData(src.data(), src.size()); } 101 102 void AppendData(const void *src, uint64_t src_len); 103 104 void Clear(); 105 106 /// LLVM RTTI support. 107 /// { 108 static char ID; 109 bool isA(const void *ClassID) const override { 110 return ClassID == &ID || WritableDataBuffer::isA(ClassID); 111 } 112 static bool classof(const DataBuffer *data_buffer) { 113 return data_buffer->isA(&ID); 114 } 115 /// } 116 117 private: 118 // This object uses a std::vector<uint8_t> to store its data. This takes care 119 // of free the data when the object is deleted. 120 typedef std::vector<uint8_t> buffer_t; ///< Buffer type 121 buffer_t m_data; ///< The heap based buffer where data is stored 122 }; 123 124 } // namespace lldb_private 125 126 #endif // LLDB_UTILITY_DATABUFFERHEAP_H 127