summaryrefslogtreecommitdiff
path: root/devtools/shared/heapsnapshot/DeserializedNode.cpp
blob: fac4cccb9efd203f013400a2ec079bff9e12c700 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/* -*-  Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2; -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "mozilla/devtools/DeserializedNode.h"
#include "mozilla/devtools/HeapSnapshot.h"
#include "nsCRTGlue.h"

namespace mozilla {
namespace devtools {

DeserializedEdge::DeserializedEdge(DeserializedEdge&& rhs)
{
  referent = rhs.referent;
  name = rhs.name;
}

DeserializedEdge& DeserializedEdge::operator=(DeserializedEdge&& rhs)
{
  MOZ_ASSERT(&rhs != this);
  this->~DeserializedEdge();
  new(this) DeserializedEdge(Move(rhs));
  return *this;
}

JS::ubi::Node
DeserializedNode::getEdgeReferent(const DeserializedEdge& edge)
{
  auto ptr = owner->nodes.lookup(edge.referent);
  MOZ_ASSERT(ptr);

  // `HashSets` only provide const access to their values, because mutating a
  // value might change its hash, rendering it unfindable in the set.
  // Unfortunately, the `ubi::Node` constructor requires a non-const pointer to
  // its referent.  However, the only aspect of a `DeserializedNode` we hash on
  // is its id, which can't be changed via `ubi::Node`, so this cast can't cause
  // the trouble `HashSet` is concerned a non-const reference would cause.
  return JS::ubi::Node(const_cast<DeserializedNode*>(&*ptr));
}

JS::ubi::StackFrame
DeserializedStackFrame::getParentStackFrame() const
{
  MOZ_ASSERT(parent.isSome());
  auto ptr = owner->frames.lookup(parent.ref());
  MOZ_ASSERT(ptr);
  // See above comment in DeserializedNode::getEdgeReferent about why this
  // const_cast is needed and safe.
  return JS::ubi::StackFrame(const_cast<DeserializedStackFrame*>(&*ptr));
}

} // namespace devtools
} // namespace mozilla

namespace JS {
namespace ubi {

using mozilla::devtools::DeserializedEdge;

const char16_t Concrete<DeserializedNode>::concreteTypeName[] =
  u"mozilla::devtools::DeserializedNode";

const char16_t*
Concrete<DeserializedNode>::typeName() const
{
  return get().typeName;
}

Node::Size
Concrete<DeserializedNode>::size(mozilla::MallocSizeOf mallocSizeof) const
{
  return get().size;
}

class DeserializedEdgeRange : public EdgeRange
{
  DeserializedNode* node;
  Edge              currentEdge;
  size_t            i;

  void settle() {
    if (i >= node->edges.length()) {
      front_ = nullptr;
      return;
    }

    auto& edge = node->edges[i];
    auto referent = node->getEdgeReferent(edge);
    currentEdge = mozilla::Move(Edge(edge.name ? NS_strdup(edge.name) : nullptr,
                                     referent));
    front_ = &currentEdge;
  }

public:
  explicit DeserializedEdgeRange(DeserializedNode& node)
    : node(&node)
    , i(0)
  {
    settle();
  }

  void popFront() override
  {
    i++;
    settle();
  }
};

StackFrame
Concrete<DeserializedNode>::allocationStack() const
{
  MOZ_ASSERT(hasAllocationStack());
  auto id = get().allocationStack.ref();
  auto ptr = get().owner->frames.lookup(id);
  MOZ_ASSERT(ptr);
  // See above comment in DeserializedNode::getEdgeReferent about why this
  // const_cast is needed and safe.
  return JS::ubi::StackFrame(const_cast<DeserializedStackFrame*>(&*ptr));
}


js::UniquePtr<EdgeRange>
Concrete<DeserializedNode>::edges(JSContext* cx, bool) const
{
  js::UniquePtr<DeserializedEdgeRange> range(js_new<DeserializedEdgeRange>(get()));

  if (!range)
    return nullptr;

  return js::UniquePtr<EdgeRange>(range.release());
}

StackFrame
ConcreteStackFrame<DeserializedStackFrame>::parent() const
{
  return get().parent.isNothing() ? StackFrame() : get().getParentStackFrame();
}

bool
ConcreteStackFrame<DeserializedStackFrame>::constructSavedFrameStack(
  JSContext* cx,
  MutableHandleObject outSavedFrameStack) const
{
  StackFrame f(&get());
  return ConstructSavedFrameStackSlow(cx, f, outSavedFrameStack);
}

} // namespace ubi
} // namespace JS