11cb0ef41Sopenharmony_ci// Copyright 2018 the V8 project authors. All rights reserved. 21cb0ef41Sopenharmony_ci// Use of this source code is governed by a BSD-style license that can be 31cb0ef41Sopenharmony_ci// found in the LICENSE file. 41cb0ef41Sopenharmony_ci 51cb0ef41Sopenharmony_ci#ifndef V8_BASE_BOUNDED_PAGE_ALLOCATOR_H_ 61cb0ef41Sopenharmony_ci#define V8_BASE_BOUNDED_PAGE_ALLOCATOR_H_ 71cb0ef41Sopenharmony_ci 81cb0ef41Sopenharmony_ci#include "include/v8-platform.h" 91cb0ef41Sopenharmony_ci#include "src/base/platform/mutex.h" 101cb0ef41Sopenharmony_ci#include "src/base/region-allocator.h" 111cb0ef41Sopenharmony_ci 121cb0ef41Sopenharmony_cinamespace v8 { 131cb0ef41Sopenharmony_cinamespace base { 141cb0ef41Sopenharmony_ci 151cb0ef41Sopenharmony_ci// Defines the page initialization mode of a BoundedPageAllocator. 161cb0ef41Sopenharmony_cienum class PageInitializationMode { 171cb0ef41Sopenharmony_ci // The contents of allocated pages must be zero initialized. This causes any 181cb0ef41Sopenharmony_ci // committed pages to be decommitted during FreePages and ReleasePages. 191cb0ef41Sopenharmony_ci kAllocatedPagesMustBeZeroInitialized, 201cb0ef41Sopenharmony_ci // Allocated pages do not have to be be zero initialized and can contain old 211cb0ef41Sopenharmony_ci // data. This is slightly faster as comitted pages are not decommitted 221cb0ef41Sopenharmony_ci // during FreePages and ReleasePages, but only made inaccessible. 231cb0ef41Sopenharmony_ci kAllocatedPagesCanBeUninitialized, 241cb0ef41Sopenharmony_ci}; 251cb0ef41Sopenharmony_ci 261cb0ef41Sopenharmony_ci// This is a v8::PageAllocator implementation that allocates pages within the 271cb0ef41Sopenharmony_ci// pre-reserved region of virtual space. This class requires the virtual space 281cb0ef41Sopenharmony_ci// to be kept reserved during the lifetime of this object. 291cb0ef41Sopenharmony_ci// The main application of bounded page allocator are 301cb0ef41Sopenharmony_ci// - V8 heap pointer compression which requires the whole V8 heap to be 311cb0ef41Sopenharmony_ci// allocated within a contiguous range of virtual address space, 321cb0ef41Sopenharmony_ci// - executable page allocation, which allows to use PC-relative 32-bit code 331cb0ef41Sopenharmony_ci// displacement on certain 64-bit platforms. 341cb0ef41Sopenharmony_ci// Bounded page allocator uses other page allocator instance for doing actual 351cb0ef41Sopenharmony_ci// page allocations. 361cb0ef41Sopenharmony_ci// The implementation is thread-safe. 371cb0ef41Sopenharmony_ciclass V8_BASE_EXPORT BoundedPageAllocator : public v8::PageAllocator { 381cb0ef41Sopenharmony_ci public: 391cb0ef41Sopenharmony_ci using Address = uintptr_t; 401cb0ef41Sopenharmony_ci 411cb0ef41Sopenharmony_ci BoundedPageAllocator(v8::PageAllocator* page_allocator, Address start, 421cb0ef41Sopenharmony_ci size_t size, size_t allocate_page_size, 431cb0ef41Sopenharmony_ci PageInitializationMode page_initialization_mode); 441cb0ef41Sopenharmony_ci BoundedPageAllocator(const BoundedPageAllocator&) = delete; 451cb0ef41Sopenharmony_ci BoundedPageAllocator& operator=(const BoundedPageAllocator&) = delete; 461cb0ef41Sopenharmony_ci ~BoundedPageAllocator() override = default; 471cb0ef41Sopenharmony_ci 481cb0ef41Sopenharmony_ci // These functions are not inlined to avoid https://crbug.com/v8/8275. 491cb0ef41Sopenharmony_ci Address begin() const; 501cb0ef41Sopenharmony_ci size_t size() const; 511cb0ef41Sopenharmony_ci 521cb0ef41Sopenharmony_ci // Returns true if given address is in the range controlled by the bounded 531cb0ef41Sopenharmony_ci // page allocator instance. 541cb0ef41Sopenharmony_ci bool contains(Address address) const { 551cb0ef41Sopenharmony_ci return region_allocator_.contains(address); 561cb0ef41Sopenharmony_ci } 571cb0ef41Sopenharmony_ci 581cb0ef41Sopenharmony_ci size_t AllocatePageSize() override { return allocate_page_size_; } 591cb0ef41Sopenharmony_ci 601cb0ef41Sopenharmony_ci size_t CommitPageSize() override { return commit_page_size_; } 611cb0ef41Sopenharmony_ci 621cb0ef41Sopenharmony_ci void SetRandomMmapSeed(int64_t seed) override { 631cb0ef41Sopenharmony_ci page_allocator_->SetRandomMmapSeed(seed); 641cb0ef41Sopenharmony_ci } 651cb0ef41Sopenharmony_ci 661cb0ef41Sopenharmony_ci void* GetRandomMmapAddr() override { 671cb0ef41Sopenharmony_ci return page_allocator_->GetRandomMmapAddr(); 681cb0ef41Sopenharmony_ci } 691cb0ef41Sopenharmony_ci 701cb0ef41Sopenharmony_ci void* AllocatePages(void* hint, size_t size, size_t alignment, 711cb0ef41Sopenharmony_ci Permission access) override; 721cb0ef41Sopenharmony_ci 731cb0ef41Sopenharmony_ci bool ReserveForSharedMemoryMapping(void* address, size_t size) override; 741cb0ef41Sopenharmony_ci 751cb0ef41Sopenharmony_ci // Allocates pages at given address, returns true on success. 761cb0ef41Sopenharmony_ci bool AllocatePagesAt(Address address, size_t size, Permission access); 771cb0ef41Sopenharmony_ci 781cb0ef41Sopenharmony_ci bool FreePages(void* address, size_t size) override; 791cb0ef41Sopenharmony_ci 801cb0ef41Sopenharmony_ci bool ReleasePages(void* address, size_t size, size_t new_size) override; 811cb0ef41Sopenharmony_ci 821cb0ef41Sopenharmony_ci bool SetPermissions(void* address, size_t size, Permission access) override; 831cb0ef41Sopenharmony_ci 841cb0ef41Sopenharmony_ci bool DiscardSystemPages(void* address, size_t size) override; 851cb0ef41Sopenharmony_ci 861cb0ef41Sopenharmony_ci bool DecommitPages(void* address, size_t size) override; 871cb0ef41Sopenharmony_ci 881cb0ef41Sopenharmony_ci private: 891cb0ef41Sopenharmony_ci v8::base::Mutex mutex_; 901cb0ef41Sopenharmony_ci const size_t allocate_page_size_; 911cb0ef41Sopenharmony_ci const size_t commit_page_size_; 921cb0ef41Sopenharmony_ci v8::PageAllocator* const page_allocator_; 931cb0ef41Sopenharmony_ci v8::base::RegionAllocator region_allocator_; 941cb0ef41Sopenharmony_ci const PageInitializationMode page_initialization_mode_; 951cb0ef41Sopenharmony_ci}; 961cb0ef41Sopenharmony_ci 971cb0ef41Sopenharmony_ci} // namespace base 981cb0ef41Sopenharmony_ci} // namespace v8 991cb0ef41Sopenharmony_ci 1001cb0ef41Sopenharmony_ci#endif // V8_BASE_BOUNDED_PAGE_ALLOCATOR_H_ 101