1 //===-- DataBufferHeap.h ----------------------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 #ifndef liblldb_DataBufferHeap_h_
10 #define liblldb_DataBufferHeap_h_
12 #include "lldb/Utility/DataBuffer.h"
13 #include "lldb/lldb-types.h"
14 #include "llvm/ADT/StringRef.h"
19 namespace lldb_private {
21 /// \class DataBufferHeap DataBufferHeap.h "lldb/Core/DataBufferHeap.h"
22 /// A subclass of DataBuffer that stores a data buffer on the heap.
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
30 class DataBufferHeap : public DataBuffer {
32 /// Default constructor
34 /// Initializes the heap based buffer with no bytes.
37 /// Construct with size \a n and fill with \a ch.
39 /// Initialize this class with \a n bytes and fills the buffer with \a ch.
42 /// The number of bytes that heap based buffer should contain.
45 /// The character to use when filling the buffer initially.
46 DataBufferHeap(lldb::offset_t n, uint8_t ch);
48 /// Construct by making a copy of \a src_len bytes from \a src.
51 /// A pointer to the data to copy.
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);
59 /// Virtual destructor since this class inherits from a pure virtual base
60 /// class #DataBuffer.
61 ~DataBufferHeap() override;
63 /// \copydoc DataBuffer::GetBytes()
64 uint8_t *GetBytes() override;
66 /// \copydoc DataBuffer::GetBytes() const
67 const uint8_t *GetBytes() const override;
69 /// \copydoc DataBuffer::GetByteSize() const
70 lldb::offset_t GetByteSize() const override;
72 /// Set the number of bytes in the data buffer.
74 /// Sets the number of bytes that this object should be able to contain.
75 /// This can be used prior to copying data into the buffer. Note that this
76 /// zero-initializes up to \p byte_size bytes.
78 /// \param[in] byte_size
79 /// The new size in bytes that this data buffer should attempt
80 /// to resize itself to.
83 /// The size in bytes after that this heap buffer was
84 /// successfully resized to.
85 lldb::offset_t SetByteSize(lldb::offset_t byte_size);
87 /// Makes a copy of the \a src_len bytes in \a src.
89 /// Copies the data in \a src into an internal buffer.
92 /// A pointer to the data to copy.
94 /// \param[in] src_len
95 /// The number of bytes in \a src to copy.
96 void CopyData(const void *src, lldb::offset_t src_len);
97 void CopyData(llvm::StringRef src) { CopyData(src.data(), src.size()); }
99 void AppendData(const void *src, uint64_t src_len);
104 // This object uses a std::vector<uint8_t> to store its data. This takes care
105 // of free the data when the object is deleted.
106 typedef std::vector<uint8_t> buffer_t; ///< Buffer type
107 buffer_t m_data; ///< The heap based buffer where data is stored
110 } // namespace lldb_private
112 #endif // liblldb_DataBufferHeap_h_