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 // Other libraries and framework includes
21 #include "lldb/lldb-private.h"
22 #include "lldb/Core/RangeMap.h"
23 #include "lldb/Host/Mutex.h"
25 namespace lldb_private {
26 //----------------------------------------------------------------------
27 // A class to track memory that was read from a live process between
29 //----------------------------------------------------------------------
33 //------------------------------------------------------------------
34 // Constructors and Destructors
35 //------------------------------------------------------------------
36 MemoryCache (Process &process);
41 Clear(bool clear_invalid_ranges = false);
44 Flush (lldb::addr_t addr, size_t size);
47 Read (lldb::addr_t addr,
53 GetMemoryCacheLineSize() const
55 return m_L2_cache_line_byte_size ;
59 AddInvalidRange (lldb::addr_t base_addr, lldb::addr_t byte_size);
62 RemoveInvalidRange (lldb::addr_t base_addr, lldb::addr_t byte_size);
64 // Allow external sources to populate data into the L1 memory cache
66 AddL1CacheData(lldb::addr_t addr, const void *src, size_t src_len);
69 AddL1CacheData(lldb::addr_t addr, const lldb::DataBufferSP &data_buffer_sp);
72 typedef std::map<lldb::addr_t, lldb::DataBufferSP> BlockMap;
73 typedef RangeArray<lldb::addr_t, lldb::addr_t, 4> InvalidRanges;
74 typedef Range<lldb::addr_t, lldb::addr_t> AddrRange;
75 //------------------------------------------------------------------
76 // Classes that inherit from MemoryCache can see and modify these
77 //------------------------------------------------------------------
79 BlockMap m_L1_cache; // A first level memory cache whose chunk sizes vary that will be used only if the memory read fits entirely in a chunk
80 BlockMap m_L2_cache; // A memory cache of fixed size chinks (m_L2_cache_line_byte_size bytes in size each)
81 InvalidRanges m_invalid_ranges;
83 uint32_t m_L2_cache_line_byte_size;
85 DISALLOW_COPY_AND_ASSIGN (MemoryCache);
92 AllocatedBlock (lldb::addr_t addr,
100 ReserveBlock (uint32_t size);
103 FreeBlock (lldb::addr_t addr);
106 GetBaseAddress () const
118 GetPermissions () const
120 return m_permissions;
124 GetChunkSize () const
130 Contains (lldb::addr_t addr) const
132 return ((addr >= m_addr) && addr < (m_addr + m_byte_size));
138 return m_byte_size / m_chunk_size;
142 CalculateChunksNeededForSize (uint32_t size) const
144 return (size + m_chunk_size - 1) / m_chunk_size;
146 const lldb::addr_t m_addr; // Base address of this block of memory
147 const uint32_t m_byte_size; // 4GB of chunk should be enough...
148 const uint32_t m_permissions; // Permissions for this memory (logical OR of lldb::Permissions bits)
149 const uint32_t m_chunk_size; // The size of chunks that the memory at m_addr is divied up into
150 typedef std::map<uint32_t, uint32_t> OffsetToChunkSize;
151 OffsetToChunkSize m_offset_to_chunk_size;
155 //----------------------------------------------------------------------
156 // A class that can track allocated memory and give out allocated memory
157 // without us having to make an allocate/deallocate call every time we
158 // need some memory in a process that is being debugged.
159 //----------------------------------------------------------------------
160 class AllocatedMemoryCache
163 //------------------------------------------------------------------
164 // Constructors and Destructors
165 //------------------------------------------------------------------
166 AllocatedMemoryCache (Process &process);
168 ~AllocatedMemoryCache ();
174 AllocateMemory (size_t byte_size,
175 uint32_t permissions,
179 DeallocateMemory (lldb::addr_t ptr);
182 typedef std::shared_ptr<AllocatedBlock> AllocatedBlockSP;
185 AllocatePage (uint32_t byte_size,
186 uint32_t permissions,
191 //------------------------------------------------------------------
192 // Classes that inherit from MemoryCache can see and modify these
193 //------------------------------------------------------------------
196 typedef std::multimap<uint32_t, AllocatedBlockSP> PermissionsToBlockMap;
197 PermissionsToBlockMap m_memory_map;
200 DISALLOW_COPY_AND_ASSIGN (AllocatedMemoryCache);
203 } // namespace lldb_private
205 #endif // liblldb_Memory_h_