• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2021 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_SHARED_HEAP_SERIALIZER_H_
6 #define V8_SNAPSHOT_SHARED_HEAP_SERIALIZER_H_
7 
8 #include "src/snapshot/roots-serializer.h"
9 
10 namespace v8 {
11 namespace internal {
12 
13 class HeapObject;
14 class ReadOnlySerializer;
15 
16 // SharedHeapSerializer serializes objects that should be in the shared heap in
17 // the shared Isolate during startup. Currently the shared heap is only in use
18 // behind flags (e.g. --shared-string-table). When it is not in use, its
19 // contents are deserialized into each Isolate.
20 class V8_EXPORT_PRIVATE SharedHeapSerializer : public RootsSerializer {
21  public:
22   SharedHeapSerializer(Isolate* isolate, Snapshot::SerializerFlags flags,
23                        ReadOnlySerializer* read_only_serializer);
24   ~SharedHeapSerializer() override;
25   SharedHeapSerializer(const SharedHeapSerializer&) = delete;
26   SharedHeapSerializer& operator=(const SharedHeapSerializer&) = delete;
27 
28   // Terminate the shared heap object cache with an undefined value and
29   // serialize the string table..
30   void FinalizeSerialization();
31 
32   // If |obj| can be serialized in the read-only snapshot then add it to the
33   // read-only object cache if not already present and emit a
34   // ReadOnlyObjectCache bytecode into |sink|. Returns whether this was
35   // successful.
36   bool SerializeUsingReadOnlyObjectCache(SnapshotByteSink* sink,
37                                          Handle<HeapObject> obj);
38 
39   // If |obj| can be serialized in the shared heap snapshot then add it to the
40   // shared heap object cache if not already present and emit a
41   // SharedHeapObjectCache bytecode into |sink|. Returns whether this was
42   // successful.
43   bool SerializeUsingSharedHeapObjectCache(SnapshotByteSink* sink,
44                                            Handle<HeapObject> obj);
45 
46   static bool CanBeInSharedOldSpace(HeapObject obj);
47 
48   static bool ShouldBeInSharedHeapObjectCache(HeapObject obj);
49 
50  private:
51   bool ShouldReconstructSharedHeapObjectCacheForTesting() const;
52 
53   void ReconstructSharedHeapObjectCacheForTesting();
54 
55   void SerializeStringTable(StringTable* string_table);
56 
57   void SerializeObjectImpl(Handle<HeapObject> obj) override;
58 
59   ReadOnlySerializer* read_only_serializer_;
60 
61 #ifdef DEBUG
62   IdentityMap<int, base::DefaultAllocationPolicy> serialized_objects_;
63 #endif
64 };
65 
66 }  // namespace internal
67 }  // namespace v8
68 
69 #endif  // V8_SNAPSHOT_SHARED_HEAP_SERIALIZER_H_
70