DataBufferMemoryMap.h revision fe6dc6e241c52822710380cec0931351a1d7b2d3
1//===-- DataBufferMemoryMap.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_DataBufferMemoryMap_h_
11#define liblldb_DataBufferMemoryMap_h_
12#if defined(__cplusplus)
13
14
15#include "lldb/lldb-private.h"
16#include "lldb/Core/DataBuffer.h"
17#include "lldb/Core/Error.h"
18#include <string>
19
20namespace lldb_private {
21
22//----------------------------------------------------------------------
23/// @class DataBufferMemoryMap DataBufferMemoryMap.h "lldb/Core/DataBufferMemoryMap.h"
24/// @brief A subclass of DataBuffer that memory maps data.
25///
26/// This class memory maps data and stores any needed data for the
27/// memory mapping in its internal state. Memory map requests are not
28/// required to have any alignment or size constraints, this class will
29/// work around any host OS issues regarding such things.
30///
31/// This class is designed to allow pages to be faulted in as needed and
32/// works well data from large files that won't be accessed all at once.
33//----------------------------------------------------------------------
34class DataBufferMemoryMap : public DataBuffer
35{
36public:
37    //------------------------------------------------------------------
38    /// Default Constructor
39    //------------------------------------------------------------------
40    DataBufferMemoryMap ();
41
42    //------------------------------------------------------------------
43    /// Destructor.
44    ///
45    /// Virtual destructor since this class inherits from a pure virtual
46    /// base class #DataBuffer.
47    //------------------------------------------------------------------
48    virtual
49    ~DataBufferMemoryMap ();
50
51    //------------------------------------------------------------------
52    /// Reverts this object to an empty state by unmapping any memory
53    /// that is currently owned.
54    //------------------------------------------------------------------
55    void
56    Clear ();
57
58    //------------------------------------------------------------------
59    /// @copydoc DataBuffer::GetBytes()
60    //------------------------------------------------------------------
61    virtual uint8_t *
62    GetBytes ();
63
64    //------------------------------------------------------------------
65    /// @copydoc DataBuffer::GetBytes() const
66    //------------------------------------------------------------------
67    virtual const uint8_t *
68    GetBytes () const;
69
70    //------------------------------------------------------------------
71    /// @copydoc DataBuffer::GetByteSize() const
72    //------------------------------------------------------------------
73    virtual lldb::offset_t
74    GetByteSize () const;
75
76    //------------------------------------------------------------------
77    /// Error get accessor.
78    ///
79    /// @return
80    ///     A const reference to Error object in case memory mapping
81    ///     fails.
82    //------------------------------------------------------------------
83    const Error &
84    GetError() const;
85
86    //------------------------------------------------------------------
87    /// Memory map all or part of a file.
88    ///
89    /// Memory map \a length bytes from \a file starting \a offset
90    /// bytes into the file. If \a length is set to \c SIZE_MAX,
91    /// then map as many bytes as possible.
92    ///
93    /// @param[in] file
94    ///     The file specification from which to map data.
95    ///
96    /// @param[in] offset
97    ///     The offset in bytes from the beginning of the file where
98    ///     memory mapping should begin.
99    ///
100    /// @param[in] length
101    ///     The size in bytes that should be mapped starting \a offset
102    ///     bytes into the file. If \a length is \c SIZE_MAX, map
103    ///     as many bytes as possible.
104    ///
105    /// @return
106    ///     The number of bytes mapped starting from the \a offset.
107    //------------------------------------------------------------------
108    size_t
109    MemoryMapFromFileSpec (const FileSpec* file,
110                           lldb::offset_t offset = 0,
111                           lldb::offset_t length = SIZE_MAX,
112                           bool writeable = false);
113
114    //------------------------------------------------------------------
115    /// Memory map all or part of a file.
116    ///
117    /// Memory map \a length bytes from an opened file descriptor \a fd
118    /// starting \a offset bytes into the file. If \a length is set to
119    /// \c SIZE_MAX, then map as many bytes as possible.
120    ///
121    /// @param[in] fd
122    ///     The posix file descriptor for an already opened file
123    ///     from which to map data.
124    ///
125    /// @param[in] offset
126    ///     The offset in bytes from the beginning of the file where
127    ///     memory mapping should begin.
128    ///
129    /// @param[in] length
130    ///     The size in bytes that should be mapped starting \a offset
131    ///     bytes into the file. If \a length is \c SIZE_MAX, map
132    ///     as many bytes as possible.
133    ///
134    /// @return
135    ///     The number of bytes mapped starting from the \a offset.
136    //------------------------------------------------------------------
137    size_t
138    MemoryMapFromFileDescriptor (int fd,
139                                 lldb::offset_t offset,
140                                 lldb::offset_t length,
141                                 bool write,
142                                 bool fd_is_file);
143
144protected:
145    //------------------------------------------------------------------
146    // Classes that inherit from DataBufferMemoryMap can see and modify these
147    //------------------------------------------------------------------
148    uint8_t * m_mmap_addr;  ///< The actual pointer that was returned from \c mmap()
149    size_t m_mmap_size;     ///< The actual number of bytes that were mapped when \c mmap() was called
150    uint8_t *m_data;        ///< The data the user requested somewhere within the memory mapped data.
151    lldb::offset_t m_size;  ///< The size of the data the user got when data was requested
152
153private:
154    DISALLOW_COPY_AND_ASSIGN (DataBufferMemoryMap);
155};
156
157} // namespace lldb_private
158
159#endif  // #if defined(__cplusplus)
160#endif  // liblldb_DataBufferMemoryMap_h_
161