1 //===-- Memory.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_Memory_h_
11 #define liblldb_Memory_h_
12 
13 // C Includes
14 // C++ Includes
15 #include <map>
16 #include <vector>
17 
18 // Other libraries and framework includes
19 //#include "llvm/ADT/BitVector.h"
20 
21 // Project includes
22 #include "lldb/lldb-private.h"
23 #include "lldb/Host/Mutex.h"
24 
25 namespace lldb_private {
26     //----------------------------------------------------------------------
27     // A class to track memory that was read from a live process between
28     // runs.
29     //----------------------------------------------------------------------
30     class MemoryCache
31     {
32     public:
33         //------------------------------------------------------------------
34         // Constructors and Destructors
35         //------------------------------------------------------------------
36         MemoryCache (Process &process);
37 
38         ~MemoryCache ();
39 
40         void
41         Clear();
42 
43         void
44         Flush (lldb::addr_t addr, size_t size);
45 
46         size_t
47         Read (lldb::addr_t addr,
48               void *dst,
49               size_t dst_len,
50               Error &error);
51 
52         uint32_t
53         GetMemoryCacheLineSize() const
54         {
55             return m_cache_line_byte_size ;
56         }
57     protected:
58         typedef std::map<lldb::addr_t, lldb::DataBufferSP> collection;
59         //------------------------------------------------------------------
60         // Classes that inherit from MemoryCache can see and modify these
61         //------------------------------------------------------------------
62         Process &m_process;
63         uint32_t m_cache_line_byte_size;
64         Mutex m_cache_mutex;
65         collection m_cache;
66 
67     private:
68         DISALLOW_COPY_AND_ASSIGN (MemoryCache);
69     };
70 
71 
72     class AllocatedBlock
73     {
74     public:
75         AllocatedBlock (lldb::addr_t addr,
76                         uint32_t byte_size,
77                         uint32_t permissions,
78                         uint32_t chunk_size);
79 
80         ~AllocatedBlock ();
81 
82         lldb::addr_t
83         ReserveBlock (uint32_t size);
84 
85         bool
86         FreeBlock (lldb::addr_t addr);
87 
88         lldb::addr_t
89         GetBaseAddress () const
90         {
91             return m_addr;
92         }
93 
94         uint32_t
95         GetByteSize () const
96         {
97             return m_byte_size;
98         }
99 
100         uint32_t
101         GetPermissions () const
102         {
103             return m_permissions;
104         }
105 
106         uint32_t
107         GetChunkSize () const
108         {
109             return m_chunk_size;
110         }
111 
112         bool
113         Contains (lldb::addr_t addr) const
114         {
115             return ((addr >= m_addr) && addr < (m_addr + m_byte_size));
116         }
117     protected:
118         uint32_t
119         TotalChunks () const
120         {
121             return m_byte_size / m_chunk_size;
122         }
123 
124         uint32_t
125         CalculateChunksNeededForSize (uint32_t size) const
126         {
127             return (size + m_chunk_size - 1) / m_chunk_size;
128         }
129         const lldb::addr_t m_addr;    // Base address of this block of memory
130         const uint32_t m_byte_size;   // 4GB of chunk should be enough...
131         const uint32_t m_permissions; // Permissions for this memory (logical OR of lldb::Permissions bits)
132         const uint32_t m_chunk_size;  // The size of chunks that the memory at m_addr is divied up into
133         typedef std::map<uint32_t, uint32_t> OffsetToChunkSize;
134         OffsetToChunkSize m_offset_to_chunk_size;
135         //llvm::BitVector m_allocated;
136     };
137 
138 
139     //----------------------------------------------------------------------
140     // A class that can track allocated memory and give out allocated memory
141     // without us having to make an allocate/deallocate call every time we
142     // need some memory in a process that is being debugged.
143     //----------------------------------------------------------------------
144     class AllocatedMemoryCache
145     {
146     public:
147         //------------------------------------------------------------------
148         // Constructors and Destructors
149         //------------------------------------------------------------------
150         AllocatedMemoryCache (Process &process);
151 
152         ~AllocatedMemoryCache ();
153 
154         void
155         Clear();
156 
157         lldb::addr_t
158         AllocateMemory (size_t byte_size,
159                         uint32_t permissions,
160                         Error &error);
161 
162         bool
163         DeallocateMemory (lldb::addr_t ptr);
164 
165     protected:
166         typedef lldb::SharedPtr<AllocatedBlock>::Type AllocatedBlockSP;
167 
168         AllocatedBlockSP
169         AllocatePage (uint32_t byte_size,
170                       uint32_t permissions,
171                       uint32_t chunk_size,
172                       Error &error);
173 
174 
175         //------------------------------------------------------------------
176         // Classes that inherit from MemoryCache can see and modify these
177         //------------------------------------------------------------------
178         Process &m_process;
179         Mutex m_mutex;
180         typedef std::multimap<uint32_t, AllocatedBlockSP> PermissionsToBlockMap;
181         PermissionsToBlockMap m_memory_map;
182 
183     private:
184         DISALLOW_COPY_AND_ASSIGN (AllocatedMemoryCache);
185     };
186 
187 } // namespace lldb_private
188 
189 #endif  // liblldb_Memory_h_
190