Home | History | Annotate | Download | only in snapshot
      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   explicit StartupSerializer(Isolate* isolate);
     18   ~StartupSerializer() override;
     19 
     20   // Serialize the current state of the heap.  The order is:
     21   // 1) Strong roots
     22   // 2) Builtins and bytecode handlers
     23   // 3) Partial snapshot cache
     24   // 4) Weak references (e.g. the string table)
     25   void SerializeStrongReferences();
     26   void SerializeWeakReferencesAndDeferred();
     27 
     28   int PartialSnapshotCacheIndex(HeapObject* o);
     29 
     30   bool can_be_rehashed() const { return can_be_rehashed_; }
     31   bool root_has_been_serialized(int root_index) const {
     32     return root_has_been_serialized_.test(root_index);
     33   }
     34 
     35  private:
     36   class PartialCacheIndexMap {
     37    public:
     38     PartialCacheIndexMap() : map_(), next_index_(0) {}
     39 
     40     // Lookup object in the map. Return its index if found, or create
     41     // a new entry with new_index as value, and return kInvalidIndex.
     42     bool LookupOrInsert(HeapObject* obj, int* index_out) {
     43       Maybe<uint32_t> maybe_index = map_.Get(obj);
     44       if (maybe_index.IsJust()) {
     45         *index_out = maybe_index.FromJust();
     46         return true;
     47       }
     48       *index_out = next_index_;
     49       map_.Set(obj, next_index_++);
     50       return false;
     51     }
     52 
     53    private:
     54     DisallowHeapAllocation no_allocation_;
     55     HeapObjectToIndexHashMap map_;
     56     int next_index_;
     57 
     58     DISALLOW_COPY_AND_ASSIGN(PartialCacheIndexMap);
     59   };
     60 
     61   // The StartupSerializer has to serialize the root array, which is slightly
     62   // different.
     63   void VisitRootPointers(Root root, const char* description, Object** start,
     64                          Object** end) override;
     65   void SerializeObject(HeapObject* o, HowToCode how_to_code,
     66                        WhereToPoint where_to_point, int skip) override;
     67   void Synchronize(VisitorSynchronization::SyncTag tag) override;
     68   bool MustBeDeferred(HeapObject* object) override;
     69 
     70   void CheckRehashability(HeapObject* obj);
     71 
     72   std::bitset<Heap::kStrongRootListLength> root_has_been_serialized_;
     73   PartialCacheIndexMap partial_cache_index_map_;
     74   std::vector<AccessorInfo*> accessor_infos_;
     75   std::vector<CallHandlerInfo*> call_handler_infos_;
     76   // Indicates whether we only serialized hash tables that we can rehash.
     77   // TODO(yangguo): generalize rehashing, and remove this flag.
     78   bool can_be_rehashed_;
     79 
     80   DISALLOW_COPY_AND_ASSIGN(StartupSerializer);
     81 };
     82 
     83 class SerializedHandleChecker : public RootVisitor {
     84  public:
     85   SerializedHandleChecker(Isolate* isolate, std::vector<Context*>* contexts);
     86   virtual void VisitRootPointers(Root root, const char* description,
     87                                  Object** start, Object** end);
     88   bool CheckGlobalAndEternalHandles();
     89 
     90  private:
     91   void AddToSet(FixedArray* serialized);
     92 
     93   Isolate* isolate_;
     94   std::unordered_set<Object*> serialized_;
     95   bool ok_ = true;
     96 };
     97 
     98 }  // namespace internal
     99 }  // namespace v8
    100 
    101 #endif  // V8_SNAPSHOT_STARTUP_SERIALIZER_H_
    102