• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2018 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef V8_BASE_BOUNDED_PAGE_ALLOCATOR_H_
6 #define V8_BASE_BOUNDED_PAGE_ALLOCATOR_H_
7 
8 #include "include/v8-platform.h"
9 #include "src/base/platform/mutex.h"
10 #include "src/base/region-allocator.h"
11 
12 namespace v8 {
13 namespace base {
14 
15 // Defines the page initialization mode of a BoundedPageAllocator.
16 enum class PageInitializationMode {
17   // The contents of allocated pages must be zero initialized. This causes any
18   // committed pages to be decommitted during FreePages and ReleasePages.
19   kAllocatedPagesMustBeZeroInitialized,
20   // Allocated pages do not have to be be zero initialized and can contain old
21   // data. This is slightly faster as comitted pages are not decommitted
22   // during FreePages and ReleasePages, but only made inaccessible.
23   kAllocatedPagesCanBeUninitialized,
24 };
25 
26 // This is a v8::PageAllocator implementation that allocates pages within the
27 // pre-reserved region of virtual space. This class requires the virtual space
28 // to be kept reserved during the lifetime of this object.
29 // The main application of bounded page allocator are
30 //  - V8 heap pointer compression which requires the whole V8 heap to be
31 //    allocated within a contiguous range of virtual address space,
32 //  - executable page allocation, which allows to use PC-relative 32-bit code
33 //    displacement on certain 64-bit platforms.
34 // Bounded page allocator uses other page allocator instance for doing actual
35 // page allocations.
36 // The implementation is thread-safe.
37 class V8_BASE_EXPORT BoundedPageAllocator : public v8::PageAllocator {
38  public:
39   using Address = uintptr_t;
40 
41   BoundedPageAllocator(v8::PageAllocator* page_allocator, Address start,
42                        size_t size, size_t allocate_page_size,
43                        PageInitializationMode page_initialization_mode);
44   BoundedPageAllocator(const BoundedPageAllocator&) = delete;
45   BoundedPageAllocator& operator=(const BoundedPageAllocator&) = delete;
46   ~BoundedPageAllocator() override = default;
47 
48   // These functions are not inlined to avoid https://crbug.com/v8/8275.
49   Address begin() const;
50   size_t size() const;
51 
52   // Returns true if given address is in the range controlled by the bounded
53   // page allocator instance.
contains(Address address)54   bool contains(Address address) const {
55     return region_allocator_.contains(address);
56   }
57 
AllocatePageSize()58   size_t AllocatePageSize() override { return allocate_page_size_; }
59 
CommitPageSize()60   size_t CommitPageSize() override { return commit_page_size_; }
61 
SetRandomMmapSeed(int64_t seed)62   void SetRandomMmapSeed(int64_t seed) override {
63     page_allocator_->SetRandomMmapSeed(seed);
64   }
65 
GetRandomMmapAddr()66   void* GetRandomMmapAddr() override {
67     return page_allocator_->GetRandomMmapAddr();
68   }
69 
70   void* AllocatePages(void* hint, size_t size, size_t alignment,
71                       Permission access) override;
72 
73   bool ReserveForSharedMemoryMapping(void* address, size_t size) override;
74 
75   // Allocates pages at given address, returns true on success.
76   bool AllocatePagesAt(Address address, size_t size, Permission access);
77 
78   bool FreePages(void* address, size_t size) override;
79 
80   bool ReleasePages(void* address, size_t size, size_t new_size) override;
81 
82   bool SetPermissions(void* address, size_t size, Permission access) override;
83 
84   bool DiscardSystemPages(void* address, size_t size) override;
85 
86   bool DecommitPages(void* address, size_t size) override;
87 
88  private:
89   v8::base::Mutex mutex_;
90   const size_t allocate_page_size_;
91   const size_t commit_page_size_;
92   v8::PageAllocator* const page_allocator_;
93   v8::base::RegionAllocator region_allocator_;
94   const PageInitializationMode page_initialization_mode_;
95 };
96 
97 }  // namespace base
98 }  // namespace v8
99 
100 #endif  // V8_BASE_BOUNDED_PAGE_ALLOCATOR_H_
101