forked from facebook/hermes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHandleRootOwner.cpp
More file actions
169 lines (140 loc) · 4.96 KB
/
HandleRootOwner.cpp
File metadata and controls
169 lines (140 loc) · 4.96 KB
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#include "hermes/VM/HandleRootOwner.h"
#include "hermes/Support/CheckedMalloc.h"
#include "hermes/VM/GC.h"
#include "hermes/VM/HandleRootOwner-inline.h"
namespace hermes {
namespace vm {
//===----------------------------------------------------------------------===//
// class HandleRootOwner
const PinnedHermesValue HandleRootOwner::nullPointer_{
HermesValue::encodeNullptrObjectValue()};
const PinnedHermesValue HandleRootOwner::undefinedValue_{
HermesValue::encodeUndefinedValue()};
const PinnedHermesValue HandleRootOwner::nullValue_{
HermesValue::encodeNullValue()};
const PinnedHermesValue HandleRootOwner::emptyValue_{
HermesValue::encodeEmptyValue()};
const PinnedHermesValue HandleRootOwner::trueValue_{
HermesValue::encodeBoolValue(true)};
const PinnedHermesValue HandleRootOwner::falseValue_{
HermesValue::encodeBoolValue(false)};
const PinnedHermesValue HandleRootOwner::zeroValue_{HVConstants::kZero};
const PinnedHermesValue HandleRootOwner::oneValue_{HVConstants::kOne};
const PinnedHermesValue HandleRootOwner::negOneValue_{HVConstants::kNegOne};
void HandleRootOwner::markGCScopes(SlotAcceptor &acceptor) {
for (GCScope *gcScope = topGCScope_; gcScope; gcScope = gcScope->prevScope_)
gcScope->mark(acceptor);
}
//===----------------------------------------------------------------------===//
// class GCScope
GCScope::~GCScope() {
// Pop ourselves from the scope list.
runtime_->topGCScope_ = prevScope_;
// Free the dynamically allocated chunks, which are all chunks except the
// first one.
auto it = chunks_.begin();
auto e = chunks_.end();
assert(it != e && "must have at least one chunk");
++it; // Skip the first chunk
// Invalidate all the values outside of the chunk in case they are used.
invalidateFreedHandleValues(0, chunks_[0]);
for (; it != e; ++it)
::free(*it); // The chunk was allocated with malloc()
#ifdef HERMESVM_DEBUG_TRACK_GCSCOPE_HANDLES
gcScopeHandleTracker.record(name_, maxAllocatedHandles_);
#endif
}
PinnedHermesValue *GCScope::_newChunkAndPHV(HermesValue value) {
assert(next_ == curChunkEnd_ && "current chunk is not exhaused");
// Move to the next chunk.
++curChunkIndex_;
// Do we need to allocate a new chunk?
if (curChunkIndex_ == chunks_.size()) {
// Allocate memory with malloc() to prevent initialization.
void *mem = checkedMalloc2(CHUNK_SIZE, sizeof(PinnedHermesValue));
chunks_.push_back(static_cast<PinnedHermesValue *>(mem));
// Initialize the new chunk.
next_ = chunks_.back();
} else {
// Point to the start of the next chunk.
next_ = chunks_[curChunkIndex_];
}
curChunkEnd_ = next_ + CHUNK_SIZE;
/// Initialize the new handle with the specified value and return.
return new (next_++) PinnedHermesValue(value);
}
void GCScope::mark(SlotAcceptor &acceptor) {
for (auto it = chunks_.begin(), e = it + curChunkIndex_ + 1; it != e; ++it) {
PinnedHermesValue *first = *it;
PinnedHermesValue *last = *it + CHUNK_SIZE;
// If this is the current chunk, it is not yet full.
if (curChunkEnd_ == last)
last = next_;
// Mark the handles.
for (; first != last; ++first)
acceptor.accept(*first);
}
}
#ifdef HERMES_SLOW_DEBUG
void GCScope::invalidateFreedHandleValues(
unsigned chunkStart,
PinnedHermesValue *valueStart) {
std::fill(
valueStart,
chunks_[chunkStart] + CHUNK_SIZE,
HermesValue::encodeInvalidValue());
for (auto i = chunkStart + 1; i < curChunkIndex_; ++i) {
std::fill(
chunks_[i], chunks_[i] + CHUNK_SIZE, HermesValue::encodeInvalidValue());
}
}
#endif
#ifdef HERMESVM_DEBUG_TRACK_GCSCOPE_HANDLES
GCScopeHandleTracker gcScopeHandleTracker;
GCScopeHandleTracker::~GCScopeHandleTracker() {
{
// Sort by number of handles.
std::vector<CountMapT::value_type> histogram(
countMap_.begin(), countMap_.end());
std::sort(
histogram.begin(),
histogram.end(),
[](const CountMapT::value_type &a, const CountMapT::value_type &b) {
return a.first < b.first;
});
fprintf(stderr, "GCScope\n");
fprintf(stderr, "HCount\tInstances\n");
for (auto &p : histogram) {
fprintf(
stderr,
"%-5u\t%-5u\t%s\n",
p.first,
p.second.first,
p.second.second ? p.second.second : "");
}
}
{
// Sort by name.
std::vector<NameMapT::value_type> byName(nameMap_.begin(), nameMap_.end());
std::sort(
byName.begin(),
byName.end(),
[](const NameMapT::value_type &a, const NameMapT::value_type &b) {
return ::strcmp(a.first, b.first) < 0;
});
fprintf(stderr, "\nGCScope\n");
fprintf(stderr, "Name\tMax HCount\n");
for (auto &p : byName) {
fprintf(stderr, "%s\t%u\n", p.first, p.second);
}
}
}
#endif
} // namespace vm
} // namespace hermes