1 //===-- Memory.h ------------------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef liblldb_Memory_h_
11 #define liblldb_Memory_h_
18 #include "lldb/Core/RangeMap.h"
19 #include "lldb/lldb-private.h"
21 namespace lldb_private {
22 //----------------------------------------------------------------------
23 // A class to track memory that was read from a live process between
25 //----------------------------------------------------------------------
28 //------------------------------------------------------------------
29 // Constructors and Destructors
30 //------------------------------------------------------------------
31 MemoryCache(Process &process);
35 void Clear(bool clear_invalid_ranges = false);
37 void Flush(lldb::addr_t addr, size_t size);
39 size_t Read(lldb::addr_t addr, void *dst, size_t dst_len, Status &error);
41 uint32_t GetMemoryCacheLineSize() const { return m_L2_cache_line_byte_size; }
43 void AddInvalidRange(lldb::addr_t base_addr, lldb::addr_t byte_size);
45 bool RemoveInvalidRange(lldb::addr_t base_addr, lldb::addr_t byte_size);
47 // Allow external sources to populate data into the L1 memory cache
48 void AddL1CacheData(lldb::addr_t addr, const void *src, size_t src_len);
50 void AddL1CacheData(lldb::addr_t addr,
51 const lldb::DataBufferSP &data_buffer_sp);
54 typedef std::map<lldb::addr_t, lldb::DataBufferSP> BlockMap;
55 typedef RangeArray<lldb::addr_t, lldb::addr_t, 4> InvalidRanges;
56 typedef Range<lldb::addr_t, lldb::addr_t> AddrRange;
57 //------------------------------------------------------------------
58 // Classes that inherit from MemoryCache can see and modify these
59 //------------------------------------------------------------------
60 std::recursive_mutex m_mutex;
61 BlockMap m_L1_cache; // A first level memory cache whose chunk sizes vary that
62 // will be used only if the memory read fits entirely in
64 BlockMap m_L2_cache; // A memory cache of fixed size chinks
65 // (m_L2_cache_line_byte_size bytes in size each)
66 InvalidRanges m_invalid_ranges;
68 uint32_t m_L2_cache_line_byte_size;
71 DISALLOW_COPY_AND_ASSIGN(MemoryCache);
76 class AllocatedBlock {
78 AllocatedBlock(lldb::addr_t addr, uint32_t byte_size, uint32_t permissions,
83 lldb::addr_t ReserveBlock(uint32_t size);
85 bool FreeBlock(lldb::addr_t addr);
87 lldb::addr_t GetBaseAddress() const { return m_range.GetRangeBase(); }
89 uint32_t GetByteSize() const { return m_range.GetByteSize(); }
91 uint32_t GetPermissions() const { return m_permissions; }
93 uint32_t GetChunkSize() const { return m_chunk_size; }
95 bool Contains(lldb::addr_t addr) const {
96 return m_range.Contains(addr);
100 uint32_t TotalChunks() const { return GetByteSize() / GetChunkSize(); }
102 uint32_t CalculateChunksNeededForSize(uint32_t size) const {
103 return (size + m_chunk_size - 1) / m_chunk_size;
105 // Base address of this block of memory 4GB of chunk should be enough.
106 Range<lldb::addr_t, uint32_t> m_range;
107 // Permissions for this memory (logical OR of lldb::Permissions bits)
108 const uint32_t m_permissions;
109 // The size of chunks that the memory at m_addr is divied up into.
110 const uint32_t m_chunk_size;
111 // A sorted list of free address ranges.
112 RangeVector<lldb::addr_t, uint32_t> m_free_blocks;
113 // A sorted list of reserved address.
114 RangeVector<lldb::addr_t, uint32_t> m_reserved_blocks;
117 //----------------------------------------------------------------------
118 // A class that can track allocated memory and give out allocated memory
119 // without us having to make an allocate/deallocate call every time we need
120 // some memory in a process that is being debugged.
121 //----------------------------------------------------------------------
122 class AllocatedMemoryCache {
124 //------------------------------------------------------------------
125 // Constructors and Destructors
126 //------------------------------------------------------------------
127 AllocatedMemoryCache(Process &process);
129 ~AllocatedMemoryCache();
133 lldb::addr_t AllocateMemory(size_t byte_size, uint32_t permissions,
136 bool DeallocateMemory(lldb::addr_t ptr);
139 typedef std::shared_ptr<AllocatedBlock> AllocatedBlockSP;
141 AllocatedBlockSP AllocatePage(uint32_t byte_size, uint32_t permissions,
142 uint32_t chunk_size, Status &error);
144 //------------------------------------------------------------------
145 // Classes that inherit from MemoryCache can see and modify these
146 //------------------------------------------------------------------
148 std::recursive_mutex m_mutex;
149 typedef std::multimap<uint32_t, AllocatedBlockSP> PermissionsToBlockMap;
150 PermissionsToBlockMap m_memory_map;
153 DISALLOW_COPY_AND_ASSIGN(AllocatedMemoryCache);
156 } // namespace lldb_private
158 #endif // liblldb_Memory_h_