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_MACOS) | 6 #if defined(HOST_OS_MACOS) |
7 | 7 |
8 #include "vm/virtual_memory.h" | 8 #include "vm/virtual_memory.h" |
9 | 9 |
10 #include <sys/mman.h> // NOLINT | 10 #include <sys/mman.h> // NOLINT |
11 #include <unistd.h> // NOLINT | 11 #include <unistd.h> // NOLINT |
12 | 12 |
13 #include "platform/assert.h" | 13 #include "platform/assert.h" |
14 #include "platform/utils.h" | 14 #include "platform/utils.h" |
15 | 15 |
16 #include "vm/isolate.h" | 16 #include "vm/isolate.h" |
17 | 17 |
18 namespace dart { | 18 namespace dart { |
19 | 19 |
20 // standard MAP_FAILED causes "error: use of old-style cast" as it | 20 // standard MAP_FAILED causes "error: use of old-style cast" as it |
21 // defines MAP_FAILED as ((void *) -1) | 21 // defines MAP_FAILED as ((void *) -1) |
22 #undef MAP_FAILED | 22 #undef MAP_FAILED |
23 #define MAP_FAILED reinterpret_cast<void*>(-1) | 23 #define MAP_FAILED reinterpret_cast<void*>(-1) |
24 | 24 |
25 uword VirtualMemory::page_size_ = 0; | 25 uword VirtualMemory::page_size_ = 0; |
26 | 26 |
27 | |
28 void VirtualMemory::InitOnce() { | 27 void VirtualMemory::InitOnce() { |
29 page_size_ = getpagesize(); | 28 page_size_ = getpagesize(); |
30 } | 29 } |
31 | 30 |
32 | |
33 VirtualMemory* VirtualMemory::ReserveInternal(intptr_t size) { | 31 VirtualMemory* VirtualMemory::ReserveInternal(intptr_t size) { |
34 ASSERT((size & (PageSize() - 1)) == 0); | 32 ASSERT((size & (PageSize() - 1)) == 0); |
35 void* address = mmap(NULL, size, PROT_NONE, | 33 void* address = mmap(NULL, size, PROT_NONE, |
36 MAP_PRIVATE | MAP_ANON | MAP_NORESERVE, -1, 0); | 34 MAP_PRIVATE | MAP_ANON | MAP_NORESERVE, -1, 0); |
37 if (address == MAP_FAILED) { | 35 if (address == MAP_FAILED) { |
38 return NULL; | 36 return NULL; |
39 } | 37 } |
40 MemoryRegion region(address, size); | 38 MemoryRegion region(address, size); |
41 return new VirtualMemory(region); | 39 return new VirtualMemory(region); |
42 } | 40 } |
43 | 41 |
44 | |
45 static void unmap(void* address, intptr_t size) { | 42 static void unmap(void* address, intptr_t size) { |
46 if (size == 0) { | 43 if (size == 0) { |
47 return; | 44 return; |
48 } | 45 } |
49 | 46 |
50 if (munmap(address, size) != 0) { | 47 if (munmap(address, size) != 0) { |
51 FATAL("munmap failed\n"); | 48 FATAL("munmap failed\n"); |
52 } | 49 } |
53 } | 50 } |
54 | 51 |
55 | |
56 VirtualMemory::~VirtualMemory() { | 52 VirtualMemory::~VirtualMemory() { |
57 if (vm_owns_region()) { | 53 if (vm_owns_region()) { |
58 unmap(address(), reserved_size_); | 54 unmap(address(), reserved_size_); |
59 } | 55 } |
60 } | 56 } |
61 | 57 |
62 | |
63 bool VirtualMemory::FreeSubSegment(int32_t handle, | 58 bool VirtualMemory::FreeSubSegment(int32_t handle, |
64 void* address, | 59 void* address, |
65 intptr_t size) { | 60 intptr_t size) { |
66 unmap(address, size); | 61 unmap(address, size); |
67 return true; | 62 return true; |
68 } | 63 } |
69 | 64 |
70 | |
71 bool VirtualMemory::Commit(uword addr, | 65 bool VirtualMemory::Commit(uword addr, |
72 intptr_t size, | 66 intptr_t size, |
73 bool executable, | 67 bool executable, |
74 const char* name) { | 68 const char* name) { |
75 ASSERT(Contains(addr)); | 69 ASSERT(Contains(addr)); |
76 ASSERT(Contains(addr + size) || (addr + size == end())); | 70 ASSERT(Contains(addr + size) || (addr + size == end())); |
77 int prot = PROT_READ | PROT_WRITE | (executable ? PROT_EXEC : 0); | 71 int prot = PROT_READ | PROT_WRITE | (executable ? PROT_EXEC : 0); |
78 void* address = mmap(reinterpret_cast<void*>(addr), size, prot, | 72 void* address = mmap(reinterpret_cast<void*>(addr), size, prot, |
79 MAP_PRIVATE | MAP_ANON | MAP_FIXED, -1, 0); | 73 MAP_PRIVATE | MAP_ANON | MAP_FIXED, -1, 0); |
80 if (address == MAP_FAILED) { | 74 if (address == MAP_FAILED) { |
81 return false; | 75 return false; |
82 } | 76 } |
83 return true; | 77 return true; |
84 } | 78 } |
85 | 79 |
86 | |
87 bool VirtualMemory::Protect(void* address, intptr_t size, Protection mode) { | 80 bool VirtualMemory::Protect(void* address, intptr_t size, Protection mode) { |
88 ASSERT(Thread::Current()->IsMutatorThread() || | 81 ASSERT(Thread::Current()->IsMutatorThread() || |
89 Isolate::Current()->mutator_thread()->IsAtSafepoint()); | 82 Isolate::Current()->mutator_thread()->IsAtSafepoint()); |
90 uword start_address = reinterpret_cast<uword>(address); | 83 uword start_address = reinterpret_cast<uword>(address); |
91 uword end_address = start_address + size; | 84 uword end_address = start_address + size; |
92 uword page_address = Utils::RoundDown(start_address, PageSize()); | 85 uword page_address = Utils::RoundDown(start_address, PageSize()); |
93 int prot = 0; | 86 int prot = 0; |
94 switch (mode) { | 87 switch (mode) { |
95 case kNoAccess: | 88 case kNoAccess: |
96 prot = PROT_NONE; | 89 prot = PROT_NONE; |
(...skipping 11 matching lines...) Expand all Loading... |
108 prot = PROT_READ | PROT_WRITE | PROT_EXEC; | 101 prot = PROT_READ | PROT_WRITE | PROT_EXEC; |
109 break; | 102 break; |
110 } | 103 } |
111 return (mprotect(reinterpret_cast<void*>(page_address), | 104 return (mprotect(reinterpret_cast<void*>(page_address), |
112 end_address - page_address, prot) == 0); | 105 end_address - page_address, prot) == 0); |
113 } | 106 } |
114 | 107 |
115 } // namespace dart | 108 } // namespace dart |
116 | 109 |
117 #endif // defined(HOST_OS_MACOS) | 110 #endif // defined(HOST_OS_MACOS) |
OLD | NEW |