[Lldb-commits] [lldb] [lldb][NFC] Factor out helper code from MemoryCache (PR #201120)
Felipe de Azevedo Piovezan via lldb-commits
lldb-commits at lists.llvm.org
Tue Jun 2 06:32:02 PDT 2026
https://github.com/felipepiovezan created https://github.com/llvm/llvm-project/pull/201120
This will be reused in a subsequent commit.
>From 2ef62a60f5aa7fd97c55c8a1251d60a550f37a74 Mon Sep 17 00:00:00 2001
From: Felipe de Azevedo Piovezan <fpiovezan at apple.com>
Date: Tue, 2 Jun 2026 14:27:52 +0100
Subject: [PATCH] [lldb][NFC] Factor out helper code from MemoryCache
This will be reused in a subsequent commit.
(cherry picked from commit 80aa4efa1e6edfb7e42f675ce18b8d3ece588262)
---
lldb/include/lldb/Target/Memory.h | 5 +++++
lldb/source/Target/Memory.cpp | 29 +++++++++++++++++------------
2 files changed, 22 insertions(+), 12 deletions(-)
diff --git a/lldb/include/lldb/Target/Memory.h b/lldb/include/lldb/Target/Memory.h
index 864ef6ca00802..85584f29ec7e7 100644
--- a/lldb/include/lldb/Target/Memory.h
+++ b/lldb/include/lldb/Target/Memory.h
@@ -63,6 +63,11 @@ class MemoryCache {
const MemoryCache &operator=(const MemoryCache &) = delete;
lldb::DataBufferSP GetL2CacheLine(lldb::addr_t addr, Status &error);
+
+ // If the entire range [addr, addr+len) is covered by a single L1 entry,
+ // returns a pointer into that entry's data at the correct offset. Returns
+ // nullptr on a miss. Caller must hold m_mutex.
+ const uint8_t *FindL1CacheEntry(lldb::addr_t addr, size_t len) const;
};
diff --git a/lldb/source/Target/Memory.cpp b/lldb/source/Target/Memory.cpp
index d0aa67f66d20c..6c4650f1eb2d7 100644
--- a/lldb/source/Target/Memory.cpp
+++ b/lldb/source/Target/Memory.cpp
@@ -123,6 +123,20 @@ bool MemoryCache::RemoveInvalidRange(lldb::addr_t base_addr,
return false;
}
+const uint8_t *MemoryCache::FindL1CacheEntry(lldb::addr_t addr,
+ size_t len) const {
+ if (m_L1_cache.empty())
+ return nullptr;
+ AddrRange read_range(addr, len);
+ BlockMap::const_iterator pos = m_L1_cache.upper_bound(addr);
+ if (pos != m_L1_cache.begin())
+ --pos;
+ AddrRange chunk_range(pos->first, pos->second->GetByteSize());
+ if (!chunk_range.Contains(read_range))
+ return nullptr;
+ return pos->second->GetBytes() + (addr - chunk_range.GetRangeBase());
+}
+
lldb::DataBufferSP MemoryCache::GetL2CacheLine(lldb::addr_t line_base_addr,
Status &error) {
// This function assumes that the address given is aligned correctly.
@@ -173,18 +187,9 @@ size_t MemoryCache::Read(addr_t addr, void *dst, size_t dst_len,
// L1 cache contains chunks of memory that are not required to be the size of
// an L2 cache line. We avoid trying to do partial reads from the L1 cache to
// simplify the implementation.
- if (!m_L1_cache.empty()) {
- AddrRange read_range(addr, dst_len);
- BlockMap::iterator pos = m_L1_cache.upper_bound(addr);
- if (pos != m_L1_cache.begin()) {
- --pos;
- }
- AddrRange chunk_range(pos->first, pos->second->GetByteSize());
- if (chunk_range.Contains(read_range)) {
- memcpy(dst, pos->second->GetBytes() + (addr - chunk_range.GetRangeBase()),
- dst_len);
- return dst_len;
- }
+ if (const uint8_t *l1_data = FindL1CacheEntry(addr, dst_len)) {
+ memcpy(dst, l1_data, dst_len);
+ return dst_len;
}
// If the size of the read is greater than the size of an L2 cache line, we'll
More information about the lldb-commits
mailing list