Line data Source code
1 : // Copyright 2016 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_STARTUP_SERIALIZER_H_
6 : #define V8_SNAPSHOT_STARTUP_SERIALIZER_H_
7 :
8 : #include <bitset>
9 : #include "include/v8.h"
10 : #include "src/snapshot/serializer.h"
11 :
12 : namespace v8 {
13 : namespace internal {
14 :
15 : class StartupSerializer : public Serializer<> {
16 : public:
17 : StartupSerializer(
18 : Isolate* isolate,
19 : v8::SnapshotCreator::FunctionCodeHandling function_code_handling);
20 : ~StartupSerializer() override;
21 :
22 : // Serialize the current state of the heap. The order is:
23 : // 1) Immortal immovable roots
24 : // 2) Remaining strong references.
25 : // 3) Partial snapshot cache.
26 : // 4) Weak references (e.g. the string table).
27 : void SerializeStrongReferences();
28 : void SerializeWeakReferencesAndDeferred();
29 :
30 : int PartialSnapshotCacheIndex(HeapObject* o);
31 :
32 : bool can_be_rehashed() const { return can_be_rehashed_; }
33 : bool clear_function_code() const { return clear_function_code_; }
34 : bool root_has_been_serialized(int root_index) const {
35 2262453 : return root_has_been_serialized_.test(root_index);
36 : }
37 :
38 : private:
39 : class PartialCacheIndexMap {
40 : public:
41 322 : PartialCacheIndexMap() : map_(), next_index_(0) {}
42 :
43 : // Lookup object in the map. Return its index if found, or create
44 : // a new entry with new_index as value, and return kInvalidIndex.
45 791196 : bool LookupOrInsert(HeapObject* obj, int* index_out) {
46 791196 : Maybe<uint32_t> maybe_index = map_.Get(obj);
47 791196 : if (maybe_index.IsJust()) {
48 428683 : *index_out = maybe_index.FromJust();
49 428683 : return true;
50 : }
51 362513 : *index_out = next_index_;
52 362513 : map_.Set(obj, next_index_++);
53 362513 : return false;
54 : }
55 :
56 : private:
57 : DisallowHeapAllocation no_allocation_;
58 : HeapObjectToIndexHashMap map_;
59 : int next_index_;
60 :
61 : DISALLOW_COPY_AND_ASSIGN(PartialCacheIndexMap);
62 : };
63 :
64 : // The StartupSerializer has to serialize the root array, which is slightly
65 : // different.
66 : void VisitRootPointers(Root root, Object** start, Object** end) override;
67 : void SerializeObject(HeapObject* o, HowToCode how_to_code,
68 : WhereToPoint where_to_point, int skip) override;
69 : void Synchronize(VisitorSynchronization::SyncTag tag) override;
70 : bool MustBeDeferred(HeapObject* object) override;
71 :
72 : // Some roots should not be serialized, because their actual value depends on
73 : // absolute addresses and they are reset after deserialization, anyway.
74 : // In the first pass over the root list, we only serialize immortal immovable
75 : // roots. In the second pass, we serialize the rest.
76 : bool RootShouldBeSkipped(int root_index);
77 :
78 : void CheckRehashability(HeapObject* hashtable);
79 :
80 : const bool clear_function_code_;
81 : bool serializing_immortal_immovables_roots_;
82 : std::bitset<Heap::kStrongRootListLength> root_has_been_serialized_;
83 : PartialCacheIndexMap partial_cache_index_map_;
84 : std::vector<AccessorInfo*> accessor_infos_;
85 : // Indicates whether we only serialized hash tables that we can rehash.
86 : // TODO(yangguo): generalize rehashing, and remove this flag.
87 : bool can_be_rehashed_;
88 :
89 : DISALLOW_COPY_AND_ASSIGN(StartupSerializer);
90 : };
91 :
92 : } // namespace internal
93 : } // namespace v8
94 :
95 : #endif // V8_SNAPSHOT_STARTUP_SERIALIZER_H_
|