Line data Source code
1 : // Copyright 2019 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 : #include "src/heap/read-only-heap.h"
6 :
7 : #include <cstring>
8 :
9 : #include "src/base/once.h"
10 : #include "src/heap/heap-inl.h"
11 : #include "src/heap/spaces.h"
12 : #include "src/snapshot/read-only-deserializer.h"
13 :
14 : namespace v8 {
15 : namespace internal {
16 :
17 : #ifdef V8_SHARED_RO_HEAP
18 : V8_DECLARE_ONCE(setup_ro_heap_once);
19 : ReadOnlyHeap* shared_ro_heap = nullptr;
20 : #endif
21 :
22 : // static
23 62441 : void ReadOnlyHeap::SetUp(Isolate* isolate, ReadOnlyDeserializer* des) {
24 : #ifdef V8_SHARED_RO_HEAP
25 : void* isolate_ro_roots = reinterpret_cast<void*>(
26 : isolate->roots_table().read_only_roots_begin().address());
27 : base::CallOnce(&setup_ro_heap_once, [isolate, des, isolate_ro_roots]() {
28 : shared_ro_heap = Init(isolate, des);
29 : if (des != nullptr) {
30 : std::memcpy(shared_ro_heap->read_only_roots_, isolate_ro_roots,
31 : kEntriesCount * sizeof(Address));
32 : }
33 : });
34 :
35 : isolate->heap()->SetUpFromReadOnlyHeap(shared_ro_heap);
36 : if (des != nullptr) {
37 : std::memcpy(isolate_ro_roots, shared_ro_heap->read_only_roots_,
38 : kEntriesCount * sizeof(Address));
39 : }
40 : #else
41 62441 : Init(isolate, des);
42 : #endif // V8_SHARED_RO_HEAP
43 62442 : }
44 :
45 56 : void ReadOnlyHeap::OnCreateHeapObjectsComplete() {
46 : DCHECK(!deserializing_);
47 : #ifdef V8_SHARED_RO_HEAP
48 : read_only_space_->Forget();
49 : #endif
50 56 : read_only_space_->MarkAsReadOnly();
51 56 : }
52 :
53 : // static
54 62442 : ReadOnlyHeap* ReadOnlyHeap::Init(Isolate* isolate, ReadOnlyDeserializer* des) {
55 62442 : auto* ro_heap = new ReadOnlyHeap(new ReadOnlySpace(isolate->heap()));
56 62442 : isolate->heap()->SetUpFromReadOnlyHeap(ro_heap);
57 62441 : if (des != nullptr) {
58 62385 : des->DeserializeInto(isolate);
59 62386 : ro_heap->deserializing_ = true;
60 : #ifdef V8_SHARED_RO_HEAP
61 : ro_heap->read_only_space_->Forget();
62 : #endif
63 62386 : ro_heap->read_only_space_->MarkAsReadOnly();
64 : }
65 62442 : return ro_heap;
66 : }
67 :
68 62425 : void ReadOnlyHeap::OnHeapTearDown() {
69 : #ifndef V8_SHARED_RO_HEAP
70 62425 : delete read_only_space_;
71 124854 : delete this;
72 : #endif
73 62427 : }
74 :
75 : // static
76 5211686 : bool ReadOnlyHeap::Contains(HeapObject object) {
77 5211686 : return Page::FromAddress(object.ptr())->owner()->identity() == RO_SPACE;
78 : }
79 :
80 : } // namespace internal
81 122036 : } // namespace v8
|