1ac7ddfbfSEd Maste //===-- Memory.cpp ----------------------------------------------*- C++ -*-===//
2ac7ddfbfSEd Maste //
3ac7ddfbfSEd Maste //                     The LLVM Compiler Infrastructure
4ac7ddfbfSEd Maste //
5ac7ddfbfSEd Maste // This file is distributed under the University of Illinois Open Source
6ac7ddfbfSEd Maste // License. See LICENSE.TXT for details.
7ac7ddfbfSEd Maste //
8ac7ddfbfSEd Maste //===----------------------------------------------------------------------===//
9ac7ddfbfSEd Maste 
10ac7ddfbfSEd Maste #include "lldb/Target/Memory.h"
110127ef0fSEd Maste #include <inttypes.h>
121c3bbb01SEd Maste #include "lldb/Core/RangeMap.h"
13ac7ddfbfSEd Maste #include "lldb/Target/Process.h"
14f678e45dSDimitry Andric #include "lldb/Utility/DataBufferHeap.h"
15f678e45dSDimitry Andric #include "lldb/Utility/Log.h"
16*b5893f02SDimitry Andric #include "lldb/Utility/State.h"
17ac7ddfbfSEd Maste 
18ac7ddfbfSEd Maste using namespace lldb;
19ac7ddfbfSEd Maste using namespace lldb_private;
20ac7ddfbfSEd Maste 
21ac7ddfbfSEd Maste //----------------------------------------------------------------------
22ac7ddfbfSEd Maste // MemoryCache constructor
23ac7ddfbfSEd Maste //----------------------------------------------------------------------
MemoryCache(Process & process)244bb0738eSEd Maste MemoryCache::MemoryCache(Process &process)
25435933ddSDimitry Andric     : m_mutex(), m_L1_cache(), m_L2_cache(), m_invalid_ranges(),
261c3bbb01SEd Maste       m_process(process),
27435933ddSDimitry Andric       m_L2_cache_line_byte_size(process.GetMemoryCacheLineSize()) {}
28ac7ddfbfSEd Maste 
29ac7ddfbfSEd Maste //----------------------------------------------------------------------
30ac7ddfbfSEd Maste // Destructor
31ac7ddfbfSEd Maste //----------------------------------------------------------------------
~MemoryCache()32435933ddSDimitry Andric MemoryCache::~MemoryCache() {}
33ac7ddfbfSEd Maste 
Clear(bool clear_invalid_ranges)34435933ddSDimitry Andric void MemoryCache::Clear(bool clear_invalid_ranges) {
354bb0738eSEd Maste   std::lock_guard<std::recursive_mutex> guard(m_mutex);
361c3bbb01SEd Maste   m_L1_cache.clear();
371c3bbb01SEd Maste   m_L2_cache.clear();
38ac7ddfbfSEd Maste   if (clear_invalid_ranges)
39ac7ddfbfSEd Maste     m_invalid_ranges.Clear();
401c3bbb01SEd Maste   m_L2_cache_line_byte_size = m_process.GetMemoryCacheLineSize();
411c3bbb01SEd Maste }
421c3bbb01SEd Maste 
AddL1CacheData(lldb::addr_t addr,const void * src,size_t src_len)43435933ddSDimitry Andric void MemoryCache::AddL1CacheData(lldb::addr_t addr, const void *src,
44435933ddSDimitry Andric                                  size_t src_len) {
45435933ddSDimitry Andric   AddL1CacheData(
46435933ddSDimitry Andric       addr, DataBufferSP(new DataBufferHeap(DataBufferHeap(src, src_len))));
471c3bbb01SEd Maste }
481c3bbb01SEd Maste 
AddL1CacheData(lldb::addr_t addr,const DataBufferSP & data_buffer_sp)49435933ddSDimitry Andric void MemoryCache::AddL1CacheData(lldb::addr_t addr,
50435933ddSDimitry Andric                                  const DataBufferSP &data_buffer_sp) {
514bb0738eSEd Maste   std::lock_guard<std::recursive_mutex> guard(m_mutex);
521c3bbb01SEd Maste   m_L1_cache[addr] = data_buffer_sp;
53ac7ddfbfSEd Maste }
54ac7ddfbfSEd Maste 
Flush(addr_t addr,size_t size)55435933ddSDimitry Andric void MemoryCache::Flush(addr_t addr, size_t size) {
56ac7ddfbfSEd Maste   if (size == 0)
57ac7ddfbfSEd Maste     return;
58ac7ddfbfSEd Maste 
594bb0738eSEd Maste   std::lock_guard<std::recursive_mutex> guard(m_mutex);
60ac7ddfbfSEd Maste 
611c3bbb01SEd Maste   // Erase any blocks from the L1 cache that intersect with the flush range
62435933ddSDimitry Andric   if (!m_L1_cache.empty()) {
631c3bbb01SEd Maste     AddrRange flush_range(addr, size);
644bb0738eSEd Maste     BlockMap::iterator pos = m_L1_cache.upper_bound(addr);
65435933ddSDimitry Andric     if (pos != m_L1_cache.begin()) {
664bb0738eSEd Maste       --pos;
674bb0738eSEd Maste     }
68435933ddSDimitry Andric     while (pos != m_L1_cache.end()) {
691c3bbb01SEd Maste       AddrRange chunk_range(pos->first, pos->second->GetByteSize());
701c3bbb01SEd Maste       if (!chunk_range.DoesIntersect(flush_range))
711c3bbb01SEd Maste         break;
721c3bbb01SEd Maste       pos = m_L1_cache.erase(pos);
731c3bbb01SEd Maste     }
741c3bbb01SEd Maste   }
751c3bbb01SEd Maste 
76435933ddSDimitry Andric   if (!m_L2_cache.empty()) {
771c3bbb01SEd Maste     const uint32_t cache_line_byte_size = m_L2_cache_line_byte_size;
78ac7ddfbfSEd Maste     const addr_t end_addr = (addr + size - 1);
79ac7ddfbfSEd Maste     const addr_t first_cache_line_addr = addr - (addr % cache_line_byte_size);
80435933ddSDimitry Andric     const addr_t last_cache_line_addr =
81435933ddSDimitry Andric         end_addr - (end_addr % cache_line_byte_size);
82ac7ddfbfSEd Maste     // Watch for overflow where size will cause us to go off the end of the
83ac7ddfbfSEd Maste     // 64 bit address space
84ac7ddfbfSEd Maste     uint32_t num_cache_lines;
85ac7ddfbfSEd Maste     if (last_cache_line_addr >= first_cache_line_addr)
86435933ddSDimitry Andric       num_cache_lines = ((last_cache_line_addr - first_cache_line_addr) /
87435933ddSDimitry Andric                          cache_line_byte_size) +
88435933ddSDimitry Andric                         1;
89ac7ddfbfSEd Maste     else
90435933ddSDimitry Andric       num_cache_lines =
91435933ddSDimitry Andric           (UINT64_MAX - first_cache_line_addr + 1) / cache_line_byte_size;
92ac7ddfbfSEd Maste 
93ac7ddfbfSEd Maste     uint32_t cache_idx = 0;
94435933ddSDimitry Andric     for (addr_t curr_addr = first_cache_line_addr; cache_idx < num_cache_lines;
95435933ddSDimitry Andric          curr_addr += cache_line_byte_size, ++cache_idx) {
961c3bbb01SEd Maste       BlockMap::iterator pos = m_L2_cache.find(curr_addr);
971c3bbb01SEd Maste       if (pos != m_L2_cache.end())
981c3bbb01SEd Maste         m_L2_cache.erase(pos);
991c3bbb01SEd Maste     }
100ac7ddfbfSEd Maste   }
101ac7ddfbfSEd Maste }
102ac7ddfbfSEd Maste 
AddInvalidRange(lldb::addr_t base_addr,lldb::addr_t byte_size)103435933ddSDimitry Andric void MemoryCache::AddInvalidRange(lldb::addr_t base_addr,
104435933ddSDimitry Andric                                   lldb::addr_t byte_size) {
105435933ddSDimitry Andric   if (byte_size > 0) {
1064bb0738eSEd Maste     std::lock_guard<std::recursive_mutex> guard(m_mutex);
107ac7ddfbfSEd Maste     InvalidRanges::Entry range(base_addr, byte_size);
108ac7ddfbfSEd Maste     m_invalid_ranges.Append(range);
109ac7ddfbfSEd Maste     m_invalid_ranges.Sort();
110ac7ddfbfSEd Maste   }
111ac7ddfbfSEd Maste }
112ac7ddfbfSEd Maste 
RemoveInvalidRange(lldb::addr_t base_addr,lldb::addr_t byte_size)113435933ddSDimitry Andric bool MemoryCache::RemoveInvalidRange(lldb::addr_t base_addr,
114435933ddSDimitry Andric                                      lldb::addr_t byte_size) {
115435933ddSDimitry Andric   if (byte_size > 0) {
1164bb0738eSEd Maste     std::lock_guard<std::recursive_mutex> guard(m_mutex);
117ac7ddfbfSEd Maste     const uint32_t idx = m_invalid_ranges.FindEntryIndexThatContains(base_addr);
118435933ddSDimitry Andric     if (idx != UINT32_MAX) {
119ac7ddfbfSEd Maste       const InvalidRanges::Entry *entry = m_invalid_ranges.GetEntryAtIndex(idx);
120435933ddSDimitry Andric       if (entry->GetRangeBase() == base_addr &&
121435933ddSDimitry Andric           entry->GetByteSize() == byte_size)
122ac7ddfbfSEd Maste         return m_invalid_ranges.RemoveEntrtAtIndex(idx);
123ac7ddfbfSEd Maste     }
124ac7ddfbfSEd Maste   }
125ac7ddfbfSEd Maste   return false;
126ac7ddfbfSEd Maste }
127ac7ddfbfSEd Maste 
Read(addr_t addr,void * dst,size_t dst_len,Status & error)1285517e702SDimitry Andric size_t MemoryCache::Read(addr_t addr, void *dst, size_t dst_len,
1295517e702SDimitry Andric                          Status &error) {
130ac7ddfbfSEd Maste   size_t bytes_left = dst_len;
1310127ef0fSEd Maste 
1324ba319b5SDimitry Andric   // Check the L1 cache for a range that contain the entire memory read. If we
1334ba319b5SDimitry Andric   // find a range in the L1 cache that does, we use it. Else we fall back to
1344ba319b5SDimitry Andric   // reading memory in m_L2_cache_line_byte_size byte sized chunks. The L1
1354ba319b5SDimitry Andric   // cache contains chunks of memory that are not required to be
1364ba319b5SDimitry Andric   // m_L2_cache_line_byte_size bytes in size, so we don't try anything tricky
1374ba319b5SDimitry Andric   // when reading from them (no partial reads from the L1 cache).
1381c3bbb01SEd Maste 
1394bb0738eSEd Maste   std::lock_guard<std::recursive_mutex> guard(m_mutex);
140435933ddSDimitry Andric   if (!m_L1_cache.empty()) {
1411c3bbb01SEd Maste     AddrRange read_range(addr, dst_len);
1429f2f44ceSEd Maste     BlockMap::iterator pos = m_L1_cache.upper_bound(addr);
143435933ddSDimitry Andric     if (pos != m_L1_cache.begin()) {
1441c3bbb01SEd Maste       --pos;
1451c3bbb01SEd Maste     }
1469f2f44ceSEd Maste     AddrRange chunk_range(pos->first, pos->second->GetByteSize());
147435933ddSDimitry Andric     if (chunk_range.Contains(read_range)) {
148435933ddSDimitry Andric       memcpy(dst, pos->second->GetBytes() + addr - chunk_range.GetRangeBase(),
149435933ddSDimitry Andric              dst_len);
1501c3bbb01SEd Maste       return dst_len;
1511c3bbb01SEd Maste     }
1521c3bbb01SEd Maste   }
1531c3bbb01SEd Maste 
1544ba319b5SDimitry Andric   // If this memory read request is larger than the cache line size, then we
1554ba319b5SDimitry Andric   // (1) try to read as much of it at once as possible, and (2) don't add the
1564ba319b5SDimitry Andric   // data to the memory cache.  We don't want to split a big read up into more
1574ba319b5SDimitry Andric   // separate reads than necessary, and with a large memory read request, it is
1584ba319b5SDimitry Andric   // unlikely that the caller function will ask for the next
1590127ef0fSEd Maste   // 4 bytes after the large memory read - so there's little benefit to saving
1600127ef0fSEd Maste   // it in the cache.
161435933ddSDimitry Andric   if (dst && dst_len > m_L2_cache_line_byte_size) {
162435933ddSDimitry Andric     size_t bytes_read =
163435933ddSDimitry Andric         m_process.ReadMemoryFromInferior(addr, dst, dst_len, error);
164435933ddSDimitry Andric     // Add this non block sized range to the L1 cache if we actually read
165435933ddSDimitry Andric     // anything
1661c3bbb01SEd Maste     if (bytes_read > 0)
1671c3bbb01SEd Maste       AddL1CacheData(addr, dst, bytes_read);
1681c3bbb01SEd Maste     return bytes_read;
1690127ef0fSEd Maste   }
1700127ef0fSEd Maste 
171435933ddSDimitry Andric   if (dst && bytes_left > 0) {
1721c3bbb01SEd Maste     const uint32_t cache_line_byte_size = m_L2_cache_line_byte_size;
173ac7ddfbfSEd Maste     uint8_t *dst_buf = (uint8_t *)dst;
174ac7ddfbfSEd Maste     addr_t curr_addr = addr - (addr % cache_line_byte_size);
175ac7ddfbfSEd Maste     addr_t cache_offset = addr - curr_addr;
176ac7ddfbfSEd Maste 
177435933ddSDimitry Andric     while (bytes_left > 0) {
178435933ddSDimitry Andric       if (m_invalid_ranges.FindEntryThatContains(curr_addr)) {
179435933ddSDimitry Andric         error.SetErrorStringWithFormat("memory read failed for 0x%" PRIx64,
180435933ddSDimitry Andric                                        curr_addr);
181ac7ddfbfSEd Maste         return dst_len - bytes_left;
182ac7ddfbfSEd Maste       }
183ac7ddfbfSEd Maste 
1841c3bbb01SEd Maste       BlockMap::const_iterator pos = m_L2_cache.find(curr_addr);
1851c3bbb01SEd Maste       BlockMap::const_iterator end = m_L2_cache.end();
186ac7ddfbfSEd Maste 
187435933ddSDimitry Andric       if (pos != end) {
188ac7ddfbfSEd Maste         size_t curr_read_size = cache_line_byte_size - cache_offset;
189ac7ddfbfSEd Maste         if (curr_read_size > bytes_left)
190ac7ddfbfSEd Maste           curr_read_size = bytes_left;
191ac7ddfbfSEd Maste 
192435933ddSDimitry Andric         memcpy(dst_buf + dst_len - bytes_left,
193435933ddSDimitry Andric                pos->second->GetBytes() + cache_offset, curr_read_size);
194ac7ddfbfSEd Maste 
195ac7ddfbfSEd Maste         bytes_left -= curr_read_size;
196ac7ddfbfSEd Maste         curr_addr += curr_read_size + cache_offset;
197ac7ddfbfSEd Maste         cache_offset = 0;
198ac7ddfbfSEd Maste 
199435933ddSDimitry Andric         if (bytes_left > 0) {
200ac7ddfbfSEd Maste           // Get sequential cache page hits
201435933ddSDimitry Andric           for (++pos; (pos != end) && (bytes_left > 0); ++pos) {
202ac7ddfbfSEd Maste             assert((curr_addr % cache_line_byte_size) == 0);
203ac7ddfbfSEd Maste 
204ac7ddfbfSEd Maste             if (pos->first != curr_addr)
205ac7ddfbfSEd Maste               break;
206ac7ddfbfSEd Maste 
207ac7ddfbfSEd Maste             curr_read_size = pos->second->GetByteSize();
208ac7ddfbfSEd Maste             if (curr_read_size > bytes_left)
209ac7ddfbfSEd Maste               curr_read_size = bytes_left;
210ac7ddfbfSEd Maste 
211435933ddSDimitry Andric             memcpy(dst_buf + dst_len - bytes_left, pos->second->GetBytes(),
212435933ddSDimitry Andric                    curr_read_size);
213ac7ddfbfSEd Maste 
214ac7ddfbfSEd Maste             bytes_left -= curr_read_size;
215ac7ddfbfSEd Maste             curr_addr += curr_read_size;
216ac7ddfbfSEd Maste 
2174ba319b5SDimitry Andric             // We have a cache page that succeeded to read some bytes but not
2184ba319b5SDimitry Andric             // an entire page. If this happens, we must cap off how much data
2194ba319b5SDimitry Andric             // we are able to read...
220ac7ddfbfSEd Maste             if (pos->second->GetByteSize() != cache_line_byte_size)
221ac7ddfbfSEd Maste               return dst_len - bytes_left;
222ac7ddfbfSEd Maste           }
223ac7ddfbfSEd Maste         }
224ac7ddfbfSEd Maste       }
225ac7ddfbfSEd Maste 
226ac7ddfbfSEd Maste       // We need to read from the process
227ac7ddfbfSEd Maste 
228435933ddSDimitry Andric       if (bytes_left > 0) {
229ac7ddfbfSEd Maste         assert((curr_addr % cache_line_byte_size) == 0);
230435933ddSDimitry Andric         std::unique_ptr<DataBufferHeap> data_buffer_heap_ap(
231435933ddSDimitry Andric             new DataBufferHeap(cache_line_byte_size, 0));
232435933ddSDimitry Andric         size_t process_bytes_read = m_process.ReadMemoryFromInferior(
233435933ddSDimitry Andric             curr_addr, data_buffer_heap_ap->GetBytes(),
234435933ddSDimitry Andric             data_buffer_heap_ap->GetByteSize(), error);
235ac7ddfbfSEd Maste         if (process_bytes_read == 0)
236ac7ddfbfSEd Maste           return dst_len - bytes_left;
237ac7ddfbfSEd Maste 
238ac7ddfbfSEd Maste         if (process_bytes_read != cache_line_byte_size)
239ac7ddfbfSEd Maste           data_buffer_heap_ap->SetByteSize(process_bytes_read);
2401c3bbb01SEd Maste         m_L2_cache[curr_addr] = DataBufferSP(data_buffer_heap_ap.release());
241ac7ddfbfSEd Maste         // We have read data and put it into the cache, continue through the
242ac7ddfbfSEd Maste         // loop again to get the data out of the cache...
243ac7ddfbfSEd Maste       }
244ac7ddfbfSEd Maste     }
245ac7ddfbfSEd Maste   }
246ac7ddfbfSEd Maste 
247ac7ddfbfSEd Maste   return dst_len - bytes_left;
248ac7ddfbfSEd Maste }
249ac7ddfbfSEd Maste 
AllocatedBlock(lldb::addr_t addr,uint32_t byte_size,uint32_t permissions,uint32_t chunk_size)250435933ddSDimitry Andric AllocatedBlock::AllocatedBlock(lldb::addr_t addr, uint32_t byte_size,
251435933ddSDimitry Andric                                uint32_t permissions, uint32_t chunk_size)
252f678e45dSDimitry Andric     : m_range(addr, byte_size), m_permissions(permissions),
253f678e45dSDimitry Andric       m_chunk_size(chunk_size)
254ac7ddfbfSEd Maste {
255f678e45dSDimitry Andric   // The entire address range is free to start with.
256f678e45dSDimitry Andric   m_free_blocks.Append(m_range);
257ac7ddfbfSEd Maste   assert(byte_size > chunk_size);
258ac7ddfbfSEd Maste }
259ac7ddfbfSEd Maste 
~AllocatedBlock()260435933ddSDimitry Andric AllocatedBlock::~AllocatedBlock() {}
261ac7ddfbfSEd Maste 
ReserveBlock(uint32_t size)262435933ddSDimitry Andric lldb::addr_t AllocatedBlock::ReserveBlock(uint32_t size) {
263f678e45dSDimitry Andric   // We must return something valid for zero bytes.
264f678e45dSDimitry Andric   if (size == 0)
265f678e45dSDimitry Andric     size = 1;
266f678e45dSDimitry Andric   Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS));
267ac7ddfbfSEd Maste 
268f678e45dSDimitry Andric   const size_t free_count = m_free_blocks.GetSize();
269f678e45dSDimitry Andric   for (size_t i=0; i<free_count; ++i)
270f678e45dSDimitry Andric   {
271f678e45dSDimitry Andric     auto &free_block = m_free_blocks.GetEntryRef(i);
272f678e45dSDimitry Andric     const lldb::addr_t range_size = free_block.GetByteSize();
273f678e45dSDimitry Andric     if (range_size >= size)
274f678e45dSDimitry Andric     {
275f678e45dSDimitry Andric       // We found a free block that is big enough for our data. Figure out how
2764ba319b5SDimitry Andric       // many chunks we will need and calculate the resulting block size we
2774ba319b5SDimitry Andric       // will reserve.
278f678e45dSDimitry Andric       addr_t addr = free_block.GetRangeBase();
279f678e45dSDimitry Andric       size_t num_chunks = CalculateChunksNeededForSize(size);
280f678e45dSDimitry Andric       lldb::addr_t block_size = num_chunks * m_chunk_size;
281f678e45dSDimitry Andric       lldb::addr_t bytes_left = range_size - block_size;
282f678e45dSDimitry Andric       if (bytes_left == 0)
283f678e45dSDimitry Andric       {
284f678e45dSDimitry Andric         // The newly allocated block will take all of the bytes in this
285f678e45dSDimitry Andric         // available block, so we can just add it to the allocated ranges and
286f678e45dSDimitry Andric         // remove the range from the free ranges.
287f678e45dSDimitry Andric         m_reserved_blocks.Insert(free_block, false);
288f678e45dSDimitry Andric         m_free_blocks.RemoveEntryAtIndex(i);
289ac7ddfbfSEd Maste       }
290f678e45dSDimitry Andric       else
291f678e45dSDimitry Andric       {
292f678e45dSDimitry Andric         // Make the new allocated range and add it to the allocated ranges.
293f678e45dSDimitry Andric         Range<lldb::addr_t, uint32_t> reserved_block(free_block);
294f678e45dSDimitry Andric         reserved_block.SetByteSize(block_size);
2954ba319b5SDimitry Andric         // Insert the reserved range and don't combine it with other blocks in
2964ba319b5SDimitry Andric         // the reserved blocks list.
297f678e45dSDimitry Andric         m_reserved_blocks.Insert(reserved_block, false);
298f678e45dSDimitry Andric         // Adjust the free range in place since we won't change the sorted
299f678e45dSDimitry Andric         // ordering of the m_free_blocks list.
300f678e45dSDimitry Andric         free_block.SetRangeBase(reserved_block.GetRangeEnd());
301f678e45dSDimitry Andric         free_block.SetByteSize(bytes_left);
302ac7ddfbfSEd Maste       }
303f678e45dSDimitry Andric       LLDB_LOGV(log, "({0}) (size = {1} ({1:x})) => {2:x}", this, size, addr);
304ac7ddfbfSEd Maste       return addr;
305ac7ddfbfSEd Maste     }
306f678e45dSDimitry Andric   }
307f678e45dSDimitry Andric 
308f678e45dSDimitry Andric   LLDB_LOGV(log, "({0}) (size = {1} ({1:x})) => {2:x}", this, size,
309f678e45dSDimitry Andric             LLDB_INVALID_ADDRESS);
310f678e45dSDimitry Andric   return LLDB_INVALID_ADDRESS;
311f678e45dSDimitry Andric }
312ac7ddfbfSEd Maste 
FreeBlock(addr_t addr)313435933ddSDimitry Andric bool AllocatedBlock::FreeBlock(addr_t addr) {
314ac7ddfbfSEd Maste   bool success = false;
315f678e45dSDimitry Andric   auto entry_idx = m_reserved_blocks.FindEntryIndexThatContains(addr);
316f678e45dSDimitry Andric   if (entry_idx != UINT32_MAX)
317f678e45dSDimitry Andric   {
318f678e45dSDimitry Andric     m_free_blocks.Insert(m_reserved_blocks.GetEntryRef(entry_idx), true);
319f678e45dSDimitry Andric     m_reserved_blocks.RemoveEntryAtIndex(entry_idx);
320ac7ddfbfSEd Maste     success = true;
321ac7ddfbfSEd Maste   }
322f678e45dSDimitry Andric   Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS));
323f678e45dSDimitry Andric   LLDB_LOGV(log, "({0}) (addr = {1:x}) => {2}", this, addr, success);
324ac7ddfbfSEd Maste   return success;
325ac7ddfbfSEd Maste }
326ac7ddfbfSEd Maste 
AllocatedMemoryCache(Process & process)327435933ddSDimitry Andric AllocatedMemoryCache::AllocatedMemoryCache(Process &process)
328435933ddSDimitry Andric     : m_process(process), m_mutex(), m_memory_map() {}
329ac7ddfbfSEd Maste 
~AllocatedMemoryCache()330435933ddSDimitry Andric AllocatedMemoryCache::~AllocatedMemoryCache() {}
331ac7ddfbfSEd Maste 
Clear()332435933ddSDimitry Andric void AllocatedMemoryCache::Clear() {
3334bb0738eSEd Maste   std::lock_guard<std::recursive_mutex> guard(m_mutex);
334435933ddSDimitry Andric   if (m_process.IsAlive()) {
335ac7ddfbfSEd Maste     PermissionsToBlockMap::iterator pos, end = m_memory_map.end();
336ac7ddfbfSEd Maste     for (pos = m_memory_map.begin(); pos != end; ++pos)
337ac7ddfbfSEd Maste       m_process.DoDeallocateMemory(pos->second->GetBaseAddress());
338ac7ddfbfSEd Maste   }
339ac7ddfbfSEd Maste   m_memory_map.clear();
340ac7ddfbfSEd Maste }
341ac7ddfbfSEd Maste 
342ac7ddfbfSEd Maste AllocatedMemoryCache::AllocatedBlockSP
AllocatePage(uint32_t byte_size,uint32_t permissions,uint32_t chunk_size,Status & error)343435933ddSDimitry Andric AllocatedMemoryCache::AllocatePage(uint32_t byte_size, uint32_t permissions,
3445517e702SDimitry Andric                                    uint32_t chunk_size, Status &error) {
345ac7ddfbfSEd Maste   AllocatedBlockSP block_sp;
346ac7ddfbfSEd Maste   const size_t page_size = 4096;
347ac7ddfbfSEd Maste   const size_t num_pages = (byte_size + page_size - 1) / page_size;
348ac7ddfbfSEd Maste   const size_t page_byte_size = num_pages * page_size;
349ac7ddfbfSEd Maste 
350ac7ddfbfSEd Maste   addr_t addr = m_process.DoAllocateMemory(page_byte_size, permissions, error);
351ac7ddfbfSEd Maste 
352ac7ddfbfSEd Maste   Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS));
353435933ddSDimitry Andric   if (log) {
354435933ddSDimitry Andric     log->Printf("Process::DoAllocateMemory (byte_size = 0x%8.8" PRIx32
355435933ddSDimitry Andric                 ", permissions = %s) => 0x%16.16" PRIx64,
356435933ddSDimitry Andric                 (uint32_t)page_byte_size, GetPermissionsAsCString(permissions),
357ac7ddfbfSEd Maste                 (uint64_t)addr);
358ac7ddfbfSEd Maste   }
359ac7ddfbfSEd Maste 
360435933ddSDimitry Andric   if (addr != LLDB_INVALID_ADDRESS) {
361435933ddSDimitry Andric     block_sp.reset(
362435933ddSDimitry Andric         new AllocatedBlock(addr, page_byte_size, permissions, chunk_size));
363ac7ddfbfSEd Maste     m_memory_map.insert(std::make_pair(permissions, block_sp));
364ac7ddfbfSEd Maste   }
365ac7ddfbfSEd Maste   return block_sp;
366ac7ddfbfSEd Maste }
367ac7ddfbfSEd Maste 
AllocateMemory(size_t byte_size,uint32_t permissions,Status & error)368435933ddSDimitry Andric lldb::addr_t AllocatedMemoryCache::AllocateMemory(size_t byte_size,
369ac7ddfbfSEd Maste                                                   uint32_t permissions,
3705517e702SDimitry Andric                                                   Status &error) {
3714bb0738eSEd Maste   std::lock_guard<std::recursive_mutex> guard(m_mutex);
372ac7ddfbfSEd Maste 
373ac7ddfbfSEd Maste   addr_t addr = LLDB_INVALID_ADDRESS;
374435933ddSDimitry Andric   std::pair<PermissionsToBlockMap::iterator, PermissionsToBlockMap::iterator>
375435933ddSDimitry Andric       range = m_memory_map.equal_range(permissions);
376ac7ddfbfSEd Maste 
377435933ddSDimitry Andric   for (PermissionsToBlockMap::iterator pos = range.first; pos != range.second;
378435933ddSDimitry Andric        ++pos) {
379ac7ddfbfSEd Maste     addr = (*pos).second->ReserveBlock(byte_size);
3800127ef0fSEd Maste     if (addr != LLDB_INVALID_ADDRESS)
3810127ef0fSEd Maste       break;
382ac7ddfbfSEd Maste   }
383ac7ddfbfSEd Maste 
384435933ddSDimitry Andric   if (addr == LLDB_INVALID_ADDRESS) {
385ac7ddfbfSEd Maste     AllocatedBlockSP block_sp(AllocatePage(byte_size, permissions, 16, error));
386ac7ddfbfSEd Maste 
387ac7ddfbfSEd Maste     if (block_sp)
388ac7ddfbfSEd Maste       addr = block_sp->ReserveBlock(byte_size);
389ac7ddfbfSEd Maste   }
390ac7ddfbfSEd Maste   Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS));
391ac7ddfbfSEd Maste   if (log)
392435933ddSDimitry Andric     log->Printf(
393435933ddSDimitry Andric         "AllocatedMemoryCache::AllocateMemory (byte_size = 0x%8.8" PRIx32
394435933ddSDimitry Andric         ", permissions = %s) => 0x%16.16" PRIx64,
395435933ddSDimitry Andric         (uint32_t)byte_size, GetPermissionsAsCString(permissions),
396435933ddSDimitry Andric         (uint64_t)addr);
397ac7ddfbfSEd Maste   return addr;
398ac7ddfbfSEd Maste }
399ac7ddfbfSEd Maste 
DeallocateMemory(lldb::addr_t addr)400435933ddSDimitry Andric bool AllocatedMemoryCache::DeallocateMemory(lldb::addr_t addr) {
4014bb0738eSEd Maste   std::lock_guard<std::recursive_mutex> guard(m_mutex);
402ac7ddfbfSEd Maste 
403ac7ddfbfSEd Maste   PermissionsToBlockMap::iterator pos, end = m_memory_map.end();
404ac7ddfbfSEd Maste   bool success = false;
405435933ddSDimitry Andric   for (pos = m_memory_map.begin(); pos != end; ++pos) {
406435933ddSDimitry Andric     if (pos->second->Contains(addr)) {
407ac7ddfbfSEd Maste       success = pos->second->FreeBlock(addr);
408ac7ddfbfSEd Maste       break;
409ac7ddfbfSEd Maste     }
410ac7ddfbfSEd Maste   }
411ac7ddfbfSEd Maste   Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS));
412ac7ddfbfSEd Maste   if (log)
413435933ddSDimitry Andric     log->Printf("AllocatedMemoryCache::DeallocateMemory (addr = 0x%16.16" PRIx64
414435933ddSDimitry Andric                 ") => %i",
415435933ddSDimitry Andric                 (uint64_t)addr, success);
416ac7ddfbfSEd Maste   return success;
417ac7ddfbfSEd Maste }
418