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
3// BSD-style license that can be found in the LICENSE file.
4
5#include "vm/virtual_memory.h"
6
7#include "platform/assert.h"
8#include "platform/utils.h"
9
10namespace dart {
11
12bool VirtualMemory::InSamePage(uword address0, uword address1) {
13 return (Utils::RoundDown(address0, PageSize()) ==
14 Utils::RoundDown(address1, PageSize()));
15}
16
17void VirtualMemory::Truncate(intptr_t new_size) {
18 ASSERT(Utils::IsAligned(new_size, PageSize()));
19 ASSERT(new_size <= size());
20 if (reserved_.size() ==
21 region_.size()) { // Don't create holes in reservation.
22 FreeSubSegment(reinterpret_cast<void*>(start() + new_size),
23 size() - new_size);
24 reserved_.set_size(new_size);
25 if (AliasOffset() != 0) {
26 FreeSubSegment(reinterpret_cast<void*>(alias_.start() + new_size),
27 alias_.size() - new_size);
28 }
29 }
30 region_.Subregion(region_, 0, new_size);
31 alias_.Subregion(alias_, 0, new_size);
32}
33
34VirtualMemory* VirtualMemory::ForImagePage(void* pointer, uword size) {
35 // Memory for precompilated instructions was allocated by the embedder, so
36 // create a VirtualMemory without allocating.
37 MemoryRegion region(pointer, size);
38 MemoryRegion reserved(0, 0); // NULL reservation indicates VM should not
39 // attempt to free this memory.
40 VirtualMemory* memory = new VirtualMemory(region, region, reserved);
41 ASSERT(!memory->vm_owns_region());
42 return memory;
43}
44
45} // namespace dart
46