serializer-common.cc 3.29 KB
Newer Older
1 2 3 4 5 6
// Copyright 2016 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "src/snapshot/serializer-common.h"

7
#include "src/external-reference-table.h"
8 9
#include "src/ic/stub-cache.h"
#include "src/list-inl.h"
10
#include "src/objects-inl.h"
11 12 13 14 15 16

namespace v8 {
namespace internal {

ExternalReferenceEncoder::ExternalReferenceEncoder(Isolate* isolate) {
  map_ = isolate->external_reference_map();
17 18 19
#ifdef DEBUG
  table_ = ExternalReferenceTable::instance(isolate);
#endif  // DEBUG
20 21
  if (map_ != nullptr) return;
  map_ = new AddressToIndexHashMap();
22
  ExternalReferenceTable* table = ExternalReferenceTable::instance(isolate);
23
  for (uint32_t i = 0; i < table->size(); ++i) {
24
    Address addr = table->address(i);
25 26
    // Ignore duplicate API references.
    if (table->is_api_reference(i) && !map_->Get(addr).IsNothing()) continue;
27
    DCHECK(map_->Get(addr).IsNothing());
28 29
    map_->Set(addr, i);
    DCHECK(map_->Get(addr).IsJust());
30 31 32 33 34
  }
  isolate->set_external_reference_map(map_);
}

uint32_t ExternalReferenceEncoder::Encode(Address address) const {
35 36
  Maybe<uint32_t> maybe_index = map_->Get(address);
  if (maybe_index.IsNothing()) {
37 38 39
    void* addr = address;
    v8::base::OS::PrintError("Unknown external reference %p.\n", addr);
    v8::base::OS::PrintError("%s", ExternalReferenceTable::ResolveSymbol(addr));
40 41
    v8::base::OS::Abort();
  }
42 43 44
#ifdef DEBUG
  table_->increment_count(maybe_index.FromJust());
#endif  // DEBUG
45
  return maybe_index.FromJust();
46 47 48 49
}

const char* ExternalReferenceEncoder::NameOfAddress(Isolate* isolate,
                                                    Address address) const {
50 51 52 53
  Maybe<uint32_t> maybe_index = map_->Get(address);
  if (maybe_index.IsNothing()) return "<unknown>";
  return ExternalReferenceTable::instance(isolate)->name(
      maybe_index.FromJust());
54 55 56 57 58 59 60 61 62 63
}

void SerializedData::AllocateData(int size) {
  DCHECK(!owns_data_);
  data_ = NewArray<byte>(size);
  size_ = size;
  owns_data_ = true;
  DCHECK(IsAligned(reinterpret_cast<intptr_t>(data_), kPointerAlignment));
}

64 65 66 67 68
// The partial snapshot cache is terminated by undefined. We visit the
// partial snapshot...
//  - during deserialization to populate it.
//  - during normal GC to keep its content alive.
//  - not during serialization. The partial serializer adds to it explicitly.
krasin's avatar
krasin committed
69
DISABLE_CFI_PERF
70 71 72 73
void SerializerDeserializer::Iterate(Isolate* isolate, ObjectVisitor* visitor) {
  List<Object*>* cache = isolate->partial_snapshot_cache();
  for (int i = 0;; ++i) {
    // Extend the array ready to get a value when deserializing.
74
    if (cache->length() <= i) cache->Add(Smi::kZero);
75 76
    // During deserialization, the visitor populates the partial snapshot cache
    // and eventually terminates the cache with undefined.
77
    visitor->VisitPointer(&cache->at(i));
78
    if (cache->at(i)->IsUndefined(isolate)) break;
79 80 81 82 83 84 85
  }
}

bool SerializerDeserializer::CanBeDeferred(HeapObject* o) {
  return !o->IsString() && !o->IsScript();
}

86 87 88 89 90 91 92 93 94
void SerializerDeserializer::RestoreExternalReferenceRedirectors(
    List<AccessorInfo*>* accessor_infos) {
  // Restore wiped accessor infos.
  for (AccessorInfo* info : *accessor_infos) {
    Foreign::cast(info->js_getter())
        ->set_foreign_address(info->redirected_getter());
  }
}

95 96
}  // namespace internal
}  // namespace v8