tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

Memory.h (4358B)


      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 // Map the region below the current stack pointer as a stack, down to the
     57 // allowed JS stack depth, so that heap allocations will never intersect with
     58 // the space that the stack may expand into. (Only on Linux fuzzing builds.)
     59 void MapStack(size_t stackSize);
     60 
     61 // We only decommit unused pages if the system page size is the same as the
     62 // hardcoded page size for the build.
     63 bool DecommitEnabled();
     64 
     65 // Disable decommit for testing purposes. This must be called before
     66 // InitMemorySubsystem.
     67 void DisableDecommit();
     68 
     69 // Tell the OS that the given pages are not in use, so they should not be
     70 // written to a paging file. This may be a no-op on some platforms.
     71 bool MarkPagesUnusedSoft(void* region, size_t length);
     72 
     73 // Tell the OS that the given pages are not in use and it can decommit them
     74 // immediately. This may defer to MarkPagesUnusedSoft and must be paired with
     75 // MarkPagesInUse to use the pages again.
     76 bool MarkPagesUnusedHard(void* region, size_t length);
     77 
     78 // Undo |MarkPagesUnusedSoft|: 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.  May make pages read/write-able.
     81 void MarkPagesInUseSoft(void* region, size_t length);
     82 
     83 // Undo |MarkPagesUnusedHard|: tell the OS that the given pages are of interest
     84 // and should be paged in and out normally. This may be a no-op on some
     85 // platforms. Callers must check the result, false could mean that the pages
     86 // are not available.  May make pages read/write.
     87 [[nodiscard]] bool MarkPagesInUseHard(void* region, size_t length);
     88 
     89 // Returns #(hard faults) + #(soft faults)
     90 size_t GetPageFaultCount();
     91 
     92 // Allocate memory mapped content.
     93 // The offset must be aligned according to alignment requirement.
     94 void* AllocateMappedContent(int fd, size_t offset, size_t length,
     95                            size_t alignment);
     96 
     97 // Deallocate memory mapped content.
     98 void DeallocateMappedContent(void* region, size_t length);
     99 
    100 void* TestMapAlignedPagesLastDitch(size_t length, size_t alignment);
    101 
    102 void ProtectPages(void* region, size_t length);
    103 void MakePagesReadOnly(void* region, size_t length);
    104 void UnprotectPages(void* region, size_t length);
    105 
    106 // Track mapped memory so we can report it to the profiler.
    107 void RecordMemoryAlloc(size_t bytes);
    108 void RecordMemoryFree(size_t bytes);
    109 
    110 }  // namespace gc
    111 }  // namespace js
    112 
    113 #endif /* gc_Memory_h */