| 1 |
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
| 2 |
* vim: set ts=8 sts=2 et sw=2 tw=80: |
| 3 |
* This Source Code Form is subject to the terms of the Mozilla Public |
| 4 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 5 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 6 |
|
| 7 |
#ifndef gc_Memory_h |
| 8 |
#define gc_Memory_h |
| 9 |
|
| 10 |
#include "mozilla/Atomics.h" |
| 11 |
|
| 12 |
#include <stddef.h> |
| 13 |
|
| 14 |
namespace js { |
| 15 |
namespace gc { |
| 16 |
|
| 17 |
extern mozilla::Atomic<size_t, mozilla::Relaxed> gMappedMemorySizeBytes; |
| 18 |
extern mozilla::Atomic<uint64_t, mozilla::Relaxed> gMappedMemoryOperations; |
| 19 |
|
| 20 |
// Sanity check that our compiled configuration matches the currently |
| 21 |
// running instance and initialize any runtime data needed for allocation. |
| 22 |
void InitMemorySubsystem(); |
| 23 |
|
| 24 |
// Assert that memory use as recorded RecordMemoryAlloc/Free balances. |
| 25 |
void CheckMemorySubsystemOnShutDown(); |
| 26 |
|
| 27 |
// The page size as reported by the operating system. |
| 28 |
size_t SystemPageSize(); |
| 29 |
|
| 30 |
// The number of bits that may be set in a valid address, as |
| 31 |
// reported by the operating system or measured at startup. |
| 32 |
size_t SystemAddressBits(); |
| 33 |
|
| 34 |
// The number of bytes of virtual memory that may be allocated or mapped, as |
| 35 |
// reported by the operating system on certain platforms. If no limit was able |
| 36 |
// to be determined, then it will be size_t(-1). |
| 37 |
size_t VirtualMemoryLimit(); |
| 38 |
|
| 39 |
// The scattershot allocator is used on platforms that have a large address |
| 40 |
// range. On these platforms we allocate at random addresses. |
| 41 |
bool UsingScattershotAllocator(); |
| 42 |
|
| 43 |
// Whether to stall and retry memory allocation on failure. Only supported on |
| 44 |
// Windows when built with jemalloc. |
| 45 |
enum class StallAndRetry : bool { |
| 46 |
No = false, |
| 47 |
Yes = true, |
| 48 |
}; |
| 49 |
|
| 50 |
// Allocate or deallocate pages from the system with the given alignment. |
| 51 |
// Pages will be read/write-able. |
| 52 |
void* MapAlignedPages(size_t length, size_t alignment, |
| 53 |
StallAndRetry stallAndRetry = StallAndRetry::No); |
| 54 |
void UnmapPages(void* region, size_t length); |
| 55 |
|
| 56 |
// We only decommit unused pages if the system page size is the same as the |
| 57 |
// hardcoded page size for the build. |
| 58 |
bool DecommitEnabled(); |
| 59 |
|
| 60 |
// Disable decommit for testing purposes. This must be called before |
| 61 |
// InitMemorySubsystem. |
| 62 |
void DisableDecommit(); |
| 63 |
|
| 64 |
// Tell the OS that the given pages are not in use, so they should not be |
| 65 |
// written to a paging file. This may be a no-op on some platforms. |
| 66 |
bool MarkPagesUnusedSoft(void* region, size_t length); |
| 67 |
|
| 68 |
// Tell the OS that the given pages are not in use and it can decommit them |
| 69 |
// immediately. This may defer to MarkPagesUnusedSoft and must be paired with |
| 70 |
// MarkPagesInUse to use the pages again. |
| 71 |
bool MarkPagesUnusedHard(void* region, size_t length); |
| 72 |
|
| 73 |
// Undo |MarkPagesUnusedSoft|: tell the OS that the given pages are of interest |
| 74 |
// and should be paged in and out normally. This may be a no-op on some |
| 75 |
// platforms. May make pages read/write-able. |
| 76 |
void MarkPagesInUseSoft(void* region, size_t length); |
| 77 |
|
| 78 |
// Undo |MarkPagesUnusedHard|: tell the OS that the given pages are of interest |
| 79 |
// and should be paged in and out normally. This may be a no-op on some |
| 80 |
// platforms. Callers must check the result, false could mean that the pages |
| 81 |
// are not available. May make pages read/write. |
| 82 |
[[nodiscard]] bool MarkPagesInUseHard(void* region, size_t length); |
| 83 |
|
| 84 |
// Returns #(hard faults) + #(soft faults) |
| 85 |
size_t GetPageFaultCount(); |
| 86 |
|
| 87 |
// Allocate memory mapped content. |
| 88 |
// The offset must be aligned according to alignment requirement. |
| 89 |
void* AllocateMappedContent(int fd, size_t offset, size_t length, |
| 90 |
size_t alignment); |
| 91 |
|
| 92 |
// Deallocate memory mapped content. |
| 93 |
void DeallocateMappedContent(void* region, size_t length); |
| 94 |
|
| 95 |
void* TestMapAlignedPagesLastDitch(size_t length, size_t alignment); |
| 96 |
|
| 97 |
void ProtectPages(void* region, size_t length); |
| 98 |
void MakePagesReadOnly(void* region, size_t length); |
| 99 |
void UnprotectPages(void* region, size_t length); |
| 100 |
|
| 101 |
// Track mapped memory so we can report it to the profiler. |
| 102 |
void RecordMemoryAlloc(size_t bytes); |
| 103 |
void RecordMemoryFree(size_t bytes); |
| 104 |
|
| 105 |
} // namespace gc |
| 106 |
} // namespace js |
| 107 |
|
| 108 |
#endif /* gc_Memory_h */ |