================
@@ -8,47 +8,183 @@
#include "lldb/Target/Memory.h"
#include "lldb/Target/Process.h"
-#include "lldb/Utility/DataBufferHeap.h"
#include "lldb/Utility/LLDBLog.h"
#include "lldb/Utility/Log.h"
#include "lldb/Utility/RangeMap.h"
#include "lldb/Utility/State.h"
#include "llvm/ADT/STLExtras.h"
+#include "llvm/Support/MathExtras.h"
+#include <algorithm>
#include <cinttypes>
#include <memory>
+#include <utility>
using namespace lldb;
using namespace lldb_private;
+llvm::ArrayRef<uint8_t> LineCache::Lookup(addr_t addr) const {
+ const auto pos = m_lines.find(IndexOf(addr));
+ if (pos == m_lines.end())
+ return {};
+ const addr_t line_offset = addr % m_line_byte_size;
+ return llvm::ArrayRef(pos->second.get(), m_line_byte_size)
+ .drop_front(line_offset);
+}
+
+void LineCache::Insert(addr_t addr, llvm::ArrayRef<uint8_t> src) {
+ assert((addr % m_line_byte_size) == 0 &&
+ "whole line inserted at an unaligned address!");
+ assert(src.size() == m_line_byte_size &&
+ "whole line inserted with a partial buffer!");
+ auto line = std::make_unique<uint8_t[]>(m_line_byte_size);
+ std::copy(src.begin(), src.end(), line.get());
+ m_lines[IndexOf(addr)] = std::move(line);
+}
+
+void LineCache::EraseRange(addr_t addr, addr_t size) {
+ if (size == 0)
+ return;
+ // Clamp a range running past the end of the address space to it.
+ const addr_t end_addr = llvm::SaturatingAdd(addr, size - 1);
+ const uint64_t first_idx = IndexOf(addr);
+ const uint64_t last_idx = IndexOf(end_addr);
+ m_lines.remove_if([first_idx, last_idx](const auto &entry) {
+ return entry.getFirst() >= first_idx && entry.getFirst() <= last_idx;
+ });
+}
+
+ChunkCache::Collection::const_iterator
+ChunkCache::FindChunkContaining(addr_t addr) const {
+ if (m_chunks.empty())
+ return m_chunks.end();
+ Collection::const_iterator pos = m_chunks.upper_bound(addr);
+ if (pos == m_chunks.begin())
+ return m_chunks.end();
+ --pos;
+ // Sum pos->first + size wraps at the top of the address space.
+ return addr - pos->first < pos->second.size() ? pos : m_chunks.end();
+}
+
+llvm::ArrayRef<uint8_t> ChunkCache::Lookup(addr_t addr) const {
+ const Collection::const_iterator pos = FindChunkContaining(addr);
+ if (pos == m_chunks.end())
+ return {};
+ return llvm::ArrayRef(pos->second).drop_front(addr - pos->first);
+}
+
+void ChunkCache::InsertMissing(addr_t addr, llvm::ArrayRef<uint8_t> src) {
+ if (src.empty())
+ return;
+ // The last addressable byte of the range, clamped if it runs past the end of
+ // the address space.
+ const addr_t last_addr = llvm::SaturatingAdd<addr_t>(addr, src.size() - 1);
+ const uint64_t len = last_addr - addr + 1;
+
+ for (uint64_t offset = 0; offset < len;) {
+ const addr_t curr_addr = addr + offset;
+ if (const llvm::ArrayRef<uint8_t> held = Lookup(curr_addr); !held.empty())
{
+ offset += std::min<uint64_t>(held.size(), len - offset);
+ continue;
+ }
+ // Nothing holds curr_addr, so the gap runs to the next chunk or to the
end.
+ const Collection::const_iterator next = m_chunks.lower_bound(curr_addr);
----------------
felipepiovezan wrote:
auto
https://github.com/llvm/llvm-project/pull/222688
_______________________________________________
lldb-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits