• 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_SNAPSHOT_ROOTS_SERIALIZER_H_
6 #define V8_SNAPSHOT_ROOTS_SERIALIZER_H_
7 
8 #include <bitset>
9 
10 #include "src/objects/visitors.h"
11 #include "src/snapshot/serializer.h"
12 
13 namespace v8 {
14 namespace internal {
15 
16 class HeapObject;
17 class Object;
18 class Isolate;
19 enum class RootIndex : uint16_t;
20 
21 // Base class for serializer that iterate over roots. Also maintains a cache
22 // that can be used to share non-root objects with other serializers.
23 class RootsSerializer : public Serializer {
24  public:
25   // The serializer expects that all roots before |first_root_to_be_serialized|
26   // are already serialized.
27   RootsSerializer(Isolate* isolate, Snapshot::SerializerFlags flags,
28                   RootIndex first_root_to_be_serialized);
29   RootsSerializer(const RootsSerializer&) = delete;
30   RootsSerializer& operator=(const RootsSerializer&) = delete;
31 
can_be_rehashed()32   bool can_be_rehashed() const { return can_be_rehashed_; }
root_has_been_serialized(RootIndex root_index)33   bool root_has_been_serialized(RootIndex root_index) const {
34     return root_has_been_serialized_.test(static_cast<size_t>(root_index));
35   }
36 
IsRootAndHasBeenSerialized(HeapObject obj)37   bool IsRootAndHasBeenSerialized(HeapObject obj) const {
38     RootIndex root_index;
39     return root_index_map()->Lookup(obj, &root_index) &&
40            root_has_been_serialized(root_index);
41   }
42 
43  protected:
44   void CheckRehashability(HeapObject obj);
45 
46   // Serializes |object| if not previously seen and returns its cache index.
47   int SerializeInObjectCache(Handle<HeapObject> object);
48 
49  private:
50   void VisitRootPointers(Root root, const char* description,
51                          FullObjectSlot start, FullObjectSlot end) override;
52   void Synchronize(VisitorSynchronization::SyncTag tag) override;
53 
54   const RootIndex first_root_to_be_serialized_;
55   std::bitset<RootsTable::kEntriesCount> root_has_been_serialized_;
56   ObjectCacheIndexMap object_cache_index_map_;
57   // Indicates whether we only serialized hash tables that we can rehash.
58   // TODO(yangguo): generalize rehashing, and remove this flag.
59   bool can_be_rehashed_;
60 };
61 
62 }  // namespace internal
63 }  // namespace v8
64 
65 #endif  // V8_SNAPSHOT_ROOTS_SERIALIZER_H_
66