1dda28197Spatrick //===-- Memory.cpp --------------------------------------------------------===//
2061da546Spatrick //
3061da546Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4061da546Spatrick // See https://llvm.org/LICENSE.txt for license information.
5061da546Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6061da546Spatrick //
7061da546Spatrick //===----------------------------------------------------------------------===//
8061da546Spatrick
9061da546Spatrick #include "lldb/Target/Memory.h"
10061da546Spatrick #include "lldb/Target/Process.h"
11061da546Spatrick #include "lldb/Utility/DataBufferHeap.h"
12*f6aab3d8Srobert #include "lldb/Utility/LLDBLog.h"
13061da546Spatrick #include "lldb/Utility/Log.h"
14061da546Spatrick #include "lldb/Utility/RangeMap.h"
15061da546Spatrick #include "lldb/Utility/State.h"
16061da546Spatrick
17061da546Spatrick #include <cinttypes>
18061da546Spatrick #include <memory>
19061da546Spatrick
20061da546Spatrick using namespace lldb;
21061da546Spatrick using namespace lldb_private;
22061da546Spatrick
23061da546Spatrick // MemoryCache constructor
MemoryCache(Process & process)24061da546Spatrick MemoryCache::MemoryCache(Process &process)
25061da546Spatrick : m_mutex(), m_L1_cache(), m_L2_cache(), m_invalid_ranges(),
26061da546Spatrick m_process(process),
27061da546Spatrick m_L2_cache_line_byte_size(process.GetMemoryCacheLineSize()) {}
28061da546Spatrick
29061da546Spatrick // Destructor
30be691f3bSpatrick MemoryCache::~MemoryCache() = default;
31061da546Spatrick
Clear(bool clear_invalid_ranges)32061da546Spatrick void MemoryCache::Clear(bool clear_invalid_ranges) {
33061da546Spatrick std::lock_guard<std::recursive_mutex> guard(m_mutex);
34061da546Spatrick m_L1_cache.clear();
35061da546Spatrick m_L2_cache.clear();
36061da546Spatrick if (clear_invalid_ranges)
37061da546Spatrick m_invalid_ranges.Clear();
38061da546Spatrick m_L2_cache_line_byte_size = m_process.GetMemoryCacheLineSize();
39061da546Spatrick }
40061da546Spatrick
AddL1CacheData(lldb::addr_t addr,const void * src,size_t src_len)41061da546Spatrick void MemoryCache::AddL1CacheData(lldb::addr_t addr, const void *src,
42061da546Spatrick size_t src_len) {
43061da546Spatrick AddL1CacheData(
44061da546Spatrick addr, DataBufferSP(new DataBufferHeap(DataBufferHeap(src, src_len))));
45061da546Spatrick }
46061da546Spatrick
AddL1CacheData(lldb::addr_t addr,const DataBufferSP & data_buffer_sp)47061da546Spatrick void MemoryCache::AddL1CacheData(lldb::addr_t addr,
48061da546Spatrick const DataBufferSP &data_buffer_sp) {
49061da546Spatrick std::lock_guard<std::recursive_mutex> guard(m_mutex);
50061da546Spatrick m_L1_cache[addr] = data_buffer_sp;
51061da546Spatrick }
52061da546Spatrick
Flush(addr_t addr,size_t size)53061da546Spatrick void MemoryCache::Flush(addr_t addr, size_t size) {
54061da546Spatrick if (size == 0)
55061da546Spatrick return;
56061da546Spatrick
57061da546Spatrick std::lock_guard<std::recursive_mutex> guard(m_mutex);
58061da546Spatrick
59061da546Spatrick // Erase any blocks from the L1 cache that intersect with the flush range
60061da546Spatrick if (!m_L1_cache.empty()) {
61061da546Spatrick AddrRange flush_range(addr, size);
62061da546Spatrick BlockMap::iterator pos = m_L1_cache.upper_bound(addr);
63061da546Spatrick if (pos != m_L1_cache.begin()) {
64061da546Spatrick --pos;
65061da546Spatrick }
66061da546Spatrick while (pos != m_L1_cache.end()) {
67061da546Spatrick AddrRange chunk_range(pos->first, pos->second->GetByteSize());
68061da546Spatrick if (!chunk_range.DoesIntersect(flush_range))
69061da546Spatrick break;
70061da546Spatrick pos = m_L1_cache.erase(pos);
71061da546Spatrick }
72061da546Spatrick }
73061da546Spatrick
74061da546Spatrick if (!m_L2_cache.empty()) {
75061da546Spatrick const uint32_t cache_line_byte_size = m_L2_cache_line_byte_size;
76061da546Spatrick const addr_t end_addr = (addr + size - 1);
77061da546Spatrick const addr_t first_cache_line_addr = addr - (addr % cache_line_byte_size);
78061da546Spatrick const addr_t last_cache_line_addr =
79061da546Spatrick end_addr - (end_addr % cache_line_byte_size);
80061da546Spatrick // Watch for overflow where size will cause us to go off the end of the
81061da546Spatrick // 64 bit address space
82061da546Spatrick uint32_t num_cache_lines;
83061da546Spatrick if (last_cache_line_addr >= first_cache_line_addr)
84061da546Spatrick num_cache_lines = ((last_cache_line_addr - first_cache_line_addr) /
85061da546Spatrick cache_line_byte_size) +
86061da546Spatrick 1;
87061da546Spatrick else
88061da546Spatrick num_cache_lines =
89061da546Spatrick (UINT64_MAX - first_cache_line_addr + 1) / cache_line_byte_size;
90061da546Spatrick
91061da546Spatrick uint32_t cache_idx = 0;
92061da546Spatrick for (addr_t curr_addr = first_cache_line_addr; cache_idx < num_cache_lines;
93061da546Spatrick curr_addr += cache_line_byte_size, ++cache_idx) {
94061da546Spatrick BlockMap::iterator pos = m_L2_cache.find(curr_addr);
95061da546Spatrick if (pos != m_L2_cache.end())
96061da546Spatrick m_L2_cache.erase(pos);
97061da546Spatrick }
98061da546Spatrick }
99061da546Spatrick }
100061da546Spatrick
AddInvalidRange(lldb::addr_t base_addr,lldb::addr_t byte_size)101061da546Spatrick void MemoryCache::AddInvalidRange(lldb::addr_t base_addr,
102061da546Spatrick lldb::addr_t byte_size) {
103061da546Spatrick if (byte_size > 0) {
104061da546Spatrick std::lock_guard<std::recursive_mutex> guard(m_mutex);
105061da546Spatrick InvalidRanges::Entry range(base_addr, byte_size);
106061da546Spatrick m_invalid_ranges.Append(range);
107061da546Spatrick m_invalid_ranges.Sort();
108061da546Spatrick }
109061da546Spatrick }
110061da546Spatrick
RemoveInvalidRange(lldb::addr_t base_addr,lldb::addr_t byte_size)111061da546Spatrick bool MemoryCache::RemoveInvalidRange(lldb::addr_t base_addr,
112061da546Spatrick lldb::addr_t byte_size) {
113061da546Spatrick if (byte_size > 0) {
114061da546Spatrick std::lock_guard<std::recursive_mutex> guard(m_mutex);
115061da546Spatrick const uint32_t idx = m_invalid_ranges.FindEntryIndexThatContains(base_addr);
116061da546Spatrick if (idx != UINT32_MAX) {
117061da546Spatrick const InvalidRanges::Entry *entry = m_invalid_ranges.GetEntryAtIndex(idx);
118061da546Spatrick if (entry->GetRangeBase() == base_addr &&
119061da546Spatrick entry->GetByteSize() == byte_size)
120dda28197Spatrick return m_invalid_ranges.RemoveEntryAtIndex(idx);
121061da546Spatrick }
122061da546Spatrick }
123061da546Spatrick return false;
124061da546Spatrick }
125061da546Spatrick
Read(addr_t addr,void * dst,size_t dst_len,Status & error)126061da546Spatrick size_t MemoryCache::Read(addr_t addr, void *dst, size_t dst_len,
127061da546Spatrick Status &error) {
128061da546Spatrick size_t bytes_left = dst_len;
129061da546Spatrick
130061da546Spatrick // Check the L1 cache for a range that contain the entire memory read. If we
131061da546Spatrick // find a range in the L1 cache that does, we use it. Else we fall back to
132061da546Spatrick // reading memory in m_L2_cache_line_byte_size byte sized chunks. The L1
133061da546Spatrick // cache contains chunks of memory that are not required to be
134061da546Spatrick // m_L2_cache_line_byte_size bytes in size, so we don't try anything tricky
135061da546Spatrick // when reading from them (no partial reads from the L1 cache).
136061da546Spatrick
137061da546Spatrick std::lock_guard<std::recursive_mutex> guard(m_mutex);
138061da546Spatrick if (!m_L1_cache.empty()) {
139061da546Spatrick AddrRange read_range(addr, dst_len);
140061da546Spatrick BlockMap::iterator pos = m_L1_cache.upper_bound(addr);
141061da546Spatrick if (pos != m_L1_cache.begin()) {
142061da546Spatrick --pos;
143061da546Spatrick }
144061da546Spatrick AddrRange chunk_range(pos->first, pos->second->GetByteSize());
145061da546Spatrick if (chunk_range.Contains(read_range)) {
146061da546Spatrick memcpy(dst, pos->second->GetBytes() + (addr - chunk_range.GetRangeBase()),
147061da546Spatrick dst_len);
148061da546Spatrick return dst_len;
149061da546Spatrick }
150061da546Spatrick }
151061da546Spatrick
152061da546Spatrick // If this memory read request is larger than the cache line size, then we
153061da546Spatrick // (1) try to read as much of it at once as possible, and (2) don't add the
154061da546Spatrick // data to the memory cache. We don't want to split a big read up into more
155061da546Spatrick // separate reads than necessary, and with a large memory read request, it is
156061da546Spatrick // unlikely that the caller function will ask for the next
157061da546Spatrick // 4 bytes after the large memory read - so there's little benefit to saving
158061da546Spatrick // it in the cache.
159061da546Spatrick if (dst && dst_len > m_L2_cache_line_byte_size) {
160061da546Spatrick size_t bytes_read =
161061da546Spatrick m_process.ReadMemoryFromInferior(addr, dst, dst_len, error);
162061da546Spatrick // Add this non block sized range to the L1 cache if we actually read
163061da546Spatrick // anything
164061da546Spatrick if (bytes_read > 0)
165061da546Spatrick AddL1CacheData(addr, dst, bytes_read);
166061da546Spatrick return bytes_read;
167061da546Spatrick }
168061da546Spatrick
169061da546Spatrick if (dst && bytes_left > 0) {
170061da546Spatrick const uint32_t cache_line_byte_size = m_L2_cache_line_byte_size;
171061da546Spatrick uint8_t *dst_buf = (uint8_t *)dst;
172061da546Spatrick addr_t curr_addr = addr - (addr % cache_line_byte_size);
173061da546Spatrick addr_t cache_offset = addr - curr_addr;
174061da546Spatrick
175061da546Spatrick while (bytes_left > 0) {
176061da546Spatrick if (m_invalid_ranges.FindEntryThatContains(curr_addr)) {
177061da546Spatrick error.SetErrorStringWithFormat("memory read failed for 0x%" PRIx64,
178061da546Spatrick curr_addr);
179061da546Spatrick return dst_len - bytes_left;
180061da546Spatrick }
181061da546Spatrick
182061da546Spatrick BlockMap::const_iterator pos = m_L2_cache.find(curr_addr);
183061da546Spatrick BlockMap::const_iterator end = m_L2_cache.end();
184061da546Spatrick
185061da546Spatrick if (pos != end) {
186061da546Spatrick size_t curr_read_size = cache_line_byte_size - cache_offset;
187061da546Spatrick if (curr_read_size > bytes_left)
188061da546Spatrick curr_read_size = bytes_left;
189061da546Spatrick
190061da546Spatrick memcpy(dst_buf + dst_len - bytes_left,
191061da546Spatrick pos->second->GetBytes() + cache_offset, curr_read_size);
192061da546Spatrick
193061da546Spatrick bytes_left -= curr_read_size;
194061da546Spatrick curr_addr += curr_read_size + cache_offset;
195061da546Spatrick cache_offset = 0;
196061da546Spatrick
197061da546Spatrick if (bytes_left > 0) {
198061da546Spatrick // Get sequential cache page hits
199061da546Spatrick for (++pos; (pos != end) && (bytes_left > 0); ++pos) {
200061da546Spatrick assert((curr_addr % cache_line_byte_size) == 0);
201061da546Spatrick
202061da546Spatrick if (pos->first != curr_addr)
203061da546Spatrick break;
204061da546Spatrick
205061da546Spatrick curr_read_size = pos->second->GetByteSize();
206061da546Spatrick if (curr_read_size > bytes_left)
207061da546Spatrick curr_read_size = bytes_left;
208061da546Spatrick
209061da546Spatrick memcpy(dst_buf + dst_len - bytes_left, pos->second->GetBytes(),
210061da546Spatrick curr_read_size);
211061da546Spatrick
212061da546Spatrick bytes_left -= curr_read_size;
213061da546Spatrick curr_addr += curr_read_size;
214061da546Spatrick
215061da546Spatrick // We have a cache page that succeeded to read some bytes but not
216061da546Spatrick // an entire page. If this happens, we must cap off how much data
217061da546Spatrick // we are able to read...
218061da546Spatrick if (pos->second->GetByteSize() != cache_line_byte_size)
219061da546Spatrick return dst_len - bytes_left;
220061da546Spatrick }
221061da546Spatrick }
222061da546Spatrick }
223061da546Spatrick
224061da546Spatrick // We need to read from the process
225061da546Spatrick
226061da546Spatrick if (bytes_left > 0) {
227061da546Spatrick assert((curr_addr % cache_line_byte_size) == 0);
228061da546Spatrick std::unique_ptr<DataBufferHeap> data_buffer_heap_up(
229061da546Spatrick new DataBufferHeap(cache_line_byte_size, 0));
230061da546Spatrick size_t process_bytes_read = m_process.ReadMemoryFromInferior(
231061da546Spatrick curr_addr, data_buffer_heap_up->GetBytes(),
232061da546Spatrick data_buffer_heap_up->GetByteSize(), error);
233061da546Spatrick if (process_bytes_read == 0)
234061da546Spatrick return dst_len - bytes_left;
235061da546Spatrick
236dda28197Spatrick if (process_bytes_read != cache_line_byte_size) {
237dda28197Spatrick if (process_bytes_read < data_buffer_heap_up->GetByteSize()) {
238dda28197Spatrick dst_len -= data_buffer_heap_up->GetByteSize() - process_bytes_read;
239dda28197Spatrick bytes_left = process_bytes_read;
240dda28197Spatrick }
241061da546Spatrick data_buffer_heap_up->SetByteSize(process_bytes_read);
242dda28197Spatrick }
243061da546Spatrick m_L2_cache[curr_addr] = DataBufferSP(data_buffer_heap_up.release());
244061da546Spatrick // We have read data and put it into the cache, continue through the
245061da546Spatrick // loop again to get the data out of the cache...
246061da546Spatrick }
247061da546Spatrick }
248061da546Spatrick }
249061da546Spatrick
250061da546Spatrick return dst_len - bytes_left;
251061da546Spatrick }
252061da546Spatrick
AllocatedBlock(lldb::addr_t addr,uint32_t byte_size,uint32_t permissions,uint32_t chunk_size)253061da546Spatrick AllocatedBlock::AllocatedBlock(lldb::addr_t addr, uint32_t byte_size,
254061da546Spatrick uint32_t permissions, uint32_t chunk_size)
255061da546Spatrick : m_range(addr, byte_size), m_permissions(permissions),
256061da546Spatrick m_chunk_size(chunk_size)
257061da546Spatrick {
258061da546Spatrick // The entire address range is free to start with.
259061da546Spatrick m_free_blocks.Append(m_range);
260061da546Spatrick assert(byte_size > chunk_size);
261061da546Spatrick }
262061da546Spatrick
263be691f3bSpatrick AllocatedBlock::~AllocatedBlock() = default;
264061da546Spatrick
ReserveBlock(uint32_t size)265061da546Spatrick lldb::addr_t AllocatedBlock::ReserveBlock(uint32_t size) {
266061da546Spatrick // We must return something valid for zero bytes.
267061da546Spatrick if (size == 0)
268061da546Spatrick size = 1;
269*f6aab3d8Srobert Log *log = GetLog(LLDBLog::Process);
270061da546Spatrick
271061da546Spatrick const size_t free_count = m_free_blocks.GetSize();
272061da546Spatrick for (size_t i=0; i<free_count; ++i)
273061da546Spatrick {
274061da546Spatrick auto &free_block = m_free_blocks.GetEntryRef(i);
275061da546Spatrick const lldb::addr_t range_size = free_block.GetByteSize();
276061da546Spatrick if (range_size >= size)
277061da546Spatrick {
278061da546Spatrick // We found a free block that is big enough for our data. Figure out how
279061da546Spatrick // many chunks we will need and calculate the resulting block size we
280061da546Spatrick // will reserve.
281061da546Spatrick addr_t addr = free_block.GetRangeBase();
282061da546Spatrick size_t num_chunks = CalculateChunksNeededForSize(size);
283061da546Spatrick lldb::addr_t block_size = num_chunks * m_chunk_size;
284061da546Spatrick lldb::addr_t bytes_left = range_size - block_size;
285061da546Spatrick if (bytes_left == 0)
286061da546Spatrick {
287061da546Spatrick // The newly allocated block will take all of the bytes in this
288061da546Spatrick // available block, so we can just add it to the allocated ranges and
289061da546Spatrick // remove the range from the free ranges.
290061da546Spatrick m_reserved_blocks.Insert(free_block, false);
291061da546Spatrick m_free_blocks.RemoveEntryAtIndex(i);
292061da546Spatrick }
293061da546Spatrick else
294061da546Spatrick {
295061da546Spatrick // Make the new allocated range and add it to the allocated ranges.
296061da546Spatrick Range<lldb::addr_t, uint32_t> reserved_block(free_block);
297061da546Spatrick reserved_block.SetByteSize(block_size);
298061da546Spatrick // Insert the reserved range and don't combine it with other blocks in
299061da546Spatrick // the reserved blocks list.
300061da546Spatrick m_reserved_blocks.Insert(reserved_block, false);
301061da546Spatrick // Adjust the free range in place since we won't change the sorted
302061da546Spatrick // ordering of the m_free_blocks list.
303061da546Spatrick free_block.SetRangeBase(reserved_block.GetRangeEnd());
304061da546Spatrick free_block.SetByteSize(bytes_left);
305061da546Spatrick }
306061da546Spatrick LLDB_LOGV(log, "({0}) (size = {1} ({1:x})) => {2:x}", this, size, addr);
307061da546Spatrick return addr;
308061da546Spatrick }
309061da546Spatrick }
310061da546Spatrick
311061da546Spatrick LLDB_LOGV(log, "({0}) (size = {1} ({1:x})) => {2:x}", this, size,
312061da546Spatrick LLDB_INVALID_ADDRESS);
313061da546Spatrick return LLDB_INVALID_ADDRESS;
314061da546Spatrick }
315061da546Spatrick
FreeBlock(addr_t addr)316061da546Spatrick bool AllocatedBlock::FreeBlock(addr_t addr) {
317061da546Spatrick bool success = false;
318061da546Spatrick auto entry_idx = m_reserved_blocks.FindEntryIndexThatContains(addr);
319061da546Spatrick if (entry_idx != UINT32_MAX)
320061da546Spatrick {
321061da546Spatrick m_free_blocks.Insert(m_reserved_blocks.GetEntryRef(entry_idx), true);
322061da546Spatrick m_reserved_blocks.RemoveEntryAtIndex(entry_idx);
323061da546Spatrick success = true;
324061da546Spatrick }
325*f6aab3d8Srobert Log *log = GetLog(LLDBLog::Process);
326061da546Spatrick LLDB_LOGV(log, "({0}) (addr = {1:x}) => {2}", this, addr, success);
327061da546Spatrick return success;
328061da546Spatrick }
329061da546Spatrick
AllocatedMemoryCache(Process & process)330061da546Spatrick AllocatedMemoryCache::AllocatedMemoryCache(Process &process)
331061da546Spatrick : m_process(process), m_mutex(), m_memory_map() {}
332061da546Spatrick
333be691f3bSpatrick AllocatedMemoryCache::~AllocatedMemoryCache() = default;
334061da546Spatrick
Clear(bool deallocate_memory)335*f6aab3d8Srobert void AllocatedMemoryCache::Clear(bool deallocate_memory) {
336061da546Spatrick std::lock_guard<std::recursive_mutex> guard(m_mutex);
337*f6aab3d8Srobert if (m_process.IsAlive() && deallocate_memory) {
338061da546Spatrick PermissionsToBlockMap::iterator pos, end = m_memory_map.end();
339061da546Spatrick for (pos = m_memory_map.begin(); pos != end; ++pos)
340061da546Spatrick m_process.DoDeallocateMemory(pos->second->GetBaseAddress());
341061da546Spatrick }
342061da546Spatrick m_memory_map.clear();
343061da546Spatrick }
344061da546Spatrick
345061da546Spatrick AllocatedMemoryCache::AllocatedBlockSP
AllocatePage(uint32_t byte_size,uint32_t permissions,uint32_t chunk_size,Status & error)346061da546Spatrick AllocatedMemoryCache::AllocatePage(uint32_t byte_size, uint32_t permissions,
347061da546Spatrick uint32_t chunk_size, Status &error) {
348061da546Spatrick AllocatedBlockSP block_sp;
349061da546Spatrick const size_t page_size = 4096;
350061da546Spatrick const size_t num_pages = (byte_size + page_size - 1) / page_size;
351061da546Spatrick const size_t page_byte_size = num_pages * page_size;
352061da546Spatrick
353061da546Spatrick addr_t addr = m_process.DoAllocateMemory(page_byte_size, permissions, error);
354061da546Spatrick
355*f6aab3d8Srobert Log *log = GetLog(LLDBLog::Process);
356061da546Spatrick if (log) {
357061da546Spatrick LLDB_LOGF(log,
358061da546Spatrick "Process::DoAllocateMemory (byte_size = 0x%8.8" PRIx32
359061da546Spatrick ", permissions = %s) => 0x%16.16" PRIx64,
360061da546Spatrick (uint32_t)page_byte_size, GetPermissionsAsCString(permissions),
361061da546Spatrick (uint64_t)addr);
362061da546Spatrick }
363061da546Spatrick
364061da546Spatrick if (addr != LLDB_INVALID_ADDRESS) {
365061da546Spatrick block_sp = std::make_shared<AllocatedBlock>(addr, page_byte_size,
366061da546Spatrick permissions, chunk_size);
367061da546Spatrick m_memory_map.insert(std::make_pair(permissions, block_sp));
368061da546Spatrick }
369061da546Spatrick return block_sp;
370061da546Spatrick }
371061da546Spatrick
AllocateMemory(size_t byte_size,uint32_t permissions,Status & error)372061da546Spatrick lldb::addr_t AllocatedMemoryCache::AllocateMemory(size_t byte_size,
373061da546Spatrick uint32_t permissions,
374061da546Spatrick Status &error) {
375061da546Spatrick std::lock_guard<std::recursive_mutex> guard(m_mutex);
376061da546Spatrick
377061da546Spatrick addr_t addr = LLDB_INVALID_ADDRESS;
378061da546Spatrick std::pair<PermissionsToBlockMap::iterator, PermissionsToBlockMap::iterator>
379061da546Spatrick range = m_memory_map.equal_range(permissions);
380061da546Spatrick
381061da546Spatrick for (PermissionsToBlockMap::iterator pos = range.first; pos != range.second;
382061da546Spatrick ++pos) {
383061da546Spatrick addr = (*pos).second->ReserveBlock(byte_size);
384061da546Spatrick if (addr != LLDB_INVALID_ADDRESS)
385061da546Spatrick break;
386061da546Spatrick }
387061da546Spatrick
388061da546Spatrick if (addr == LLDB_INVALID_ADDRESS) {
389061da546Spatrick AllocatedBlockSP block_sp(AllocatePage(byte_size, permissions, 16, error));
390061da546Spatrick
391061da546Spatrick if (block_sp)
392061da546Spatrick addr = block_sp->ReserveBlock(byte_size);
393061da546Spatrick }
394*f6aab3d8Srobert Log *log = GetLog(LLDBLog::Process);
395061da546Spatrick LLDB_LOGF(log,
396061da546Spatrick "AllocatedMemoryCache::AllocateMemory (byte_size = 0x%8.8" PRIx32
397061da546Spatrick ", permissions = %s) => 0x%16.16" PRIx64,
398061da546Spatrick (uint32_t)byte_size, GetPermissionsAsCString(permissions),
399061da546Spatrick (uint64_t)addr);
400061da546Spatrick return addr;
401061da546Spatrick }
402061da546Spatrick
DeallocateMemory(lldb::addr_t addr)403061da546Spatrick bool AllocatedMemoryCache::DeallocateMemory(lldb::addr_t addr) {
404061da546Spatrick std::lock_guard<std::recursive_mutex> guard(m_mutex);
405061da546Spatrick
406061da546Spatrick PermissionsToBlockMap::iterator pos, end = m_memory_map.end();
407061da546Spatrick bool success = false;
408061da546Spatrick for (pos = m_memory_map.begin(); pos != end; ++pos) {
409061da546Spatrick if (pos->second->Contains(addr)) {
410061da546Spatrick success = pos->second->FreeBlock(addr);
411061da546Spatrick break;
412061da546Spatrick }
413061da546Spatrick }
414*f6aab3d8Srobert Log *log = GetLog(LLDBLog::Process);
415061da546Spatrick LLDB_LOGF(log,
416061da546Spatrick "AllocatedMemoryCache::DeallocateMemory (addr = 0x%16.16" PRIx64
417061da546Spatrick ") => %i",
418061da546Spatrick (uint64_t)addr, success);
419061da546Spatrick return success;
420061da546Spatrick }
421