2012-01-16 13:23:40 +00:00
|
|
|
// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
|
2011-10-05 05:20:07 +00:00
|
|
|
// for details. All rights reserved. Use of this source code is governed by a
|
|
|
|
// BSD-style license that can be found in the LICENSE file.
|
|
|
|
|
2016-10-26 07:26:03 +00:00
|
|
|
#ifndef RUNTIME_VM_VIRTUAL_MEMORY_H_
|
|
|
|
#define RUNTIME_VM_VIRTUAL_MEMORY_H_
|
2011-10-05 05:20:07 +00:00
|
|
|
|
2012-01-16 13:23:40 +00:00
|
|
|
#include "platform/utils.h"
|
2019-08-07 11:37:20 +00:00
|
|
|
#include "vm/flags.h"
|
2011-10-05 05:20:07 +00:00
|
|
|
#include "vm/globals.h"
|
|
|
|
#include "vm/memory_region.h"
|
|
|
|
|
|
|
|
namespace dart {
|
|
|
|
|
|
|
|
class VirtualMemory {
|
|
|
|
public:
|
2012-07-27 18:07:59 +00:00
|
|
|
enum Protection {
|
|
|
|
kNoAccess,
|
|
|
|
kReadOnly,
|
|
|
|
kReadWrite,
|
|
|
|
kReadExecute,
|
2014-02-11 10:17:03 +00:00
|
|
|
kReadWriteExecute
|
2012-07-27 18:07:59 +00:00
|
|
|
};
|
|
|
|
|
2011-10-05 05:20:07 +00:00
|
|
|
// The reserved memory is unmapped on destruction.
|
|
|
|
~VirtualMemory();
|
|
|
|
|
|
|
|
uword start() const { return region_.start(); }
|
|
|
|
uword end() const { return region_.end(); }
|
|
|
|
void* address() const { return region_.pointer(); }
|
|
|
|
intptr_t size() const { return region_.size(); }
|
2019-03-12 20:45:45 +00:00
|
|
|
intptr_t AliasOffset() const { return alias_.start() - region_.start(); }
|
2011-10-05 05:20:07 +00:00
|
|
|
|
2018-09-28 23:18:59 +00:00
|
|
|
static void Init();
|
2021-02-22 19:58:49 +00:00
|
|
|
static void Cleanup();
|
2011-10-05 05:20:07 +00:00
|
|
|
|
2019-08-07 11:37:20 +00:00
|
|
|
// Returns true if dual mapping is enabled.
|
|
|
|
static bool DualMappingEnabled();
|
|
|
|
|
2016-11-08 21:54:47 +00:00
|
|
|
bool Contains(uword addr) const { return region_.Contains(addr); }
|
2019-03-12 20:45:45 +00:00
|
|
|
bool ContainsAlias(uword addr) const {
|
|
|
|
return (AliasOffset() != 0) && alias_.Contains(addr);
|
|
|
|
}
|
2011-10-05 05:20:07 +00:00
|
|
|
|
2012-07-27 18:07:59 +00:00
|
|
|
// Changes the protection of the virtual memory area.
|
2018-01-10 00:44:38 +00:00
|
|
|
static void Protect(void* address, intptr_t size, Protection mode);
|
|
|
|
void Protect(Protection mode) { return Protect(address(), size(), mode); }
|
2012-07-27 18:07:59 +00:00
|
|
|
|
2017-10-30 22:02:20 +00:00
|
|
|
// Reserves and commits a virtual memory segment with size. If a segment of
|
|
|
|
// the requested size cannot be allocated, NULL is returned.
|
|
|
|
static VirtualMemory* Allocate(intptr_t size,
|
|
|
|
bool is_executable,
|
2021-09-08 01:16:57 +00:00
|
|
|
bool is_compressed,
|
[vm] Implement VirtualMemory::Allocate using AllocateAligned
Instead of implementing separate aligned and unaligned memory
allocation primitives for each OS, just change the unaligned allocator
into a wrapper around the aligned primitive.
While here, we can optimize the AllocateAligned logic slightly: if we
want an N-page-aligned allocation, we only need to increase the
allocation size by N-1 pages instead of N.
Notably, this means 1-page-aligned allocations don't require any extra
alignment pages, so the new logic behaves identically as before on
Android, Fuchsia, Linux, and macOS.
On Windows, it behaves slightly differently only in that unaligned
requests used to be handled as a single VirtualAlloc call with
MEM_RESERVE | MEM_COMMIT, but now they're handled as two separate
calls (reserve *then* commit). Naively, I don't expect this matters in
practice, but if it does, we can always add a fast path for
alignment==page_size_ without affecting the OS-independent API.
Change-Id: I42b2cf5dfc6e137546d8acfb6cc8939a01687948
Reviewed-on: https://dart-review.googlesource.com/c/91081
Commit-Queue: Ryan Macnak <rmacnak@google.com>
Reviewed-by: Ryan Macnak <rmacnak@google.com>
2019-01-25 01:22:20 +00:00
|
|
|
const char* name) {
|
2021-09-08 01:16:57 +00:00
|
|
|
return AllocateAligned(size, PageSize(), is_executable, is_compressed,
|
|
|
|
name);
|
[vm] Implement VirtualMemory::Allocate using AllocateAligned
Instead of implementing separate aligned and unaligned memory
allocation primitives for each OS, just change the unaligned allocator
into a wrapper around the aligned primitive.
While here, we can optimize the AllocateAligned logic slightly: if we
want an N-page-aligned allocation, we only need to increase the
allocation size by N-1 pages instead of N.
Notably, this means 1-page-aligned allocations don't require any extra
alignment pages, so the new logic behaves identically as before on
Android, Fuchsia, Linux, and macOS.
On Windows, it behaves slightly differently only in that unaligned
requests used to be handled as a single VirtualAlloc call with
MEM_RESERVE | MEM_COMMIT, but now they're handled as two separate
calls (reserve *then* commit). Naively, I don't expect this matters in
practice, but if it does, we can always add a fast path for
alignment==page_size_ without affecting the OS-independent API.
Change-Id: I42b2cf5dfc6e137546d8acfb6cc8939a01687948
Reviewed-on: https://dart-review.googlesource.com/c/91081
Commit-Queue: Ryan Macnak <rmacnak@google.com>
Reviewed-by: Ryan Macnak <rmacnak@google.com>
2019-01-25 01:22:20 +00:00
|
|
|
}
|
2017-10-30 22:02:20 +00:00
|
|
|
static VirtualMemory* AllocateAligned(intptr_t size,
|
|
|
|
intptr_t alignment,
|
|
|
|
bool is_executable,
|
2021-09-08 01:16:57 +00:00
|
|
|
bool is_compressed,
|
2017-10-30 22:02:20 +00:00
|
|
|
const char* name);
|
2011-10-05 05:20:07 +00:00
|
|
|
|
2019-09-24 15:03:19 +00:00
|
|
|
// Returns the cached page size. Use only if Init() has been called.
|
2011-10-05 05:20:07 +00:00
|
|
|
static intptr_t PageSize() {
|
|
|
|
ASSERT(page_size_ != 0);
|
|
|
|
return page_size_;
|
|
|
|
}
|
|
|
|
|
2014-02-10 12:18:06 +00:00
|
|
|
static bool InSamePage(uword address0, uword address1);
|
|
|
|
|
2019-01-31 21:01:48 +00:00
|
|
|
// Truncate this virtual memory segment.
|
|
|
|
void Truncate(intptr_t new_size);
|
2011-10-05 05:20:07 +00:00
|
|
|
|
2017-10-30 22:02:20 +00:00
|
|
|
// False for a part of a snapshot added directly to the Dart heap, which
|
|
|
|
// belongs to the embedder and must not be deallocated or have its
|
|
|
|
// protection status changed by the VM.
|
|
|
|
bool vm_owns_region() const { return reserved_.pointer() != NULL; }
|
2015-10-26 22:43:27 +00:00
|
|
|
|
2017-01-26 17:53:06 +00:00
|
|
|
static VirtualMemory* ForImagePage(void* pointer, uword size);
|
2015-09-16 18:22:57 +00:00
|
|
|
|
2014-06-30 22:59:07 +00:00
|
|
|
private:
|
2019-09-25 17:09:16 +00:00
|
|
|
static intptr_t CalculatePageSize();
|
|
|
|
|
2011-10-05 05:20:07 +00:00
|
|
|
// Free a sub segment. On operating systems that support it this
|
2014-10-09 15:59:28 +00:00
|
|
|
// can give back the virtual memory to the system. Returns true on success.
|
2021-02-22 19:58:49 +00:00
|
|
|
static bool FreeSubSegment(void* address, intptr_t size);
|
2011-10-05 05:20:07 +00:00
|
|
|
|
2021-08-09 22:57:16 +00:00
|
|
|
static VirtualMemory* Reserve(intptr_t size, intptr_t alignment);
|
|
|
|
static void Commit(void* address, intptr_t size);
|
|
|
|
static void Decommit(void* address, intptr_t size);
|
|
|
|
|
2019-03-12 20:45:45 +00:00
|
|
|
// These constructors are only used internally when reserving new virtual
|
|
|
|
// spaces. They do not reserve any virtual address space on their own.
|
2017-10-30 22:02:20 +00:00
|
|
|
VirtualMemory(const MemoryRegion& region,
|
2019-03-12 20:45:45 +00:00
|
|
|
const MemoryRegion& alias,
|
2017-12-19 21:47:44 +00:00
|
|
|
const MemoryRegion& reserved)
|
2019-03-12 20:45:45 +00:00
|
|
|
: region_(region), alias_(alias), reserved_(reserved) {}
|
|
|
|
|
|
|
|
VirtualMemory(const MemoryRegion& region, const MemoryRegion& reserved)
|
|
|
|
: region_(region), alias_(region), reserved_(reserved) {}
|
2011-10-05 05:20:07 +00:00
|
|
|
|
|
|
|
MemoryRegion region_;
|
|
|
|
|
2019-03-12 20:45:45 +00:00
|
|
|
// Optional secondary mapping of region_ to a virtual space with different
|
|
|
|
// protection, e.g. allowing code execution.
|
|
|
|
MemoryRegion alias_;
|
|
|
|
|
2017-10-30 22:02:20 +00:00
|
|
|
// The underlying reservation not yet given back to the OS.
|
|
|
|
// Its address might disagree with region_ due to aligned allocations.
|
|
|
|
// Its size might disagree with region_ due to Truncate.
|
|
|
|
MemoryRegion reserved_;
|
2011-10-05 05:20:07 +00:00
|
|
|
|
|
|
|
static uword page_size_;
|
2021-08-09 22:57:16 +00:00
|
|
|
static VirtualMemory* compressed_heap_;
|
2011-10-05 05:20:07 +00:00
|
|
|
|
|
|
|
DISALLOW_IMPLICIT_CONSTRUCTORS(VirtualMemory);
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace dart
|
|
|
|
|
2016-10-26 07:26:03 +00:00
|
|
|
#endif // RUNTIME_VM_VIRTUAL_MEMORY_H_
|