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/virtual_memory.h" | 5 #include "vm/virtual_memory.h" |
6 | 6 |
7 #include "platform/assert.h" | 7 #include "platform/assert.h" |
8 #include "platform/utils.h" | 8 #include "platform/utils.h" |
9 | 9 |
10 namespace dart { | 10 namespace dart { |
11 | 11 |
12 bool VirtualMemory::InSamePage(uword address0, uword address1) { | 12 bool VirtualMemory::InSamePage(uword address0, uword address1) { |
13 return (Utils::RoundDown(address0, PageSize()) == | 13 return (Utils::RoundDown(address0, PageSize()) == |
14 Utils::RoundDown(address1, PageSize())); | 14 Utils::RoundDown(address1, PageSize())); |
15 } | 15 } |
16 | 16 |
17 | 17 |
18 VirtualMemory* VirtualMemory::ReserveAligned(intptr_t size, | 18 void VirtualMemory::Truncate(intptr_t new_size, bool try_unmap) { |
19 intptr_t alignment) { | 19 ASSERT((new_size & (PageSize() - 1)) == 0); |
20 ASSERT((size & (PageSize() - 1)) == 0); | 20 ASSERT(new_size <= size()); |
21 ASSERT(Utils::IsPowerOfTwo(alignment)); | 21 if (try_unmap && |
22 ASSERT(alignment >= PageSize()); | 22 (reserved_size_ == size()) && /* Don't create holes in reservation. */ |
23 VirtualMemory* result = VirtualMemory::Reserve(size + alignment); | 23 FreeSubSegment(reinterpret_cast<void*>(start() + new_size), |
24 if (result == NULL) { | 24 size() - new_size)) { |
25 FATAL("Out of memory.\n"); | 25 reserved_size_ = new_size; |
26 } | 26 } |
27 uword start = result->start(); | |
28 uword real_start = (start + alignment - 1) & ~(alignment - 1); | |
29 result->Truncate(real_start, size); | |
30 return result; | |
31 } | |
32 | |
33 | |
34 void VirtualMemory::Truncate(uword new_start, intptr_t new_size) { | |
35 ASSERT(new_start >= start()); | |
36 ASSERT((new_size & (PageSize() - 1)) == 0); | |
37 if (new_start > start()) { | |
38 uword split = new_start - start(); | |
39 ASSERT((split & (PageSize() - 1)) == 0); | |
40 FreeSubSegment(address(), split); | |
41 region_.Subregion(region_, split, size() - split); | |
42 } | |
43 ASSERT(new_size <= size()); | |
44 FreeSubSegment(reinterpret_cast<void*>(start() + new_size), | |
45 size() - new_size); | |
46 region_.Subregion(region_, 0, new_size); | 27 region_.Subregion(region_, 0, new_size); |
47 } | 28 } |
48 | 29 |
49 } // namespace dart | 30 } // namespace dart |
OLD | NEW |