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 #include "src/snapshot/roots-serializer.h"
6 
7 #include "src/execution/isolate.h"
8 #include "src/heap/heap.h"
9 #include "src/objects/objects-inl.h"
10 #include "src/objects/slots.h"
11 
12 namespace v8 {
13 namespace internal {
14 
RootsSerializer(Isolate * isolate,Snapshot::SerializerFlags flags,RootIndex first_root_to_be_serialized)15 RootsSerializer::RootsSerializer(Isolate* isolate,
16                                  Snapshot::SerializerFlags flags,
17                                  RootIndex first_root_to_be_serialized)
18     : Serializer(isolate, flags),
19       first_root_to_be_serialized_(first_root_to_be_serialized),
20       object_cache_index_map_(isolate->heap()),
21       can_be_rehashed_(true) {
22   for (size_t i = 0; i < static_cast<size_t>(first_root_to_be_serialized);
23        ++i) {
24     root_has_been_serialized_[i] = true;
25   }
26 }
27 
SerializeInObjectCache(Handle<HeapObject> heap_object)28 int RootsSerializer::SerializeInObjectCache(Handle<HeapObject> heap_object) {
29   int index;
30   if (!object_cache_index_map_.LookupOrInsert(heap_object, &index)) {
31     // This object is not part of the object cache yet. Add it to the cache so
32     // we can refer to it via cache index from the delegating snapshot.
33     SerializeObject(heap_object);
34   }
35   return index;
36 }
37 
Synchronize(VisitorSynchronization::SyncTag tag)38 void RootsSerializer::Synchronize(VisitorSynchronization::SyncTag tag) {
39   sink_.Put(kSynchronize, "Synchronize");
40 }
41 
VisitRootPointers(Root root,const char * description,FullObjectSlot start,FullObjectSlot end)42 void RootsSerializer::VisitRootPointers(Root root, const char* description,
43                                         FullObjectSlot start,
44                                         FullObjectSlot end) {
45   RootsTable& roots_table = isolate()->roots_table();
46   if (start ==
47       roots_table.begin() + static_cast<int>(first_root_to_be_serialized_)) {
48     // Serializing the root list needs special handling:
49     // - Only root list elements that have been fully serialized can be
50     //   referenced using kRootArray bytecodes.
51     for (FullObjectSlot current = start; current < end; ++current) {
52       SerializeRootObject(current);
53       size_t root_index = current - roots_table.begin();
54       root_has_been_serialized_.set(root_index);
55     }
56   } else {
57     Serializer::VisitRootPointers(root, description, start, end);
58   }
59 }
60 
CheckRehashability(HeapObject obj)61 void RootsSerializer::CheckRehashability(HeapObject obj) {
62   if (!can_be_rehashed_) return;
63   if (!obj.NeedsRehashing()) return;
64   if (obj.CanBeRehashed()) return;
65   can_be_rehashed_ = false;
66 }
67 
68 }  // namespace internal
69 }  // namespace v8
70