DataBuffer.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. #ifndef LLDB_UTILITY_DATABUFFER_H
  9. #define LLDB_UTILITY_DATABUFFER_H
  10. #if defined(__cplusplus)
  11. #include <cstdint>
  12. #include <cstring>
  13. #include "lldb/lldb-types.h"
  14. #include "llvm/ADT/ArrayRef.h"
  15. namespace lldb_private {
  16. /// \class DataBuffer DataBuffer.h "lldb/Core/DataBuffer.h"
  17. /// A pure virtual protocol class for abstracted data buffers.
  18. ///
  19. /// DataBuffer is an abstract class that gets packaged into a shared pointer
  20. /// that can use to implement various ways to store data (on the heap, memory
  21. /// mapped, cached inferior memory). It gets used by DataExtractor so many
  22. /// DataExtractor objects can share the same data and sub-ranges of that
  23. /// shared data, and the last object that contains a reference to the shared
  24. /// data will free it.
  25. ///
  26. /// Subclasses can implement as many different constructors or member
  27. /// functions that allow data to be stored in the object's buffer prior to
  28. /// handing the shared data to clients that use these buffers.
  29. ///
  30. /// All subclasses must override all of the pure virtual functions as they are
  31. /// used by clients to access the data. Having a common interface allows
  32. /// different ways of storing data, yet using it in one common way.
  33. ///
  34. /// This class currently expects all data to be available without any extra
  35. /// calls being made, but we can modify it to optionally get data on demand
  36. /// with some extra function calls to load the data before it gets accessed.
  37. class DataBuffer {
  38. public:
  39. /// Destructor
  40. ///
  41. /// The destructor is virtual as other classes will inherit from this class
  42. /// and be downcast to the DataBuffer pure virtual interface. The virtual
  43. /// destructor ensures that destructing the base class will destruct the
  44. /// class that inherited from it correctly.
  45. virtual ~DataBuffer() {}
  46. /// Get a pointer to the data.
  47. ///
  48. /// \return
  49. /// A pointer to the bytes owned by this object, or NULL if the
  50. /// object contains no bytes.
  51. virtual uint8_t *GetBytes() = 0;
  52. /// Get a const pointer to the data.
  53. ///
  54. /// \return
  55. /// A const pointer to the bytes owned by this object, or NULL
  56. /// if the object contains no bytes.
  57. virtual const uint8_t *GetBytes() const = 0;
  58. /// Get the number of bytes in the data buffer.
  59. ///
  60. /// \return
  61. /// The number of bytes this object currently contains.
  62. virtual lldb::offset_t GetByteSize() const = 0;
  63. llvm::ArrayRef<uint8_t> GetData() const {
  64. return llvm::ArrayRef<uint8_t>(GetBytes(), GetByteSize());
  65. }
  66. llvm::MutableArrayRef<uint8_t> GetData() {
  67. return llvm::MutableArrayRef<uint8_t>(GetBytes(), GetByteSize());
  68. }
  69. };
  70. class DataBufferUnowned : public DataBuffer {
  71. public:
  72. DataBufferUnowned(uint8_t *bytes, lldb::offset_t size)
  73. : m_bytes(bytes), m_size(size) {}
  74. uint8_t *GetBytes() override { return m_bytes; }
  75. const uint8_t *GetBytes() const override { return m_bytes; }
  76. lldb::offset_t GetByteSize() const override { return m_size; }
  77. private:
  78. uint8_t *m_bytes;
  79. lldb::offset_t m_size;
  80. };
  81. } // namespace lldb_private
  82. #endif /// #if defined(__cplusplus)
  83. #endif // LLDB_UTILITY_DATABUFFER_H