xref: /freebsd/contrib/llvm-project/lldb/include/lldb/Utility/DataBufferHeap.h (revision 6be3386466ab79a84b48429ae66244f21526d3df)
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 DataBuffer {
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   /// Destructor.
58   ///
59   /// Virtual destructor since this class inherits from a pure virtual base
60   /// class #DataBuffer.
61   ~DataBufferHeap() override;
62 
63   /// \copydoc DataBuffer::GetBytes()
64   uint8_t *GetBytes() override;
65 
66   /// \copydoc DataBuffer::GetBytes() const
67   const uint8_t *GetBytes() const override;
68 
69   /// \copydoc DataBuffer::GetByteSize() const
70   lldb::offset_t GetByteSize() const override;
71 
72   /// Set the number of bytes in the data buffer.
73   ///
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.
77   ///
78   /// \param[in] byte_size
79   ///     The new size in bytes that this data buffer should attempt
80   ///     to resize itself to.
81   ///
82   /// \return
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);
86 
87   /// Makes a copy of the \a src_len bytes in \a src.
88   ///
89   /// Copies the data in \a src into an internal buffer.
90   ///
91   /// \param[in] src
92   ///     A pointer to the data to copy.
93   ///
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()); }
98 
99   void AppendData(const void *src, uint64_t src_len);
100 
101   void Clear();
102 
103 private:
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
108 };
109 
110 } // namespace lldb_private
111 
112 #endif // LLDB_UTILITY_DATABUFFERHEAP_H
113