11cb0ef41Sopenharmony_ci// Copyright 2021 the V8 project authors. All rights reserved.
21cb0ef41Sopenharmony_ci// Use of this source code is governed by a BSD-style license that can be
31cb0ef41Sopenharmony_ci// found in the LICENSE file.
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ci#ifndef V8_SNAPSHOT_SHARED_HEAP_SERIALIZER_H_
61cb0ef41Sopenharmony_ci#define V8_SNAPSHOT_SHARED_HEAP_SERIALIZER_H_
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ci#include "src/snapshot/roots-serializer.h"
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_cinamespace v8 {
111cb0ef41Sopenharmony_cinamespace internal {
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_ciclass HeapObject;
141cb0ef41Sopenharmony_ciclass ReadOnlySerializer;
151cb0ef41Sopenharmony_ci
161cb0ef41Sopenharmony_ci// SharedHeapSerializer serializes objects that should be in the shared heap in
171cb0ef41Sopenharmony_ci// the shared Isolate during startup. Currently the shared heap is only in use
181cb0ef41Sopenharmony_ci// behind flags (e.g. --shared-string-table). When it is not in use, its
191cb0ef41Sopenharmony_ci// contents are deserialized into each Isolate.
201cb0ef41Sopenharmony_ciclass V8_EXPORT_PRIVATE SharedHeapSerializer : public RootsSerializer {
211cb0ef41Sopenharmony_ci public:
221cb0ef41Sopenharmony_ci  SharedHeapSerializer(Isolate* isolate, Snapshot::SerializerFlags flags,
231cb0ef41Sopenharmony_ci                       ReadOnlySerializer* read_only_serializer);
241cb0ef41Sopenharmony_ci  ~SharedHeapSerializer() override;
251cb0ef41Sopenharmony_ci  SharedHeapSerializer(const SharedHeapSerializer&) = delete;
261cb0ef41Sopenharmony_ci  SharedHeapSerializer& operator=(const SharedHeapSerializer&) = delete;
271cb0ef41Sopenharmony_ci
281cb0ef41Sopenharmony_ci  // Terminate the shared heap object cache with an undefined value and
291cb0ef41Sopenharmony_ci  // serialize the string table..
301cb0ef41Sopenharmony_ci  void FinalizeSerialization();
311cb0ef41Sopenharmony_ci
321cb0ef41Sopenharmony_ci  // If |obj| can be serialized in the read-only snapshot then add it to the
331cb0ef41Sopenharmony_ci  // read-only object cache if not already present and emit a
341cb0ef41Sopenharmony_ci  // ReadOnlyObjectCache bytecode into |sink|. Returns whether this was
351cb0ef41Sopenharmony_ci  // successful.
361cb0ef41Sopenharmony_ci  bool SerializeUsingReadOnlyObjectCache(SnapshotByteSink* sink,
371cb0ef41Sopenharmony_ci                                         Handle<HeapObject> obj);
381cb0ef41Sopenharmony_ci
391cb0ef41Sopenharmony_ci  // If |obj| can be serialized in the shared heap snapshot then add it to the
401cb0ef41Sopenharmony_ci  // shared heap object cache if not already present and emit a
411cb0ef41Sopenharmony_ci  // SharedHeapObjectCache bytecode into |sink|. Returns whether this was
421cb0ef41Sopenharmony_ci  // successful.
431cb0ef41Sopenharmony_ci  bool SerializeUsingSharedHeapObjectCache(SnapshotByteSink* sink,
441cb0ef41Sopenharmony_ci                                           Handle<HeapObject> obj);
451cb0ef41Sopenharmony_ci
461cb0ef41Sopenharmony_ci  static bool CanBeInSharedOldSpace(HeapObject obj);
471cb0ef41Sopenharmony_ci
481cb0ef41Sopenharmony_ci  static bool ShouldBeInSharedHeapObjectCache(HeapObject obj);
491cb0ef41Sopenharmony_ci
501cb0ef41Sopenharmony_ci private:
511cb0ef41Sopenharmony_ci  bool ShouldReconstructSharedHeapObjectCacheForTesting() const;
521cb0ef41Sopenharmony_ci
531cb0ef41Sopenharmony_ci  void ReconstructSharedHeapObjectCacheForTesting();
541cb0ef41Sopenharmony_ci
551cb0ef41Sopenharmony_ci  void SerializeStringTable(StringTable* string_table);
561cb0ef41Sopenharmony_ci
571cb0ef41Sopenharmony_ci  void SerializeObjectImpl(Handle<HeapObject> obj) override;
581cb0ef41Sopenharmony_ci
591cb0ef41Sopenharmony_ci  ReadOnlySerializer* read_only_serializer_;
601cb0ef41Sopenharmony_ci
611cb0ef41Sopenharmony_ci#ifdef DEBUG
621cb0ef41Sopenharmony_ci  IdentityMap<int, base::DefaultAllocationPolicy> serialized_objects_;
631cb0ef41Sopenharmony_ci#endif
641cb0ef41Sopenharmony_ci};
651cb0ef41Sopenharmony_ci
661cb0ef41Sopenharmony_ci}  // namespace internal
671cb0ef41Sopenharmony_ci}  // namespace v8
681cb0ef41Sopenharmony_ci
691cb0ef41Sopenharmony_ci#endif  // V8_SNAPSHOT_SHARED_HEAP_SERIALIZER_H_
70