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 liblldb_DataBuffer_h_
10#define liblldb_DataBuffer_h_
11#if defined(__cplusplus)
12
13#include <stdint.h>
14#include <string.h>
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 data buffers.
24///
25/// DataBuffer is an abstract class that gets packaged into a shared pointer
26/// that can use to implement various ways to store data (on the heap, memory
27/// mapped, cached inferior memory). It gets used by DataExtractor so many
28/// 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  /// Destructor
46  ///
47  /// The destructor is virtual as other classes will inherit from this class
48  /// and be downcast to the DataBuffer pure virtual interface. The virtual
49  /// destructor ensures that destructing the base class will destruct the
50  /// class that inherited from it correctly.
51  virtual ~DataBuffer() {}
52
53  /// Get a pointer to the data.
54  ///
55  /// \return
56  ///     A pointer to the bytes owned by this object, or NULL if the
57  ///     object contains no bytes.
58  virtual uint8_t *GetBytes() = 0;
59
60  /// Get a const pointer to the data.
61  ///
62  /// \return
63  ///     A const pointer to the bytes owned by this object, or NULL
64  ///     if the object contains no bytes.
65  virtual const uint8_t *GetBytes() const = 0;
66
67  /// Get the number of bytes in the data buffer.
68  ///
69  /// \return
70  ///     The number of bytes this object currently contains.
71  virtual lldb::offset_t GetByteSize() const = 0;
72
73  llvm::ArrayRef<uint8_t> GetData() const {
74    return llvm::ArrayRef<uint8_t>(GetBytes(), GetByteSize());
75  }
76
77  llvm::MutableArrayRef<uint8_t> GetData() {
78    return llvm::MutableArrayRef<uint8_t>(GetBytes(), GetByteSize());
79  }
80};
81
82} // namespace lldb_private
83
84#endif /// #if defined(__cplusplus)
85#endif /// lldb_DataBuffer_h_
86