]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/include/lldb/Utility/DataBufferHeap.h
Merge llvm, clang, compiler-rt, libc++, libunwind, lld, lldb and openmp
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lldb / include / lldb / Utility / DataBufferHeap.h
1 //===-- DataBufferHeap.h ----------------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #ifndef liblldb_DataBufferHeap_h_
11 #define liblldb_DataBufferHeap_h_
12
13 #include "lldb/Utility/DataBuffer.h"
14 #include "lldb/lldb-types.h"
15 #include "llvm/ADT/StringRef.h"
16
17 #include <cstdint>
18 #include <vector>
19
20 namespace lldb_private {
21
22 //----------------------------------------------------------------------
23 /// @class DataBufferHeap DataBufferHeap.h "lldb/Core/DataBufferHeap.h"
24 /// A subclass of DataBuffer that stores a data buffer on the heap.
25 ///
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
31 /// optimal.
32 //----------------------------------------------------------------------
33 class DataBufferHeap : public DataBuffer {
34 public:
35   //------------------------------------------------------------------
36   /// Default constructor
37   ///
38   /// Initializes the heap based buffer with no bytes.
39   //------------------------------------------------------------------
40   DataBufferHeap();
41
42   //------------------------------------------------------------------
43   /// Construct with size \a n and fill with \a ch.
44   ///
45   /// Initialize this class with \a n bytes and fills the buffer with \a ch.
46   ///
47   /// @param[in] n
48   ///     The number of bytes that heap based buffer should contain.
49   ///
50   /// @param[in] ch
51   ///     The character to use when filling the buffer initially.
52   //------------------------------------------------------------------
53   DataBufferHeap(lldb::offset_t n, uint8_t ch);
54
55   //------------------------------------------------------------------
56   /// Construct by making a copy of \a src_len bytes from \a src.
57   ///
58   /// @param[in] src
59   ///     A pointer to the data to copy.
60   ///
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);
65
66   //------------------------------------------------------------------
67   /// Destructor.
68   ///
69   /// Virtual destructor since this class inherits from a pure virtual base
70   /// class #DataBuffer.
71   //------------------------------------------------------------------
72   ~DataBufferHeap() override;
73
74   //------------------------------------------------------------------
75   /// @copydoc DataBuffer::GetBytes()
76   //------------------------------------------------------------------
77   uint8_t *GetBytes() override;
78
79   //------------------------------------------------------------------
80   /// @copydoc DataBuffer::GetBytes() const
81   //------------------------------------------------------------------
82   const uint8_t *GetBytes() const override;
83
84   //------------------------------------------------------------------
85   /// @copydoc DataBuffer::GetByteSize() const
86   //------------------------------------------------------------------
87   lldb::offset_t GetByteSize() const override;
88
89   //------------------------------------------------------------------
90   /// Set the number of bytes in the data buffer.
91   ///
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.
95   ///
96   /// @param[in] byte_size
97   ///     The new size in bytes that this data buffer should attempt
98   ///     to resize itself to.
99   ///
100   /// @return
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);
105
106   //------------------------------------------------------------------
107   /// Makes a copy of the \a src_len bytes in \a src.
108   ///
109   /// Copies the data in \a src into an internal buffer.
110   ///
111   /// @param[in] src
112   ///     A pointer to the data to copy.
113   ///
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()); }
119
120   void AppendData(const void *src, uint64_t src_len);
121
122   void Clear();
123
124 private:
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
131 };
132
133 } // namespace lldb_private
134
135 #endif // liblldb_DataBufferHeap_h_