5 #ifndef V8_SNAPSHOT_SERIALIZER_H_ 6 #define V8_SNAPSHOT_SERIALIZER_H_ 10 #include "src/isolate.h" 12 #include "src/objects.h" 13 #include "src/snapshot/embedded-data.h" 14 #include "src/snapshot/serializer-allocator.h" 15 #include "src/snapshot/serializer-common.h" 16 #include "src/snapshot/snapshot-source-sink.h" 24 isolate->logger()->AddCodeEventListener(
this);
28 isolate_->logger()->RemoveCodeEventListener(
this);
32 address_to_name_map_.Move(from->address(), to->address());
38 const char* Lookup(
Address address) {
39 return address_to_name_map_.Lookup(address);
45 NameMap() : impl_() {}
48 for (base::HashMap::Entry* p = impl_.Start(); p !=
nullptr;
50 DeleteArray(static_cast<const char*>(p->value));
54 void Insert(
Address code_address,
const char* name,
int name_size) {
55 base::HashMap::Entry* entry = FindOrCreateEntry(code_address);
56 if (entry->value ==
nullptr) {
57 entry->value = CopyName(name, name_size);
61 const char* Lookup(
Address code_address) {
62 base::HashMap::Entry* entry = FindEntry(code_address);
63 return (entry !=
nullptr) ?
static_cast<const char*
>(entry->value)
67 void Remove(
Address code_address) {
68 base::HashMap::Entry* entry = FindEntry(code_address);
69 if (entry !=
nullptr) {
70 DeleteArray(static_cast<char*>(entry->value));
76 if (from == to)
return;
77 base::HashMap::Entry* from_entry = FindEntry(from);
78 DCHECK_NOT_NULL(from_entry);
79 void* value = from_entry->value;
80 RemoveEntry(from_entry);
81 base::HashMap::Entry* to_entry = FindOrCreateEntry(to);
82 DCHECK_NULL(to_entry->value);
83 to_entry->value = value;
87 static char* CopyName(
const char* name,
int name_size) {
88 char* result = NewArray<char>(name_size + 1);
89 for (
int i = 0;
i < name_size; ++
i) {
91 if (c ==
'\0') c =
' ';
94 result[name_size] =
'\0';
98 base::HashMap::Entry* FindOrCreateEntry(
Address code_address) {
99 return impl_.LookupOrInsert(reinterpret_cast<void*>(code_address),
100 ComputeAddressHash(code_address));
103 base::HashMap::Entry* FindEntry(
Address code_address) {
104 return impl_.Lookup(reinterpret_cast<void*>(code_address),
105 ComputeAddressHash(code_address));
108 void RemoveEntry(base::HashMap::Entry* entry) {
109 impl_.Remove(entry->key, entry->hash);
114 DISALLOW_COPY_AND_ASSIGN(NameMap);
118 const char* name,
int length)
override {
119 address_to_name_map_.Insert(code->address(), name, length);
122 void LogRecordedBuffer(
const wasm::WasmCode* code,
const char* name,
123 int length)
override {
127 NameMap address_to_name_map_;
137 bool LookupOrInsert(
HeapObject* obj,
int* index_out) {
139 if (maybe_index.IsJust()) {
140 *index_out = maybe_index.
FromJust();
143 *index_out = next_index_;
144 map_.Set(obj, next_index_++);
162 std::vector<SerializedData::Reservation> EncodeReservations()
const {
163 return allocator_.EncodeReservations();
166 const std::vector<byte>* Payload()
const {
return sink_.data(); }
169 return reference_map()->LookupReference(o).is_valid();
172 Isolate* isolate()
const {
return isolate_; }
179 serializer_->recursion_depth_++;
182 bool ExceedsMaximum() {
183 return serializer_->recursion_depth_ >= kMaxRecursionDepth;
187 static const int kMaxRecursionDepth = 32;
191 void SerializeDeferredObjects();
192 virtual void SerializeObject(
HeapObject* o, HowToCode how_to_code,
193 WhereToPoint where_to_point,
int skip) = 0;
195 virtual bool MustBeDeferred(
HeapObject*
object);
197 void VisitRootPointers(Root root,
const char* description,
ObjectSlot start,
199 void SerializeRootObject(
Object*
object);
201 void PutRoot(RootIndex root_index,
HeapObject*
object, HowToCode how,
202 WhereToPoint where,
int skip);
203 void PutSmi(
Smi smi);
206 HowToCode how_to_code, WhereToPoint where_to_point);
209 void PutNextChunk(
int space);
212 bool SerializeRoot(
HeapObject* obj, HowToCode how_to_code,
213 WhereToPoint where_to_point,
int skip);
216 bool SerializeHotObject(
HeapObject* obj, HowToCode how_to_code,
217 WhereToPoint where_to_point,
int skip);
220 bool SerializeBackReference(
HeapObject* obj, HowToCode how_to_code,
221 WhereToPoint where_to_point,
int skip);
224 bool ObjectIsBytecodeHandler(
HeapObject* obj)
const;
228 sink->Put(kSkip,
"SkipFromSerializeObject");
229 sink->PutInt(skip,
"SkipDistanceFromSerializeObject");
233 inline void FlushSkip(
int skip) { FlushSkip(&sink_, skip); }
235 ExternalReferenceEncoder::Value EncodeExternalReference(Address addr) {
236 return external_reference_encoder_.Encode(addr);
241 void Pad(
int padding_offset = 0);
245 void InitializeCodeAddressMap();
247 Code CopyCode(Code code);
249 void QueueDeferredObject(HeapObject* obj) {
250 DCHECK(reference_map_.LookupReference(obj).is_back_reference());
251 deferred_objects_.push_back(obj);
254 void OutputStatistics(
const char* name);
257 void CountInstanceType(Map map,
int size, AllocationSpace space);
258 #endif // OBJECT_PRINT 261 void PushStack(HeapObject* o) { stack_.push_back(o); }
262 void PopStack() { stack_.pop_back(); }
266 SerializerReferenceMap* reference_map() {
return &reference_map_; }
267 const RootIndexMap* root_index_map()
const {
return &root_index_map_; }
268 SerializerAllocator* allocator() {
return &allocator_; }
270 SnapshotByteSink sink_;
274 SerializerReferenceMap reference_map_;
275 ExternalReferenceEncoder external_reference_encoder_;
276 RootIndexMap root_index_map_;
277 CodeAddressMap* code_address_map_ =
nullptr;
278 std::vector<byte> code_buffer_;
279 std::vector<HeapObject*> deferred_objects_;
280 int recursion_depth_ = 0;
281 SerializerAllocator allocator_;
284 static const int kInstanceTypes = LAST_TYPE + 1;
285 int* instance_type_count_[LAST_SPACE];
286 size_t* instance_type_size_[LAST_SPACE];
287 #endif // OBJECT_PRINT 290 std::vector<HeapObject*> stack_;
293 friend class SerializerAllocator;
295 DISALLOW_COPY_AND_ASSIGN(Serializer);
298 class RelocInfoIterator;
304 WhereToPoint where_to_point)
305 : serializer_(serializer),
308 reference_representation_(how_to_code + where_to_point),
309 bytes_processed_so_far_(0) {
311 serializer_->PushStack(obj);
317 serializer_->PopStack();
321 void SerializeObject();
322 void SerializeDeferred();
327 void VisitEmbeddedPointer(
Code host,
RelocInfo* target)
override;
329 void VisitExternalReference(
Code host,
RelocInfo* rinfo)
override;
330 void VisitInternalReference(
Code host,
RelocInfo* rinfo)
override;
332 void VisitRuntimeEntry(
Code host,
RelocInfo* reloc)
override;
333 void VisitOffHeapTarget(
Code host,
RelocInfo* target)
override;
338 void SerializePrologue(AllocationSpace space,
int size,
Map map);
342 void SerializeContent(
Map map,
int size);
343 void OutputRawData(
Address up_to);
344 void OutputCode(
int size);
346 int32_t SerializeBackingStore(
void* backing_store, int32_t byte_length);
347 void SerializeJSTypedArray();
348 void SerializeJSArrayBuffer();
349 void SerializeExternalString();
350 void SerializeExternalStringAsSequentialString();
355 int reference_representation_;
356 int bytes_processed_so_far_;
362 #endif // V8_SNAPSHOT_SERIALIZER_H_
V8_INLINE T FromJust() const