Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 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 | #include "src/snapshot/serializer-common.h" |
| 6 | |
| 7 | #include "src/external-reference-table.h" |
| 8 | #include "src/ic/stub-cache.h" |
| 9 | #include "src/list-inl.h" |
| 10 | |
| 11 | namespace v8 { |
| 12 | namespace internal { |
| 13 | |
| 14 | ExternalReferenceEncoder::ExternalReferenceEncoder(Isolate* isolate) { |
| 15 | map_ = isolate->external_reference_map(); |
| 16 | if (map_ != NULL) return; |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 17 | map_ = new base::HashMap(base::HashMap::PointersMatch); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 18 | ExternalReferenceTable* table = ExternalReferenceTable::instance(isolate); |
| 19 | for (int i = 0; i < table->size(); ++i) { |
| 20 | Address addr = table->address(i); |
| 21 | if (addr == ExternalReferenceTable::NotAvailable()) continue; |
| 22 | // We expect no duplicate external references entries in the table. |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 23 | // AccessorRefTable getter may have duplicates, indicated by an empty string |
| 24 | // as name. |
| 25 | DCHECK(table->name(i)[0] == '\0' || |
| 26 | map_->Lookup(addr, Hash(addr)) == nullptr); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 27 | map_->LookupOrInsert(addr, Hash(addr))->value = reinterpret_cast<void*>(i); |
| 28 | } |
| 29 | isolate->set_external_reference_map(map_); |
| 30 | } |
| 31 | |
| 32 | uint32_t ExternalReferenceEncoder::Encode(Address address) const { |
| 33 | DCHECK_NOT_NULL(address); |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 34 | base::HashMap::Entry* entry = |
| 35 | const_cast<base::HashMap*>(map_)->Lookup(address, Hash(address)); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 36 | DCHECK_NOT_NULL(entry); |
| 37 | return static_cast<uint32_t>(reinterpret_cast<intptr_t>(entry->value)); |
| 38 | } |
| 39 | |
| 40 | const char* ExternalReferenceEncoder::NameOfAddress(Isolate* isolate, |
| 41 | Address address) const { |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 42 | base::HashMap::Entry* entry = |
| 43 | const_cast<base::HashMap*>(map_)->Lookup(address, Hash(address)); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 44 | if (entry == NULL) return "<unknown>"; |
| 45 | uint32_t i = static_cast<uint32_t>(reinterpret_cast<intptr_t>(entry->value)); |
| 46 | return ExternalReferenceTable::instance(isolate)->name(i); |
| 47 | } |
| 48 | |
| 49 | void SerializedData::AllocateData(int size) { |
| 50 | DCHECK(!owns_data_); |
| 51 | data_ = NewArray<byte>(size); |
| 52 | size_ = size; |
| 53 | owns_data_ = true; |
| 54 | DCHECK(IsAligned(reinterpret_cast<intptr_t>(data_), kPointerAlignment)); |
| 55 | } |
| 56 | |
| 57 | // The partial snapshot cache is terminated by undefined. We visit the |
| 58 | // partial snapshot... |
| 59 | // - during deserialization to populate it. |
| 60 | // - during normal GC to keep its content alive. |
| 61 | // - not during serialization. The partial serializer adds to it explicitly. |
| 62 | void SerializerDeserializer::Iterate(Isolate* isolate, ObjectVisitor* visitor) { |
| 63 | List<Object*>* cache = isolate->partial_snapshot_cache(); |
| 64 | for (int i = 0;; ++i) { |
| 65 | // Extend the array ready to get a value when deserializing. |
| 66 | if (cache->length() <= i) cache->Add(Smi::FromInt(0)); |
| 67 | // During deserialization, the visitor populates the partial snapshot cache |
| 68 | // and eventually terminates the cache with undefined. |
| 69 | visitor->VisitPointer(&cache->at(i)); |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 70 | if (cache->at(i)->IsUndefined(isolate)) break; |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 71 | } |
| 72 | } |
| 73 | |
| 74 | bool SerializerDeserializer::CanBeDeferred(HeapObject* o) { |
| 75 | return !o->IsString() && !o->IsScript(); |
| 76 | } |
| 77 | |
| 78 | } // namespace internal |
| 79 | } // namespace v8 |