LLDB mainline
DataBuffer.h
Go to the documentation of this file.
1//===-- DataBuffer.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_DATABUFFER_H
10#define LLDB_UTILITY_DATABUFFER_H
11#if defined(__cplusplus)
12
13#include <cstdint>
14#include <cstring>
15
16#include "lldb/lldb-types.h"
17
18#include "llvm/ADT/ArrayRef.h"
19
20namespace lldb_private {
21
22/// \class DataBuffer DataBuffer.h "lldb/Core/DataBuffer.h"
23/// A pure virtual protocol class for abstracted read only data buffers.
24///
25/// DataBuffer is an abstract class that gets packaged into a shared
26/// pointer that can use to implement various ways to store data (on the heap,
27/// memory mapped, cached inferior memory). It gets used by DataExtractor so
28/// many DataExtractor objects can share the same data and sub-ranges of that
29/// shared data, and the last object that contains a reference to the shared
30/// data will free it.
31///
32/// Subclasses can implement as many different constructors or member
33/// functions that allow data to be stored in the object's buffer prior to
34/// handing the shared data to clients that use these buffers.
35///
36/// All subclasses must override all of the pure virtual functions as they are
37/// used by clients to access the data. Having a common interface allows
38/// different ways of storing data, yet using it in one common way.
39///
40/// This class currently expects all data to be available without any extra
41/// calls being made, but we can modify it to optionally get data on demand
42/// with some extra function calls to load the data before it gets accessed.
43class DataBuffer {
44public:
45 virtual ~DataBuffer() = default;
46
47 /// Get the number of bytes in the data buffer.
48 ///
49 /// \return
50 /// The number of bytes this object currently contains.
51 virtual lldb::offset_t GetByteSize() const = 0;
52
53 /// Get a const pointer to the data.
54 ///
55 /// \return
56 /// A const pointer to the bytes owned by this object, or NULL
57 /// if the object contains no bytes.
58 const uint8_t *GetBytes() const { return GetBytesImpl(); }
59
60 llvm::ArrayRef<uint8_t> GetData() const {
61 return llvm::ArrayRef<uint8_t>(GetBytes(), GetByteSize());
62 }
63
64 /// LLVM RTTI support.
65 /// {
66 static char ID;
67 virtual bool isA(const void *ClassID) const { return ClassID == &ID; }
68 static bool classof(const DataBuffer *data_buffer) {
69 return data_buffer->isA(&ID);
70 }
71 /// }
72
73protected:
74 /// Get a const pointer to the data.
75 ///
76 /// \return
77 /// A const pointer to the bytes owned by this object, or NULL
78 /// if the object contains no bytes.
79 virtual const uint8_t *GetBytesImpl() const = 0;
80};
81
82/// \class DataBuffer DataBuffer.h "lldb/Core/DataBuffer.h"
83/// A pure virtual protocol class for abstracted writable data buffers.
84///
85/// DataBuffer is an abstract class that gets packaged into a shared pointer
86/// that can use to implement various ways to store data (on the heap, memory
87/// mapped, cached inferior memory). It gets used by DataExtractor so many
88/// DataExtractor objects can share the same data and sub-ranges of that
89/// shared data, and the last object that contains a reference to the shared
90/// data will free it.
91class WritableDataBuffer : public DataBuffer {
92public:
93 /// Destructor
94 ///
95 /// The destructor is virtual as other classes will inherit from this class
96 /// and be downcast to the DataBuffer pure virtual interface. The virtual
97 /// destructor ensures that destructing the base class will destruct the
98 /// class that inherited from it correctly.
99 ~WritableDataBuffer() override = default;
100
101 using DataBuffer::GetBytes;
102 using DataBuffer::GetData;
103
104 /// Get a pointer to the data.
105 ///
106 /// \return
107 /// A pointer to the bytes owned by this object, or NULL if the
108 /// object contains no bytes.
109 uint8_t *GetBytes() { return const_cast<uint8_t *>(GetBytesImpl()); }
110
111 llvm::MutableArrayRef<uint8_t> GetData() {
112 return llvm::MutableArrayRef<uint8_t>(GetBytes(), GetByteSize());
113 }
114
115 /// LLVM RTTI support.
116 /// {
117 static char ID;
118 bool isA(const void *ClassID) const override {
119 return ClassID == &ID || DataBuffer::isA(ClassID);
120 }
121 static bool classof(const DataBuffer *data_buffer) {
122 return data_buffer->isA(&ID);
123 }
124 /// }
125};
126
127class DataBufferUnowned : public WritableDataBuffer {
128public:
129 DataBufferUnowned(uint8_t *bytes, lldb::offset_t size)
130 : m_bytes(bytes), m_size(size) {}
131
132 const uint8_t *GetBytesImpl() const override { return m_bytes; }
133 lldb::offset_t GetByteSize() const override { return m_size; }
134
135 /// LLVM RTTI support.
136 /// {
137 static char ID;
138 bool isA(const void *ClassID) const override {
139 return ClassID == &ID || WritableDataBuffer::isA(ClassID);
140 }
141 static bool classof(const DataBuffer *data_buffer) {
142 return data_buffer->isA(&ID);
143 }
144 /// }
145private:
146 uint8_t *m_bytes;
147 lldb::offset_t m_size;
148};
149
150} // namespace lldb_private
151
152#endif /// #if defined(__cplusplus)
153#endif // LLDB_UTILITY_DATABUFFER_H
static char ID
A class that represents a running process on the host machine.
Definition: SBAttachInfo.h:14
uint64_t offset_t
Definition: lldb-types.h:87