1 //===-- DataBufferHeap.h ----------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef liblldb_DataBufferHeap_h_
11 #define liblldb_DataBufferHeap_h_
13 #include "lldb/Utility/DataBuffer.h"
14 #include "lldb/lldb-types.h" // for offset_t
15 #include "llvm/ADT/StringRef.h" // for StringRef
17 #include <cstdint> // for uint8_t, uint64_t
20 namespace lldb_private {
22 //----------------------------------------------------------------------
23 /// @class DataBufferHeap DataBufferHeap.h "lldb/Core/DataBufferHeap.h"
24 /// @brief A subclass of DataBuffer that stores a data buffer on the heap.
26 /// This class keeps its data in a heap based buffer that is owned by
27 /// the object. This class is best used to store chunks of data that
28 /// are created or read from sources that can't intelligently and lazily
29 /// fault new data pages in. Large amounts of data that comes from files
30 /// should probably use DataBufferLLVM, which can intelligently determine
31 /// when memory mapping is optimal.
32 //----------------------------------------------------------------------
33 class DataBufferHeap : public DataBuffer {
35 //------------------------------------------------------------------
36 /// Default constructor
38 /// Initializes the heap based buffer with no bytes.
39 //------------------------------------------------------------------
42 //------------------------------------------------------------------
43 /// Construct with size \a n and fill with \a ch.
45 /// Initialize this class with \a n bytes and fills the buffer with
49 /// The number of bytes that heap based buffer should contain.
52 /// The character to use when filling the buffer initially.
53 //------------------------------------------------------------------
54 DataBufferHeap(lldb::offset_t n, uint8_t ch);
56 //------------------------------------------------------------------
57 /// Construct by making a copy of \a src_len bytes from \a src.
60 /// A pointer to the data to copy.
62 /// @param[in] src_len
63 /// The number of bytes in \a src to copy.
64 //------------------------------------------------------------------
65 DataBufferHeap(const void *src, lldb::offset_t src_len);
67 //------------------------------------------------------------------
70 /// Virtual destructor since this class inherits from a pure virtual
71 /// base class #DataBuffer.
72 //------------------------------------------------------------------
73 ~DataBufferHeap() override;
75 //------------------------------------------------------------------
76 /// @copydoc DataBuffer::GetBytes()
77 //------------------------------------------------------------------
78 uint8_t *GetBytes() override;
80 //------------------------------------------------------------------
81 /// @copydoc DataBuffer::GetBytes() const
82 //------------------------------------------------------------------
83 const uint8_t *GetBytes() const override;
85 //------------------------------------------------------------------
86 /// @copydoc DataBuffer::GetByteSize() const
87 //------------------------------------------------------------------
88 lldb::offset_t GetByteSize() const override;
90 //------------------------------------------------------------------
91 /// Set the number of bytes in the data buffer.
93 /// Sets the number of bytes that this object should be able to
94 /// contain. This can be used prior to copying data into the buffer.
96 /// @param[in] byte_size
97 /// The new size in bytes that this data buffer should attempt
98 /// to resize itself to.
101 /// The size in bytes after that this heap buffer was
102 /// successfully resized to.
103 //------------------------------------------------------------------
104 lldb::offset_t SetByteSize(lldb::offset_t byte_size);
106 //------------------------------------------------------------------
107 /// Makes a copy of the \a src_len bytes in \a src.
109 /// Copies the data in \a src into an internal buffer.
112 /// A pointer to the data to copy.
114 /// @param[in] src_len
115 /// The number of bytes in \a src to copy.
116 //------------------------------------------------------------------
117 void CopyData(const void *src, lldb::offset_t src_len);
118 void CopyData(llvm::StringRef src) { CopyData(src.data(), src.size()); }
120 void AppendData(const void *src, uint64_t src_len);
125 //------------------------------------------------------------------
126 // This object uses a std::vector<uint8_t> to store its data. This
127 // takes care of free the data when the object is deleted.
128 //------------------------------------------------------------------
129 typedef std::vector<uint8_t> buffer_t; ///< Buffer type
130 buffer_t m_data; ///< The heap based buffer where data is stored
133 } // namespace lldb_private
135 #endif // liblldb_DataBufferHeap_h_