roots-serializer.cc 2.38 KB
Newer Older
1 2 3 4 5 6
// Copyright 2018 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "src/snapshot/roots-serializer.h"

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 13 14 15

namespace v8 {
namespace internal {

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 22 23 24 25 26 27
      can_be_rehashed_(true) {
  for (size_t i = 0; i < static_cast<size_t>(first_root_to_be_serialized);
       ++i) {
    root_has_been_serialized_[i] = true;
  }
}

28
int RootsSerializer::SerializeInObjectCache(Handle<HeapObject> heap_object) {
29 30 31 32
  int index;
  if (!object_cache_index_map_.LookupOrInsert(heap_object, &index)) {
    // This object is not part of the object cache yet. Add it to the cache so
    // we can refer to it via cache index from the delegating snapshot.
33
    SerializeObject(heap_object);
34 35 36 37 38 39 40 41 42
  }
  return index;
}

void RootsSerializer::Synchronize(VisitorSynchronization::SyncTag tag) {
  sink_.Put(kSynchronize, "Synchronize");
}

void RootsSerializer::VisitRootPointers(Root root, const char* description,
43 44
                                        FullObjectSlot start,
                                        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 49 50
    // Serializing the root list needs special handling:
    // - Only root list elements that have been fully serialized can be
    //   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 55 56 57 58 59 60
      root_has_been_serialized_.set(root_index);
    }
  } else {
    Serializer::VisitRootPointers(root, description, start, end);
  }
}

61
void RootsSerializer::CheckRehashability(HeapObject obj) {
62
  if (!can_be_rehashed_) return;
63 64
  if (!obj.NeedsRehashing()) return;
  if (obj.CanBeRehashed()) return;
65 66 67 68 69
  can_be_rehashed_ = false;
}

}  // namespace internal
}  // namespace v8