Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
virtual_memory.h
Go to the documentation of this 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
3// BSD-style license that can be found in the LICENSE file.
4
5#ifndef RUNTIME_VM_VIRTUAL_MEMORY_H_
6#define RUNTIME_VM_VIRTUAL_MEMORY_H_
7
8#include "platform/utils.h"
9#include "vm/flags.h"
10#include "vm/globals.h"
11#include "vm/memory_region.h"
12
13#if defined(DART_HOST_OS_FUCHSIA)
14#include <zircon/types.h>
15#endif
16
17namespace dart {
18
20 public:
28
29 // The reserved memory is unmapped on destruction.
31
32 uword start() const { return region_.start(); }
33 uword end() const { return region_.end(); }
34 void* address() const { return region_.pointer(); }
35 intptr_t size() const { return region_.size(); }
36
37#if defined(DART_HOST_OS_FUCHSIA)
38 static void Init(zx_handle_t vmex_resource);
39#else
40 static void Init();
41#endif
42 static void Cleanup();
43
44 bool Contains(uword addr) const { return region_.Contains(addr); }
45
46 // Changes the protection of the virtual memory area.
47 static void Protect(void* address, intptr_t size, Protection mode);
48 void Protect(Protection mode) { return Protect(address(), size(), mode); }
49
50 static void DontNeed(void* address, intptr_t size);
51
52 // Reserves and commits a virtual memory segment with size. If a segment of
53 // the requested size cannot be allocated, nullptr is returned.
54 static VirtualMemory* Allocate(intptr_t size,
55 bool is_executable,
56 bool is_compressed,
57 const char* name) {
58 return AllocateAligned(size, PageSize(), is_executable, is_compressed,
59 name);
60 }
62 intptr_t alignment,
63 bool is_executable,
64 bool is_compressed,
65 const char* name);
66
67 // Duplicates `this` memory into the `target` memory. This is designed to work
68 // on all platforms, including iOS, which doesn't allow creating new
69 // executable memory.
70 //
71 // Assumes
72 // * `this` has RX protection.
73 // * `target` has RW protection, and is at least as large as `this`.
74#if !defined(DART_TARGET_OS_FUCHSIA)
76#endif // !defined(DART_TARGET_OS_FUCHSIA)
77
78 // Returns the cached page size. Use only if Init() has been called.
79 static intptr_t PageSize() {
80 ASSERT(page_size_ != 0);
81 return page_size_;
82 }
83
84 static bool InSamePage(uword address0, uword address1);
85
86 // Truncate this virtual memory segment.
87 void Truncate(intptr_t new_size);
88
89 // False for a part of a snapshot added directly to the Dart heap, which
90 // belongs to the embedder and must not be deallocated or have its
91 // protection status changed by the VM.
92 bool vm_owns_region() const { return reserved_.pointer() != nullptr; }
93
94 static VirtualMemory* ForImagePage(void* pointer, uword size);
95
96 private:
97 static intptr_t CalculatePageSize();
98
99 // Free a sub segment. On operating systems that support it this
100 // can give back the virtual memory to the system. Returns true on success.
101 static bool FreeSubSegment(void* address, intptr_t size);
102
103 static VirtualMemory* Reserve(intptr_t size, intptr_t alignment);
104 static void Commit(void* address, intptr_t size);
105 static void Decommit(void* address, intptr_t size);
106
107 // These constructors are only used internally when reserving new virtual
108 // spaces. They do not reserve any virtual address space on their own.
109 VirtualMemory(const MemoryRegion& region, const MemoryRegion& reserved)
110 : region_(region), reserved_(reserved) {}
111
112 MemoryRegion region_;
113
114 // The underlying reservation not yet given back to the OS.
115 // Its address might disagree with region_ due to aligned allocations.
116 // Its size might disagree with region_ due to Truncate.
117 MemoryRegion reserved_;
118
119 static uword page_size_;
120 static VirtualMemory* compressed_heap_;
121
122 DISALLOW_IMPLICIT_CONSTRUCTORS(VirtualMemory);
123};
124
125} // namespace dart
126
127#endif // RUNTIME_VM_VIRTUAL_MEMORY_H_
bool Contains(uword address) const
void * pointer() const
uword end() const
uword start() const
uword size() const
static void Init()
static VirtualMemory * AllocateAligned(intptr_t size, intptr_t alignment, bool is_executable, bool is_compressed, const char *name)
bool Contains(uword addr) const
static void Protect(void *address, intptr_t size, Protection mode)
static intptr_t PageSize()
bool vm_owns_region() const
intptr_t size() const
bool DuplicateRX(VirtualMemory *target)
void Protect(Protection mode)
static bool InSamePage(uword address0, uword address1)
static void DontNeed(void *address, intptr_t size)
void Truncate(intptr_t new_size)
static VirtualMemory * Allocate(intptr_t size, bool is_executable, bool is_compressed, const char *name)
static void Cleanup()
void * address() const
uword start() const
static VirtualMemory * ForImagePage(void *pointer, uword size)
#define ASSERT(E)
uint32_t * target
const char *const name
uintptr_t uword
Definition globals.h:501
#define DISALLOW_IMPLICIT_CONSTRUCTORS(TypeName)
Definition globals.h:593