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(TARGET_OS_LINUX) | 6 #if defined(TARGET_OS_LINUX) |
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 | 27 |
28 void VirtualMemory::InitOnce() { | 28 void VirtualMemory::InitOnce() { |
29 page_size_ = getpagesize(); | 29 page_size_ = getpagesize(); |
30 } | 30 } |
31 | 31 |
32 | 32 |
33 VirtualMemory* VirtualMemory::ReserveInternal(intptr_t size) { | 33 VirtualMemory* VirtualMemory::ReserveInternal(intptr_t size) { |
34 void* address = mmap(NULL, size, PROT_NONE, | 34 void* address = mmap(NULL, size, PROT_NONE, |
35 MAP_PRIVATE | MAP_ANON | MAP_NORESERVE, | 35 MAP_PRIVATE | MAP_ANON | MAP_NORESERVE, -1, 0); |
36 -1, 0); | |
37 if (address == MAP_FAILED) { | 36 if (address == MAP_FAILED) { |
38 return NULL; | 37 return NULL; |
39 } | 38 } |
40 MemoryRegion region(address, size); | 39 MemoryRegion region(address, size); |
41 return new VirtualMemory(region); | 40 return new VirtualMemory(region); |
42 } | 41 } |
43 | 42 |
44 | 43 |
45 static void unmap(void* address, intptr_t size) { | 44 static void unmap(void* address, intptr_t size) { |
46 if (size == 0) { | 45 if (size == 0) { |
(...skipping 17 matching lines...) Expand all Loading... |
64 unmap(address, size); | 63 unmap(address, size); |
65 return true; | 64 return true; |
66 } | 65 } |
67 | 66 |
68 | 67 |
69 bool VirtualMemory::Commit(uword addr, intptr_t size, bool executable) { | 68 bool VirtualMemory::Commit(uword addr, intptr_t size, bool executable) { |
70 ASSERT(Contains(addr)); | 69 ASSERT(Contains(addr)); |
71 ASSERT(Contains(addr + size) || (addr + size == end())); | 70 ASSERT(Contains(addr + size) || (addr + size == end())); |
72 int prot = PROT_READ | PROT_WRITE | (executable ? PROT_EXEC : 0); | 71 int prot = PROT_READ | PROT_WRITE | (executable ? PROT_EXEC : 0); |
73 void* address = mmap(reinterpret_cast<void*>(addr), size, prot, | 72 void* address = mmap(reinterpret_cast<void*>(addr), size, prot, |
74 MAP_PRIVATE | MAP_ANON | MAP_FIXED, | 73 MAP_PRIVATE | MAP_ANON | MAP_FIXED, -1, 0); |
75 -1, 0); | |
76 if (address == MAP_FAILED) { | 74 if (address == MAP_FAILED) { |
77 return false; | 75 return false; |
78 } | 76 } |
79 return true; | 77 return true; |
80 } | 78 } |
81 | 79 |
82 | 80 |
83 bool VirtualMemory::Protect(void* address, intptr_t size, Protection mode) { | 81 bool VirtualMemory::Protect(void* address, intptr_t size, Protection mode) { |
84 ASSERT(Thread::Current()->IsMutatorThread() || | 82 ASSERT(Thread::Current()->IsMutatorThread() || |
85 Isolate::Current()->mutator_thread()->IsAtSafepoint()); | 83 Isolate::Current()->mutator_thread()->IsAtSafepoint()); |
(...skipping 12 matching lines...) Expand all Loading... |
98 prot = PROT_READ | PROT_WRITE; | 96 prot = PROT_READ | PROT_WRITE; |
99 break; | 97 break; |
100 case kReadExecute: | 98 case kReadExecute: |
101 prot = PROT_READ | PROT_EXEC; | 99 prot = PROT_READ | PROT_EXEC; |
102 break; | 100 break; |
103 case kReadWriteExecute: | 101 case kReadWriteExecute: |
104 prot = PROT_READ | PROT_WRITE | PROT_EXEC; | 102 prot = PROT_READ | PROT_WRITE | PROT_EXEC; |
105 break; | 103 break; |
106 } | 104 } |
107 return (mprotect(reinterpret_cast<void*>(page_address), | 105 return (mprotect(reinterpret_cast<void*>(page_address), |
108 end_address - page_address, | 106 end_address - page_address, prot) == 0); |
109 prot) == 0); | |
110 } | 107 } |
111 | 108 |
112 | 109 |
113 } // namespace dart | 110 } // namespace dart |
114 | 111 |
115 #endif // defined(TARGET_OS_LINUX) | 112 #endif // defined(TARGET_OS_LINUX) |
OLD | NEW |