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"
15 #include "llvm/ADT/StringRef.h"
20 namespace lldb_private {
22 //----------------------------------------------------------------------
23 /// @class DataBufferHeap DataBufferHeap.h "lldb/Core/DataBufferHeap.h"
24 /// 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 the
27 /// object. This class is best used to store chunks of data that are created
28 /// or read from sources that can't intelligently and lazily fault new data
29 /// pages in. Large amounts of data that comes from files should probably use
30 /// DataBufferLLVM, which can intelligently determine when memory mapping is
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 \a ch.
48 /// The number of bytes that heap based buffer should contain.
51 /// The character to use when filling the buffer initially.
52 //------------------------------------------------------------------
53 DataBufferHeap(lldb::offset_t n, uint8_t ch);
55 //------------------------------------------------------------------
56 /// Construct by making a copy of \a src_len bytes from \a src.
59 /// A pointer to the data to copy.
61 /// @param[in] src_len
62 /// The number of bytes in \a src to copy.
63 //------------------------------------------------------------------
64 DataBufferHeap(const void *src, lldb::offset_t src_len);
66 //------------------------------------------------------------------
69 /// Virtual destructor since this class inherits from a pure virtual base
70 /// class #DataBuffer.
71 //------------------------------------------------------------------
72 ~DataBufferHeap() override;
74 //------------------------------------------------------------------
75 /// @copydoc DataBuffer::GetBytes()
76 //------------------------------------------------------------------
77 uint8_t *GetBytes() override;
79 //------------------------------------------------------------------
80 /// @copydoc DataBuffer::GetBytes() const
81 //------------------------------------------------------------------
82 const uint8_t *GetBytes() const override;
84 //------------------------------------------------------------------
85 /// @copydoc DataBuffer::GetByteSize() const
86 //------------------------------------------------------------------
87 lldb::offset_t GetByteSize() const override;
89 //------------------------------------------------------------------
90 /// Set the number of bytes in the data buffer.
92 /// Sets the number of bytes that this object should be able to contain.
93 /// This can be used prior to copying data into the buffer. Note that this
94 /// zero-initializes up to \p byte_size bytes.
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 takes care
127 // 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_