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