22 static DWORD getWindowsProtectionFlags(
unsigned Flags) {
30 return PAGE_READWRITE;
32 return PAGE_READWRITE;
34 return PAGE_EXECUTE_READ;
38 return PAGE_EXECUTE_READWRITE;
51 static size_t getAllocationGranularity() {
53 ::GetSystemInfo(&Info);
54 if (Info.dwPageSize > Info.dwAllocationGranularity)
55 return Info.dwPageSize;
57 return Info.dwAllocationGranularity;
64 static size_t enableProcessLargePages() {
66 size_t LargePageMin = GetLargePageMinimum();
68 OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY,
73 if (!LookupPrivilegeValue(0, SE_LOCK_MEMORY_NAME, &Luid)) {
77 TOKEN_PRIVILEGES TP{};
78 TP.PrivilegeCount = 1;
79 TP.Privileges[0].Luid = Luid;
80 TP.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
81 if (!AdjustTokenPrivileges(Token, FALSE, &TP, 0, 0, 0)) {
87 if (E == ERROR_SUCCESS)
101 const MemoryBlock *
const NearBlock,
103 std::error_code &EC) {
104 EC = std::error_code();
106 return MemoryBlock();
108 static size_t DefaultGranularity = getAllocationGranularity();
109 static size_t LargePageGranularity = enableProcessLargePages();
112 bool HugePages =
false;
113 size_t Granularity = DefaultGranularity;
115 if ((Flags &
MF_HUGE_HINT) && LargePageGranularity > 0) {
116 AllocType |= MEM_LARGE_PAGES;
118 Granularity = LargePageGranularity;
121 size_t NumBlocks = (NumBytes + Granularity - 1) / Granularity;
123 uintptr_t Start = NearBlock ?
reinterpret_cast<uintptr_t
>(NearBlock->base()) +
124 NearBlock->allocatedSize()
129 if (Start && Start % Granularity != 0)
130 Start += Granularity - Start % Granularity;
132 DWORD Protect = getWindowsProtectionFlags(Flags);
134 size_t AllocSize = NumBlocks * Granularity;
135 void *PA = ::VirtualAlloc(reinterpret_cast<void *>(Start),
136 AllocSize, AllocType, Protect);
138 if (NearBlock || HugePages) {
143 return MemoryBlock();
148 Result.AllocatedSize = AllocSize;
149 Result.Flags = (Flags & ~MF_HUGE_HINT) | (HugePages ? MF_HUGE_HINT : 0);
158 if (M.Address == 0 || M.AllocatedSize == 0)
159 return std::error_code();
161 if (!VirtualFree(M.Address, 0, MEM_RELEASE))
167 return std::error_code();
172 if (M.Address == 0 || M.AllocatedSize == 0)
173 return std::error_code();
175 DWORD Protect = getWindowsProtectionFlags(Flags);
178 if (!VirtualProtect(M.Address, M.AllocatedSize, Protect, &OldFlags))
184 return std::error_code();
191 const void *Addr,
size_t Len) {
192 FlushInstructionCache(GetCurrentProcess(), Addr, Len);
This class represents lattice values for constants.
static std::error_code releaseMappedMemory(MemoryBlock &Block)
This method releases a block of memory that was allocated with the allocateMappedMemory method...
The MF_HUGE_HINT flag is used to indicate that the request for a memory block should be satisfied wit...
static MemoryBlock allocateMappedMemory(size_t NumBytes, const MemoryBlock *const NearBlock, unsigned Flags, std::error_code &EC)
This method allocates a block of memory that is suitable for loading dynamically generated code (e...
static void InvalidateInstructionCache(const void *Addr, size_t Len)
InvalidateInstructionCache - Before the JIT can run a block of code that has been emitted it must inv...
Analysis containing CSE Info
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
std::error_code mapWindowsError(unsigned EV)
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Provides a library for accessing information about this process and other processes on the operating ...
static std::error_code protectMappedMemory(const MemoryBlock &Block, unsigned Flags)
This method sets the protection flags for a block of memory to the state specified by /p Flags...