Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
virtual_memory_win.cc
Go to the documentation of this file.
1// Copyright (c) 2021, the Dart project authors. Please see the AUTHORS file
2// for details. All rights reserved. Use of this source code is governed by a
3// BSD-style license that can be found in the LICENSE file.
4
5#include "platform/globals.h"
6#if defined(DART_HOST_OS_WINDOWS)
7
9
10#include "platform/assert.h"
11#include "platform/utils.h"
12
13namespace dart {
14namespace bin {
15
16uword VirtualMemory::page_size_ = 0;
17
18intptr_t VirtualMemory::CalculatePageSize() {
19 SYSTEM_INFO info;
20 GetSystemInfo(&info);
21 const intptr_t page_size = info.dwPageSize;
22 ASSERT(page_size != 0);
23 ASSERT(Utils::IsPowerOfTwo(page_size));
24 return page_size;
25}
26
27VirtualMemory* VirtualMemory::Allocate(intptr_t size,
28 bool is_executable,
29 const char* name) {
31 int prot = is_executable ? PAGE_EXECUTE_READWRITE : PAGE_READWRITE;
32 void* address = VirtualAlloc(nullptr, size, MEM_RESERVE | MEM_COMMIT, prot);
33 if (address == nullptr) {
34 return nullptr;
35 }
36 return new VirtualMemory(address, size);
37}
38
40 if (address_ != nullptr) {
41 if (VirtualFree(address_, 0, MEM_RELEASE) == 0) {
42 FATAL("VirtualFree failed: Error code %d\n", GetLastError());
43 }
44 }
45}
46
47void VirtualMemory::Protect(void* address, intptr_t size, Protection mode) {
48 uword start_address = reinterpret_cast<uword>(address);
49 uword end_address = start_address + size;
50 uword page_address = Utils::RoundDown(start_address, PageSize());
51 DWORD prot = 0;
52 switch (mode) {
53 case kNoAccess:
54 prot = PAGE_NOACCESS;
55 break;
56 case kReadOnly:
57 prot = PAGE_READONLY;
58 break;
59 case kReadWrite:
60 prot = PAGE_READWRITE;
61 break;
62 case kReadExecute:
63 prot = PAGE_EXECUTE_READ;
64 break;
66 prot = PAGE_EXECUTE_READWRITE;
67 break;
68 }
69 DWORD old_prot = 0;
70 if (VirtualProtect(reinterpret_cast<void*>(page_address),
71 end_address - page_address, prot, &old_prot) == 0) {
72 FATAL("VirtualProtect failed %d\n", GetLastError());
73 }
74}
75
76} // namespace bin
77} // namespace dart
78
79#endif // defined(DART_HOST_OS_WINDOWS)
static void info(const char *fmt,...) SK_PRINTF_LIKE(1
Definition DM.cpp:213
static constexpr T RoundDown(T x, intptr_t alignment)
Definition utils.h:93
static constexpr bool IsAligned(T x, uintptr_t alignment, uintptr_t offset=0)
Definition utils.h:77
static constexpr bool IsPowerOfTwo(T x)
Definition utils.h:61
static VirtualMemory * Allocate(intptr_t size, bool is_executable, const char *name)
static void Protect(void *address, intptr_t size, Protection mode)
static intptr_t PageSize()
#define ASSERT(E)
#define FATAL(error)
const char *const name
uintptr_t uword
Definition globals.h:501
WINBASEAPI _Check_return_ _Post_equals_last_error_ DWORD WINAPI GetLastError(VOID)
unsigned long DWORD