1// Licensed to the .NET Foundation under one or more agreements.
2// The .NET Foundation licenses this file to you under the MIT license.
3// See the LICENSE file in the project root for more information.
4//*****************************************************************************
5// MemoryRange.h
6//
7// defines the code:MemoryRange class.
8//*****************************************************************************
9
10#ifndef _memory_range_h
11#define _memory_range_h
12
13#include "daccess.h"
14
15// MemoryRange is a descriptor of a memory range. This groups (pointer + size).
16//
17// Some key qualities:
18// - simple!
19// - Not mutable
20// - blitabble descriptor which can be useful for out-of-process tools like the debugger.
21// - no ownership semantics.
22// - no manipulation, growing semantics.
23// - no memory marshalling, allocation, copying. etc.
24// - can be efficiently passed / copied / returned by value
25//
26// This class has general value as an abstraction to group pointer and size together. It also has significant
27// value to the debugger. An expected design pattern is that other mutable complex data structures (eg,
28// code:SBuffer, code:CGrowableStream) will provide an accessor to expose their underlying storage as a
29// MemoryRange to debugger. This mirrors the Debugger's code:TargetBuffer data structure, but as a
30// general-purpose VM utility versus a debugger right-side data structure.
31
32//
33class MemoryRange
34{
35public:
36 // Constructor to create a memory range around a (start address, size) pair.
37 MemoryRange() :
38 m_pStartAddress(NULL),
39 m_cbBytes(0)
40 {
41 SUPPORTS_DAC;
42 }
43
44 MemoryRange(PTR_VOID pStartAddress, SIZE_T cbBytes) :
45 m_pStartAddress(pStartAddress),
46 m_cbBytes(cbBytes)
47 {
48 SUPPORTS_DAC;
49 }
50
51 // Note: use compiler-default copy ctor and assignment operator
52
53
54
55 // Check whether a pointer is in the memory range represented by this instance.
56 BOOL IsInRange(PTR_VOID pAddress) const
57 {
58 LIMITED_METHOD_DAC_CONTRACT;
59
60 return (dac_cast<TADDR>(pAddress) - dac_cast<TADDR>(m_pStartAddress)) < m_cbBytes;
61 }
62
63 // Check whether a pointer is in the memory range represented by this instance.
64 BOOL IsInRange(TADDR pAddress) const
65 {
66 LIMITED_METHOD_DAC_CONTRACT;
67
68 return (pAddress - dac_cast<TADDR>(m_pStartAddress)) < m_cbBytes;
69 }
70
71 // Get the starting address.
72 PTR_VOID StartAddress() const
73 {
74 SUPPORTS_DAC;
75 return m_pStartAddress;
76 }
77
78 // Get the size of the range in bytes
79 SIZE_T Size() const
80 {
81 SUPPORTS_DAC;
82 return m_cbBytes;
83 }
84
85private:
86 // The start of the memory range.
87 PTR_VOID const m_pStartAddress;
88
89 // The size of the memory range in bytes.
90 // This is s SIZE_T so that it can describe any memory range in the process (for example, larger than 4gb on 64-bit machines)
91 const SIZE_T m_cbBytes;
92
93};
94
95typedef ArrayDPTR(MemoryRange) ARRAY_PTR_MemoryRange;
96
97#endif // _memory_range_h
98
99