]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/include/lldb/Utility/DataBufferHeap.h
Merge clang 7.0.1 and several follow-up changes
[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"    // for offset_t
15 #include "llvm/ADT/StringRef.h" // for StringRef
16
17 #include <cstdint> // for uint8_t, uint64_t
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.
94   ///
95   /// @param[in] byte_size
96   ///     The new size in bytes that this data buffer should attempt
97   ///     to resize itself to.
98   ///
99   /// @return
100   ///     The size in bytes after that this heap buffer was
101   ///     successfully resized to.
102   //------------------------------------------------------------------
103   lldb::offset_t SetByteSize(lldb::offset_t byte_size);
104
105   //------------------------------------------------------------------
106   /// Makes a copy of the \a src_len bytes in \a src.
107   ///
108   /// Copies the data in \a src into an internal buffer.
109   ///
110   /// @param[in] src
111   ///     A pointer to the data to copy.
112   ///
113   /// @param[in] src_len
114   ///     The number of bytes in \a src to copy.
115   //------------------------------------------------------------------
116   void CopyData(const void *src, lldb::offset_t src_len);
117   void CopyData(llvm::StringRef src) { CopyData(src.data(), src.size()); }
118
119   void AppendData(const void *src, uint64_t src_len);
120
121   void Clear();
122
123 private:
124   //------------------------------------------------------------------
125   // This object uses a std::vector<uint8_t> to store its data. This takes care
126   // of free the data when the object is deleted.
127   //------------------------------------------------------------------
128   typedef std::vector<uint8_t> buffer_t; ///< Buffer type
129   buffer_t m_data; ///< The heap based buffer where data is stored
130 };
131
132 } // namespace lldb_private
133
134 #endif // liblldb_DataBufferHeap_h_