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_
19 // Other libraries and framework includes
22 #include "lldb/Core/RangeMap.h"
23 #include "lldb/lldb-private.h"
25 namespace lldb_private {
26 //----------------------------------------------------------------------
27 // A class to track memory that was read from a live process between
29 //----------------------------------------------------------------------
32 //------------------------------------------------------------------
33 // Constructors and Destructors
34 //------------------------------------------------------------------
35 MemoryCache(Process &process);
39 void Clear(bool clear_invalid_ranges = false);
41 void Flush(lldb::addr_t addr, size_t size);
43 size_t Read(lldb::addr_t addr, void *dst, size_t dst_len, Error &error);
45 uint32_t GetMemoryCacheLineSize() const { return m_L2_cache_line_byte_size; }
47 void AddInvalidRange(lldb::addr_t base_addr, lldb::addr_t byte_size);
49 bool RemoveInvalidRange(lldb::addr_t base_addr, lldb::addr_t byte_size);
51 // Allow external sources to populate data into the L1 memory cache
52 void AddL1CacheData(lldb::addr_t addr, const void *src, size_t src_len);
54 void AddL1CacheData(lldb::addr_t addr,
55 const lldb::DataBufferSP &data_buffer_sp);
58 typedef std::map<lldb::addr_t, lldb::DataBufferSP> BlockMap;
59 typedef RangeArray<lldb::addr_t, lldb::addr_t, 4> InvalidRanges;
60 typedef Range<lldb::addr_t, lldb::addr_t> AddrRange;
61 //------------------------------------------------------------------
62 // Classes that inherit from MemoryCache can see and modify these
63 //------------------------------------------------------------------
64 std::recursive_mutex m_mutex;
65 BlockMap m_L1_cache; // A first level memory cache whose chunk sizes vary that
66 // will be used only if the memory read fits entirely in
68 BlockMap m_L2_cache; // A memory cache of fixed size chinks
69 // (m_L2_cache_line_byte_size bytes in size each)
70 InvalidRanges m_invalid_ranges;
72 uint32_t m_L2_cache_line_byte_size;
75 DISALLOW_COPY_AND_ASSIGN(MemoryCache);
78 class AllocatedBlock {
80 AllocatedBlock(lldb::addr_t addr, uint32_t byte_size, uint32_t permissions,
85 lldb::addr_t ReserveBlock(uint32_t size);
87 bool FreeBlock(lldb::addr_t addr);
89 lldb::addr_t GetBaseAddress() const { return m_addr; }
91 uint32_t GetByteSize() const { return m_byte_size; }
93 uint32_t GetPermissions() const { return m_permissions; }
95 uint32_t GetChunkSize() const { return m_chunk_size; }
97 bool Contains(lldb::addr_t addr) const {
98 return ((addr >= m_addr) && addr < (m_addr + m_byte_size));
102 uint32_t TotalChunks() const { return m_byte_size / m_chunk_size; }
104 uint32_t CalculateChunksNeededForSize(uint32_t size) const {
105 return (size + m_chunk_size - 1) / m_chunk_size;
107 const lldb::addr_t m_addr; // Base address of this block of memory
108 const uint32_t m_byte_size; // 4GB of chunk should be enough...
109 const uint32_t m_permissions; // Permissions for this memory (logical OR of
110 // lldb::Permissions bits)
111 const uint32_t m_chunk_size; // The size of chunks that the memory at m_addr
113 typedef std::map<uint32_t, uint32_t> OffsetToChunkSize;
114 OffsetToChunkSize m_offset_to_chunk_size;
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
120 // need 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, Error &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_