OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 #include "vm/globals.h" | 5 #include "vm/globals.h" |
6 #if defined(HOST_OS_WINDOWS) | 6 #if defined(HOST_OS_WINDOWS) |
7 | 7 |
8 #include "vm/virtual_memory.h" | 8 #include "vm/virtual_memory.h" |
9 | 9 |
10 #include "platform/assert.h" | 10 #include "platform/assert.h" |
11 #include "vm/os.h" | 11 #include "vm/os.h" |
12 | 12 |
13 #include "vm/isolate.h" | 13 #include "vm/isolate.h" |
14 | 14 |
15 namespace dart { | 15 namespace dart { |
16 | 16 |
17 uword VirtualMemory::page_size_ = 0; | 17 uword VirtualMemory::page_size_ = 0; |
18 | 18 |
19 | |
20 void VirtualMemory::InitOnce() { | 19 void VirtualMemory::InitOnce() { |
21 SYSTEM_INFO info; | 20 SYSTEM_INFO info; |
22 GetSystemInfo(&info); | 21 GetSystemInfo(&info); |
23 page_size_ = info.dwPageSize; | 22 page_size_ = info.dwPageSize; |
24 } | 23 } |
25 | 24 |
26 | |
27 VirtualMemory* VirtualMemory::ReserveInternal(intptr_t size) { | 25 VirtualMemory* VirtualMemory::ReserveInternal(intptr_t size) { |
28 void* address = VirtualAlloc(NULL, size, MEM_RESERVE, PAGE_NOACCESS); | 26 void* address = VirtualAlloc(NULL, size, MEM_RESERVE, PAGE_NOACCESS); |
29 if (address == NULL) { | 27 if (address == NULL) { |
30 return NULL; | 28 return NULL; |
31 } | 29 } |
32 MemoryRegion region(address, size); | 30 MemoryRegion region(address, size); |
33 return new VirtualMemory(region); | 31 return new VirtualMemory(region); |
34 } | 32 } |
35 | 33 |
36 | |
37 VirtualMemory::~VirtualMemory() { | 34 VirtualMemory::~VirtualMemory() { |
38 if (!vm_owns_region() || (reserved_size_ == 0)) { | 35 if (!vm_owns_region() || (reserved_size_ == 0)) { |
39 return; | 36 return; |
40 } | 37 } |
41 if (VirtualFree(address(), 0, MEM_RELEASE) == 0) { | 38 if (VirtualFree(address(), 0, MEM_RELEASE) == 0) { |
42 FATAL("VirtualFree failed"); | 39 FATAL("VirtualFree failed"); |
43 } | 40 } |
44 } | 41 } |
45 | 42 |
46 | |
47 bool VirtualMemory::FreeSubSegment(int32_t handle, | 43 bool VirtualMemory::FreeSubSegment(int32_t handle, |
48 void* address, | 44 void* address, |
49 intptr_t size) { | 45 intptr_t size) { |
50 // On Windows only the entire segment returned by VirtualAlloc | 46 // On Windows only the entire segment returned by VirtualAlloc |
51 // can be freed. Therefore we will have to waste these unused | 47 // can be freed. Therefore we will have to waste these unused |
52 // virtual memory sub-segments. | 48 // virtual memory sub-segments. |
53 return false; | 49 return false; |
54 } | 50 } |
55 | 51 |
56 | |
57 bool VirtualMemory::Commit(uword addr, | 52 bool VirtualMemory::Commit(uword addr, |
58 intptr_t size, | 53 intptr_t size, |
59 bool executable, | 54 bool executable, |
60 const char* name) { | 55 const char* name) { |
61 ASSERT(Contains(addr)); | 56 ASSERT(Contains(addr)); |
62 ASSERT(Contains(addr + size) || (addr + size == end())); | 57 ASSERT(Contains(addr + size) || (addr + size == end())); |
63 int prot = executable ? PAGE_EXECUTE_READWRITE : PAGE_READWRITE; | 58 int prot = executable ? PAGE_EXECUTE_READWRITE : PAGE_READWRITE; |
64 if (VirtualAlloc(reinterpret_cast<void*>(addr), size, MEM_COMMIT, prot) == | 59 if (VirtualAlloc(reinterpret_cast<void*>(addr), size, MEM_COMMIT, prot) == |
65 NULL) { | 60 NULL) { |
66 return false; | 61 return false; |
67 } | 62 } |
68 return true; | 63 return true; |
69 } | 64 } |
70 | 65 |
71 | |
72 bool VirtualMemory::Protect(void* address, intptr_t size, Protection mode) { | 66 bool VirtualMemory::Protect(void* address, intptr_t size, Protection mode) { |
73 ASSERT(Thread::Current()->IsMutatorThread() || | 67 ASSERT(Thread::Current()->IsMutatorThread() || |
74 Isolate::Current()->mutator_thread()->IsAtSafepoint()); | 68 Isolate::Current()->mutator_thread()->IsAtSafepoint()); |
75 uword start_address = reinterpret_cast<uword>(address); | 69 uword start_address = reinterpret_cast<uword>(address); |
76 uword end_address = start_address + size; | 70 uword end_address = start_address + size; |
77 uword page_address = Utils::RoundDown(start_address, PageSize()); | 71 uword page_address = Utils::RoundDown(start_address, PageSize()); |
78 DWORD prot = 0; | 72 DWORD prot = 0; |
79 switch (mode) { | 73 switch (mode) { |
80 case kNoAccess: | 74 case kNoAccess: |
81 prot = PAGE_NOACCESS; | 75 prot = PAGE_NOACCESS; |
(...skipping 13 matching lines...) Expand all Loading... |
95 } | 89 } |
96 DWORD old_prot = 0; | 90 DWORD old_prot = 0; |
97 bool result = VirtualProtect(reinterpret_cast<void*>(page_address), | 91 bool result = VirtualProtect(reinterpret_cast<void*>(page_address), |
98 end_address - page_address, prot, &old_prot); | 92 end_address - page_address, prot, &old_prot); |
99 return result; | 93 return result; |
100 } | 94 } |
101 | 95 |
102 } // namespace dart | 96 } // namespace dart |
103 | 97 |
104 #endif // defined(HOST_OS_WINDOWS) | 98 #endif // defined(HOST_OS_WINDOWS) |
OLD | NEW |