| OLD | NEW |
| 1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 the V8 project authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef V8_SNAPSHOT_SERIALIZE_H_ | 5 #ifndef V8_SNAPSHOT_SERIALIZE_H_ |
| 6 #define V8_SNAPSHOT_SERIALIZE_H_ | 6 #define V8_SNAPSHOT_SERIALIZE_H_ |
| 7 | 7 |
| 8 #include "src/hashmap.h" | 8 #include "src/address-map.h" |
| 9 #include "src/heap/heap.h" | 9 #include "src/heap/heap.h" |
| 10 #include "src/objects.h" | 10 #include "src/objects.h" |
| 11 #include "src/snapshot/snapshot-source-sink.h" | 11 #include "src/snapshot/snapshot-source-sink.h" |
| 12 | 12 |
| 13 namespace v8 { | 13 namespace v8 { |
| 14 namespace internal { | 14 namespace internal { |
| 15 | 15 |
| 16 class Isolate; | 16 class Isolate; |
| 17 class ScriptData; | 17 class ScriptData; |
| 18 | 18 |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 63 return static_cast<uint32_t>(reinterpret_cast<uintptr_t>(key) >> | 63 return static_cast<uint32_t>(reinterpret_cast<uintptr_t>(key) >> |
| 64 kPointerSizeLog2); | 64 kPointerSizeLog2); |
| 65 } | 65 } |
| 66 | 66 |
| 67 HashMap* map_; | 67 HashMap* map_; |
| 68 | 68 |
| 69 DISALLOW_COPY_AND_ASSIGN(ExternalReferenceEncoder); | 69 DISALLOW_COPY_AND_ASSIGN(ExternalReferenceEncoder); |
| 70 }; | 70 }; |
| 71 | 71 |
| 72 | 72 |
| 73 class AddressMapBase { | |
| 74 protected: | |
| 75 static void SetValue(HashMap::Entry* entry, uint32_t v) { | |
| 76 entry->value = reinterpret_cast<void*>(v); | |
| 77 } | |
| 78 | |
| 79 static uint32_t GetValue(HashMap::Entry* entry) { | |
| 80 return static_cast<uint32_t>(reinterpret_cast<intptr_t>(entry->value)); | |
| 81 } | |
| 82 | |
| 83 inline static HashMap::Entry* LookupEntry(HashMap* map, HeapObject* obj, | |
| 84 bool insert) { | |
| 85 if (insert) { | |
| 86 map->LookupOrInsert(Key(obj), Hash(obj)); | |
| 87 } | |
| 88 return map->Lookup(Key(obj), Hash(obj)); | |
| 89 } | |
| 90 | |
| 91 private: | |
| 92 static uint32_t Hash(HeapObject* obj) { | |
| 93 return static_cast<int32_t>(reinterpret_cast<intptr_t>(obj->address())); | |
| 94 } | |
| 95 | |
| 96 static void* Key(HeapObject* obj) { | |
| 97 return reinterpret_cast<void*>(obj->address()); | |
| 98 } | |
| 99 }; | |
| 100 | |
| 101 | |
| 102 class RootIndexMap : public AddressMapBase { | |
| 103 public: | |
| 104 explicit RootIndexMap(Isolate* isolate); | |
| 105 | |
| 106 static const int kInvalidRootIndex = -1; | |
| 107 | |
| 108 int Lookup(HeapObject* obj) { | |
| 109 HashMap::Entry* entry = LookupEntry(map_, obj, false); | |
| 110 if (entry) return GetValue(entry); | |
| 111 return kInvalidRootIndex; | |
| 112 } | |
| 113 | |
| 114 private: | |
| 115 HashMap* map_; | |
| 116 | |
| 117 DISALLOW_COPY_AND_ASSIGN(RootIndexMap); | |
| 118 }; | |
| 119 | |
| 120 | |
| 121 class PartialCacheIndexMap : public AddressMapBase { | 73 class PartialCacheIndexMap : public AddressMapBase { |
| 122 public: | 74 public: |
| 123 PartialCacheIndexMap() : map_(HashMap::PointersMatch) {} | 75 PartialCacheIndexMap() : map_(HashMap::PointersMatch) {} |
| 124 | 76 |
| 125 static const int kInvalidIndex = -1; | 77 static const int kInvalidIndex = -1; |
| 126 | 78 |
| 127 // Lookup object in the map. Return its index if found, or create | 79 // Lookup object in the map. Return its index if found, or create |
| 128 // a new entry with new_index as value, and return kInvalidIndex. | 80 // a new entry with new_index as value, and return kInvalidIndex. |
| 129 int LookupOrInsert(HeapObject* obj, int new_index) { | 81 int LookupOrInsert(HeapObject* obj, int new_index) { |
| 130 HashMap::Entry* entry = LookupEntry(&map_, obj, false); | 82 HashMap::Entry* entry = LookupEntry(&map_, obj, false); |
| 131 if (entry != NULL) return GetValue(entry); | 83 if (entry != NULL) return GetValue(entry); |
| 132 SetValue(LookupEntry(&map_, obj, true), static_cast<uint32_t>(new_index)); | 84 SetValue(LookupEntry(&map_, obj, true), static_cast<uint32_t>(new_index)); |
| 133 return kInvalidIndex; | 85 return kInvalidIndex; |
| 134 } | 86 } |
| 135 | 87 |
| 136 private: | 88 private: |
| 137 HashMap map_; | 89 HashMap map_; |
| 138 | 90 |
| 139 DISALLOW_COPY_AND_ASSIGN(PartialCacheIndexMap); | 91 DISALLOW_COPY_AND_ASSIGN(PartialCacheIndexMap); |
| 140 }; | 92 }; |
| 141 | 93 |
| 142 | 94 |
| 143 class BackReference { | |
| 144 public: | |
| 145 explicit BackReference(uint32_t bitfield) : bitfield_(bitfield) {} | |
| 146 | |
| 147 BackReference() : bitfield_(kInvalidValue) {} | |
| 148 | |
| 149 static BackReference SourceReference() { return BackReference(kSourceValue); } | |
| 150 | |
| 151 static BackReference GlobalProxyReference() { | |
| 152 return BackReference(kGlobalProxyValue); | |
| 153 } | |
| 154 | |
| 155 static BackReference LargeObjectReference(uint32_t index) { | |
| 156 return BackReference(SpaceBits::encode(LO_SPACE) | | |
| 157 ChunkOffsetBits::encode(index)); | |
| 158 } | |
| 159 | |
| 160 static BackReference DummyReference() { return BackReference(kDummyValue); } | |
| 161 | |
| 162 static BackReference Reference(AllocationSpace space, uint32_t chunk_index, | |
| 163 uint32_t chunk_offset) { | |
| 164 DCHECK(IsAligned(chunk_offset, kObjectAlignment)); | |
| 165 DCHECK_NE(LO_SPACE, space); | |
| 166 return BackReference( | |
| 167 SpaceBits::encode(space) | ChunkIndexBits::encode(chunk_index) | | |
| 168 ChunkOffsetBits::encode(chunk_offset >> kObjectAlignmentBits)); | |
| 169 } | |
| 170 | |
| 171 bool is_valid() const { return bitfield_ != kInvalidValue; } | |
| 172 bool is_source() const { return bitfield_ == kSourceValue; } | |
| 173 bool is_global_proxy() const { return bitfield_ == kGlobalProxyValue; } | |
| 174 | |
| 175 AllocationSpace space() const { | |
| 176 DCHECK(is_valid()); | |
| 177 return SpaceBits::decode(bitfield_); | |
| 178 } | |
| 179 | |
| 180 uint32_t chunk_offset() const { | |
| 181 DCHECK(is_valid()); | |
| 182 return ChunkOffsetBits::decode(bitfield_) << kObjectAlignmentBits; | |
| 183 } | |
| 184 | |
| 185 uint32_t large_object_index() const { | |
| 186 DCHECK(is_valid()); | |
| 187 DCHECK(chunk_index() == 0); | |
| 188 return ChunkOffsetBits::decode(bitfield_); | |
| 189 } | |
| 190 | |
| 191 uint32_t chunk_index() const { | |
| 192 DCHECK(is_valid()); | |
| 193 return ChunkIndexBits::decode(bitfield_); | |
| 194 } | |
| 195 | |
| 196 uint32_t reference() const { | |
| 197 DCHECK(is_valid()); | |
| 198 return bitfield_ & (ChunkOffsetBits::kMask | ChunkIndexBits::kMask); | |
| 199 } | |
| 200 | |
| 201 uint32_t bitfield() const { return bitfield_; } | |
| 202 | |
| 203 private: | |
| 204 static const uint32_t kInvalidValue = 0xFFFFFFFF; | |
| 205 static const uint32_t kSourceValue = 0xFFFFFFFE; | |
| 206 static const uint32_t kGlobalProxyValue = 0xFFFFFFFD; | |
| 207 static const uint32_t kDummyValue = 0xFFFFFFFC; | |
| 208 static const int kChunkOffsetSize = kPageSizeBits - kObjectAlignmentBits; | |
| 209 static const int kChunkIndexSize = 32 - kChunkOffsetSize - kSpaceTagSize; | |
| 210 | |
| 211 public: | |
| 212 static const int kMaxChunkIndex = (1 << kChunkIndexSize) - 1; | |
| 213 | |
| 214 private: | |
| 215 class ChunkOffsetBits : public BitField<uint32_t, 0, kChunkOffsetSize> {}; | |
| 216 class ChunkIndexBits | |
| 217 : public BitField<uint32_t, ChunkOffsetBits::kNext, kChunkIndexSize> {}; | |
| 218 class SpaceBits | |
| 219 : public BitField<AllocationSpace, ChunkIndexBits::kNext, kSpaceTagSize> { | |
| 220 }; | |
| 221 | |
| 222 uint32_t bitfield_; | |
| 223 }; | |
| 224 | |
| 225 | |
| 226 // Mapping objects to their location after deserialization. | |
| 227 // This is used during building, but not at runtime by V8. | |
| 228 class BackReferenceMap : public AddressMapBase { | |
| 229 public: | |
| 230 BackReferenceMap() | |
| 231 : no_allocation_(), map_(new HashMap(HashMap::PointersMatch)) {} | |
| 232 | |
| 233 ~BackReferenceMap() { delete map_; } | |
| 234 | |
| 235 BackReference Lookup(HeapObject* obj) { | |
| 236 HashMap::Entry* entry = LookupEntry(map_, obj, false); | |
| 237 return entry ? BackReference(GetValue(entry)) : BackReference(); | |
| 238 } | |
| 239 | |
| 240 void Add(HeapObject* obj, BackReference b) { | |
| 241 DCHECK(b.is_valid()); | |
| 242 DCHECK_NULL(LookupEntry(map_, obj, false)); | |
| 243 HashMap::Entry* entry = LookupEntry(map_, obj, true); | |
| 244 SetValue(entry, b.bitfield()); | |
| 245 } | |
| 246 | |
| 247 void AddSourceString(String* string) { | |
| 248 Add(string, BackReference::SourceReference()); | |
| 249 } | |
| 250 | |
| 251 void AddGlobalProxy(HeapObject* global_proxy) { | |
| 252 Add(global_proxy, BackReference::GlobalProxyReference()); | |
| 253 } | |
| 254 | |
| 255 private: | |
| 256 DisallowHeapAllocation no_allocation_; | |
| 257 HashMap* map_; | |
| 258 DISALLOW_COPY_AND_ASSIGN(BackReferenceMap); | |
| 259 }; | |
| 260 | |
| 261 | |
| 262 class HotObjectsList { | 95 class HotObjectsList { |
| 263 public: | 96 public: |
| 264 HotObjectsList() : index_(0) { | 97 HotObjectsList() : index_(0) { |
| 265 for (int i = 0; i < kSize; i++) circular_queue_[i] = NULL; | 98 for (int i = 0; i < kSize; i++) circular_queue_[i] = NULL; |
| 266 } | 99 } |
| 267 | 100 |
| 268 void Add(HeapObject* object) { | 101 void Add(HeapObject* object) { |
| 269 circular_queue_[index_] = object; | 102 circular_queue_[index_] = object; |
| 270 index_ = (index_ + 1) & kSizeMask; | 103 index_ = (index_ + 1) & kSizeMask; |
| 271 } | 104 } |
| (...skipping 704 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 976 static const int kNumCodeStubKeysOffset = kNumReservationsOffset + kInt32Size; | 809 static const int kNumCodeStubKeysOffset = kNumReservationsOffset + kInt32Size; |
| 977 static const int kPayloadLengthOffset = kNumCodeStubKeysOffset + kInt32Size; | 810 static const int kPayloadLengthOffset = kNumCodeStubKeysOffset + kInt32Size; |
| 978 static const int kChecksum1Offset = kPayloadLengthOffset + kInt32Size; | 811 static const int kChecksum1Offset = kPayloadLengthOffset + kInt32Size; |
| 979 static const int kChecksum2Offset = kChecksum1Offset + kInt32Size; | 812 static const int kChecksum2Offset = kChecksum1Offset + kInt32Size; |
| 980 static const int kHeaderSize = kChecksum2Offset + kInt32Size; | 813 static const int kHeaderSize = kChecksum2Offset + kInt32Size; |
| 981 }; | 814 }; |
| 982 } // namespace internal | 815 } // namespace internal |
| 983 } // namespace v8 | 816 } // namespace v8 |
| 984 | 817 |
| 985 #endif // V8_SNAPSHOT_SERIALIZE_H_ | 818 #endif // V8_SNAPSHOT_SERIALIZE_H_ |
| OLD | NEW |