• 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_INIT_ISOLATE_ALLOCATOR_H_
6 #define V8_INIT_ISOLATE_ALLOCATOR_H_
7 
8 #include <memory>
9 
10 #include "src/base/page-allocator.h"
11 #include "src/common/globals.h"
12 #include "src/flags/flags.h"
13 #include "src/utils/allocation.h"
14 
15 namespace v8 {
16 namespace internal {
17 
18 // IsolateAllocator object is responsible for allocating memory for one (!)
19 // Isolate object. Depending on the whether pointer compression is enabled,
20 // the memory can be allocated
21 //
22 // 1) in the C++ heap (when pointer compression is disabled or when multiple
23 // Isolates share a pointer compression cage)
24 //
25 // 2) in a proper part of a properly aligned region of a reserved address space
26 //   (when pointer compression is enabled and each Isolate has its own pointer
27 //   compression cage).
28 //
29 // Isolate::New() first creates IsolateAllocator object which allocates the
30 // memory and then it constructs Isolate object in this memory. Once it's done
31 // the Isolate object takes ownership of the IsolateAllocator object to keep
32 // the memory alive.
33 // Isolate::Delete() takes care of the proper order of the objects destruction.
34 class V8_EXPORT_PRIVATE IsolateAllocator final {
35  public:
36   IsolateAllocator();
37   ~IsolateAllocator();
38   IsolateAllocator(const IsolateAllocator&) = delete;
39   IsolateAllocator& operator=(const IsolateAllocator&) = delete;
40 
isolate_memory()41   void* isolate_memory() const { return isolate_memory_; }
42 
page_allocator()43   v8::PageAllocator* page_allocator() const { return page_allocator_; }
44 
GetPtrComprCageBase()45   Address GetPtrComprCageBase() const {
46     return COMPRESS_POINTERS_BOOL ? GetPtrComprCage()->base() : kNullAddress;
47   }
48 
49   // When pointer compression is on, return the pointer compression
50   // cage. Otherwise return nullptr.
51   VirtualMemoryCage* GetPtrComprCage();
52   const VirtualMemoryCage* GetPtrComprCage() const;
53 
54   static void InitializeOncePerProcess();
55 
56  private:
57   void CommitPagesForIsolate();
58 
59   friend class SequentialUnmapperTest;
60   // Only used for testing.
61   static void FreeProcessWidePtrComprCageForTesting();
62 
63   // The allocated memory for Isolate instance.
64   void* isolate_memory_ = nullptr;
65   v8::PageAllocator* page_allocator_ = nullptr;
66 #ifdef V8_COMPRESS_POINTERS_IN_ISOLATE_CAGE
67   VirtualMemoryCage isolate_ptr_compr_cage_;
68 #endif
69 };
70 
71 }  // namespace internal
72 }  // namespace v8
73 
74 #endif  // V8_INIT_ISOLATE_ALLOCATOR_H_
75