1 // Copyright 2013 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_PROFILER_HEAP_SNAPSHOT_GENERATOR_INL_H_
6 #define V8_PROFILER_HEAP_SNAPSHOT_GENERATOR_INL_H_
7 
8 #include "src/profiler/heap-snapshot-generator.h"
9 
10 #include "src/profiler/heap-profiler.h"
11 #include "src/strings/string-hasher-inl.h"
12 
13 namespace v8 {
14 namespace internal {
15 
from()16 HeapEntry* HeapGraphEdge::from() const {
17   return &snapshot()->entries()[from_index()];
18 }
19 
isolate()20 Isolate* HeapGraphEdge::isolate() const { return to_entry_->isolate(); }
21 
snapshot()22 HeapSnapshot* HeapGraphEdge::snapshot() const {
23   return to_entry_->snapshot();
24 }
25 
set_children_index(int index)26 int HeapEntry::set_children_index(int index) {
27   // Note: children_count_ and children_end_index_ are parts of a union.
28   int next_index = index + children_count_;
29   children_end_index_ = index;
30   return next_index;
31 }
32 
add_child(HeapGraphEdge * edge)33 void HeapEntry::add_child(HeapGraphEdge* edge) {
34   snapshot_->children()[children_end_index_++] = edge;
35 }
36 
child(int i)37 HeapGraphEdge* HeapEntry::child(int i) { return children_begin()[i]; }
38 
children_begin()39 std::vector<HeapGraphEdge*>::iterator HeapEntry::children_begin() const {
40   return index_ == 0 ? snapshot_->children().begin()
41                      : snapshot_->entries()[index_ - 1].children_end();
42 }
43 
children_end()44 std::vector<HeapGraphEdge*>::iterator HeapEntry::children_end() const {
45   DCHECK_GE(children_end_index_, 0);
46   return snapshot_->children().begin() + children_end_index_;
47 }
48 
children_count()49 int HeapEntry::children_count() const {
50   return static_cast<int>(children_end() - children_begin());
51 }
52 
isolate()53 Isolate* HeapEntry::isolate() const { return snapshot_->profiler()->isolate(); }
54 
StringHash(const void * string)55 uint32_t HeapSnapshotJSONSerializer::StringHash(const void* string) {
56   const char* s = reinterpret_cast<const char*>(string);
57   int len = static_cast<int>(strlen(s));
58   return StringHasher::HashSequentialString(s, len,
59                                             v8::internal::kZeroHashSeed);
60 }
61 
to_node_index(const HeapEntry * e)62 int HeapSnapshotJSONSerializer::to_node_index(const HeapEntry* e) {
63   return to_node_index(e->index());
64 }
65 
to_node_index(int entry_index)66 int HeapSnapshotJSONSerializer::to_node_index(int entry_index) {
67   return entry_index * kNodeFieldsCount;
68 }
69 
70 }  // namespace internal
71 }  // namespace v8
72 
73 #endif  // V8_PROFILER_HEAP_SNAPSHOT_GENERATOR_INL_H_
74