Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1 | // Copyright 2009 the V8 project authors. All rights reserved. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5 | #include "src/global-handles.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7 | #include "src/api.h" |
| 8 | #include "src/v8.h" |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9 | #include "src/vm-state-inl.h" |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 10 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11 | namespace v8 { |
| 12 | namespace internal { |
| 13 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 14 | |
| 15 | ObjectGroup::~ObjectGroup() { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 16 | if (info != NULL) info->Dispose(); |
| 17 | delete[] objects; |
| 18 | } |
| 19 | |
| 20 | |
| 21 | ImplicitRefGroup::~ImplicitRefGroup() { |
| 22 | delete[] children; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 23 | } |
| 24 | |
| 25 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 26 | class GlobalHandles::Node { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 27 | public: |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 28 | // State transition diagram: |
| 29 | // FREE -> NORMAL <-> WEAK -> PENDING -> NEAR_DEATH -> { NORMAL, WEAK, FREE } |
| 30 | enum State { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 31 | FREE = 0, |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 32 | NORMAL, // Normal global handle. |
| 33 | WEAK, // Flagged as weak but not yet finalized. |
| 34 | PENDING, // Has been recognized as only reachable by weak handles. |
| 35 | NEAR_DEATH, // Callback has informed the handle is near death. |
| 36 | NUMBER_OF_NODE_STATES |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 37 | }; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 38 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 39 | // Maps handle location (slot) to the containing node. |
| 40 | static Node* FromLocation(Object** location) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 41 | DCHECK(offsetof(Node, object_) == 0); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 42 | return reinterpret_cast<Node*>(location); |
| 43 | } |
| 44 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 45 | Node() { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 46 | DCHECK(offsetof(Node, class_id_) == Internals::kNodeClassIdOffset); |
| 47 | DCHECK(offsetof(Node, flags_) == Internals::kNodeFlagsOffset); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 48 | STATIC_ASSERT(static_cast<int>(NodeState::kMask) == |
| 49 | Internals::kNodeStateMask); |
| 50 | STATIC_ASSERT(WEAK == Internals::kNodeStateIsWeakValue); |
| 51 | STATIC_ASSERT(PENDING == Internals::kNodeStateIsPendingValue); |
| 52 | STATIC_ASSERT(NEAR_DEATH == Internals::kNodeStateIsNearDeathValue); |
| 53 | STATIC_ASSERT(static_cast<int>(IsIndependent::kShift) == |
| 54 | Internals::kNodeIsIndependentShift); |
| 55 | STATIC_ASSERT(static_cast<int>(IsPartiallyDependent::kShift) == |
| 56 | Internals::kNodeIsPartiallyDependentShift); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 57 | STATIC_ASSERT(static_cast<int>(IsActive::kShift) == |
| 58 | Internals::kNodeIsActiveShift); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 59 | } |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 60 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 61 | #ifdef ENABLE_HANDLE_ZAPPING |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 62 | ~Node() { |
| 63 | // TODO(1428): if it's a weak handle we should have invoked its callback. |
| 64 | // Zap the values for eager trapping. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 65 | object_ = reinterpret_cast<Object*>(kGlobalHandleZapValue); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 66 | class_id_ = v8::HeapProfiler::kPersistentHandleNoClassId; |
| 67 | index_ = 0; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 68 | set_independent(false); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 69 | if (FLAG_scavenge_reclaim_unmodified_objects) { |
| 70 | set_active(false); |
| 71 | } else { |
| 72 | set_partially_dependent(false); |
| 73 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 74 | set_in_new_space_list(false); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 75 | parameter_or_next_free_.next_free = NULL; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 76 | weak_callback_ = NULL; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 77 | } |
| 78 | #endif |
| 79 | |
| 80 | void Initialize(int index, Node** first_free) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 81 | object_ = reinterpret_cast<Object*>(kGlobalHandleZapValue); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 82 | index_ = static_cast<uint8_t>(index); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 83 | DCHECK(static_cast<int>(index_) == index); |
| 84 | set_state(FREE); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 85 | set_weakness_type(NORMAL_WEAK); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 86 | set_in_new_space_list(false); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 87 | parameter_or_next_free_.next_free = *first_free; |
| 88 | *first_free = this; |
| 89 | } |
| 90 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 91 | void Acquire(Object* object) { |
| 92 | DCHECK(state() == FREE); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 93 | object_ = object; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 94 | class_id_ = v8::HeapProfiler::kPersistentHandleNoClassId; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 95 | set_independent(false); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 96 | if (FLAG_scavenge_reclaim_unmodified_objects) { |
| 97 | set_active(false); |
| 98 | } else { |
| 99 | set_partially_dependent(false); |
| 100 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 101 | set_state(NORMAL); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 102 | parameter_or_next_free_.parameter = NULL; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 103 | weak_callback_ = NULL; |
| 104 | IncreaseBlockUses(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 105 | } |
| 106 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 107 | void Zap() { |
| 108 | DCHECK(IsInUse()); |
| 109 | // Zap the values for eager trapping. |
| 110 | object_ = reinterpret_cast<Object*>(kGlobalHandleZapValue); |
| 111 | } |
| 112 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 113 | void Release() { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 114 | DCHECK(IsInUse()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 115 | set_state(FREE); |
| 116 | // Zap the values for eager trapping. |
| 117 | object_ = reinterpret_cast<Object*>(kGlobalHandleZapValue); |
| 118 | class_id_ = v8::HeapProfiler::kPersistentHandleNoClassId; |
| 119 | set_independent(false); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 120 | if (FLAG_scavenge_reclaim_unmodified_objects) { |
| 121 | set_active(false); |
| 122 | } else { |
| 123 | set_partially_dependent(false); |
| 124 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 125 | weak_callback_ = NULL; |
| 126 | DecreaseBlockUses(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 127 | } |
| 128 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 129 | // Object slot accessors. |
| 130 | Object* object() const { return object_; } |
| 131 | Object** location() { return &object_; } |
| 132 | Handle<Object> handle() { return Handle<Object>(location()); } |
| 133 | |
| 134 | // Wrapper class ID accessors. |
| 135 | bool has_wrapper_class_id() const { |
| 136 | return class_id_ != v8::HeapProfiler::kPersistentHandleNoClassId; |
| 137 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 138 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 139 | uint16_t wrapper_class_id() const { return class_id_; } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 140 | |
| 141 | // State and flag accessors. |
| 142 | |
| 143 | State state() const { |
| 144 | return NodeState::decode(flags_); |
| 145 | } |
| 146 | void set_state(State state) { |
| 147 | flags_ = NodeState::update(flags_, state); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 148 | } |
| 149 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 150 | bool is_independent() { |
| 151 | return IsIndependent::decode(flags_); |
| 152 | } |
| 153 | void set_independent(bool v) { |
| 154 | flags_ = IsIndependent::update(flags_, v); |
| 155 | } |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 156 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 157 | bool is_partially_dependent() { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 158 | CHECK(!FLAG_scavenge_reclaim_unmodified_objects); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 159 | return IsPartiallyDependent::decode(flags_); |
| 160 | } |
| 161 | void set_partially_dependent(bool v) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 162 | CHECK(!FLAG_scavenge_reclaim_unmodified_objects); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 163 | flags_ = IsPartiallyDependent::update(flags_, v); |
| 164 | } |
| 165 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 166 | bool is_active() { |
| 167 | CHECK(FLAG_scavenge_reclaim_unmodified_objects); |
| 168 | return IsActive::decode(flags_); |
| 169 | } |
| 170 | void set_active(bool v) { |
| 171 | CHECK(FLAG_scavenge_reclaim_unmodified_objects); |
| 172 | flags_ = IsActive::update(flags_, v); |
| 173 | } |
| 174 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 175 | bool is_in_new_space_list() { |
| 176 | return IsInNewSpaceList::decode(flags_); |
| 177 | } |
| 178 | void set_in_new_space_list(bool v) { |
| 179 | flags_ = IsInNewSpaceList::update(flags_, v); |
| 180 | } |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 181 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 182 | WeaknessType weakness_type() const { |
| 183 | return NodeWeaknessType::decode(flags_); |
| 184 | } |
| 185 | void set_weakness_type(WeaknessType weakness_type) { |
| 186 | flags_ = NodeWeaknessType::update(flags_, weakness_type); |
| 187 | } |
| 188 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 189 | bool IsNearDeath() const { |
| 190 | // Check for PENDING to ensure correct answer when processing callbacks. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 191 | return state() == PENDING || state() == NEAR_DEATH; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 192 | } |
| 193 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 194 | bool IsWeak() const { return state() == WEAK; } |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 195 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 196 | bool IsInUse() const { return state() != FREE; } |
| 197 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 198 | bool IsRetainer() const { |
| 199 | return state() != FREE && |
| 200 | !(state() == NEAR_DEATH && weakness_type() != NORMAL_WEAK); |
| 201 | } |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 202 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 203 | bool IsStrongRetainer() const { return state() == NORMAL; } |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 204 | |
| 205 | bool IsWeakRetainer() const { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 206 | return state() == WEAK || state() == PENDING || |
| 207 | (state() == NEAR_DEATH && weakness_type() == NORMAL_WEAK); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | void MarkPending() { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 211 | DCHECK(state() == WEAK); |
| 212 | set_state(PENDING); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | // Independent flag accessors. |
| 216 | void MarkIndependent() { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 217 | DCHECK(IsInUse()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 218 | set_independent(true); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 219 | } |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 220 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 221 | void MarkPartiallyDependent() { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 222 | DCHECK(IsInUse()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 223 | if (GetGlobalHandles()->isolate()->heap()->InNewSpace(object_)) { |
| 224 | set_partially_dependent(true); |
| 225 | } |
| 226 | } |
| 227 | void clear_partially_dependent() { set_partially_dependent(false); } |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 228 | |
| 229 | // Callback accessor. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 230 | // TODO(svenpanne) Re-enable or nuke later. |
| 231 | // WeakReferenceCallback callback() { return callback_; } |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 232 | |
| 233 | // Callback parameter accessors. |
| 234 | void set_parameter(void* parameter) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 235 | DCHECK(IsInUse()); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 236 | parameter_or_next_free_.parameter = parameter; |
| 237 | } |
| 238 | void* parameter() const { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 239 | DCHECK(IsInUse()); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 240 | return parameter_or_next_free_.parameter; |
| 241 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 242 | |
| 243 | // Accessors for next free node in the free list. |
| 244 | Node* next_free() { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 245 | DCHECK(state() == FREE); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 246 | return parameter_or_next_free_.next_free; |
| 247 | } |
| 248 | void set_next_free(Node* value) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 249 | DCHECK(state() == FREE); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 250 | parameter_or_next_free_.next_free = value; |
| 251 | } |
| 252 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 253 | void MakeWeak(void* parameter, WeakCallback weak_callback) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 254 | DCHECK(weak_callback != nullptr); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 255 | DCHECK(IsInUse()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 256 | CHECK_NE(object_, reinterpret_cast<Object*>(kGlobalHandleZapValue)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 257 | set_state(WEAK); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 258 | set_weakness_type(NORMAL_WEAK); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 259 | set_parameter(parameter); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 260 | weak_callback_ = weak_callback; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 261 | } |
| 262 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 263 | void MakeWeak(void* parameter, |
| 264 | WeakCallbackInfo<void>::Callback phantom_callback, |
| 265 | v8::WeakCallbackType type) { |
| 266 | DCHECK(phantom_callback != nullptr); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 267 | DCHECK(IsInUse()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 268 | CHECK_NE(object_, reinterpret_cast<Object*>(kGlobalHandleZapValue)); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 269 | set_state(WEAK); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 270 | switch (type) { |
| 271 | case v8::WeakCallbackType::kParameter: |
| 272 | set_weakness_type(PHANTOM_WEAK); |
| 273 | break; |
| 274 | case v8::WeakCallbackType::kInternalFields: |
| 275 | set_weakness_type(PHANTOM_WEAK_2_INTERNAL_FIELDS); |
| 276 | break; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 277 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 278 | set_parameter(parameter); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 279 | weak_callback_ = reinterpret_cast<WeakCallback>(phantom_callback); |
| 280 | } |
| 281 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 282 | void* ClearWeakness() { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 283 | DCHECK(IsInUse()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 284 | void* p = parameter(); |
| 285 | set_state(NORMAL); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 286 | set_parameter(NULL); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 287 | return p; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 288 | } |
| 289 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 290 | void CollectPhantomCallbackData( |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 291 | Isolate* isolate, |
| 292 | List<PendingPhantomCallback>* pending_phantom_callbacks) { |
| 293 | DCHECK(weakness_type() == PHANTOM_WEAK || |
| 294 | weakness_type() == PHANTOM_WEAK_2_INTERNAL_FIELDS); |
| 295 | DCHECK(state() == PENDING); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 296 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 297 | void* internal_fields[v8::kInternalFieldsInWeakCallback] = {nullptr, |
| 298 | nullptr}; |
| 299 | if (weakness_type() != PHANTOM_WEAK && object()->IsJSObject()) { |
| 300 | auto jsobject = JSObject::cast(object()); |
| 301 | int field_count = jsobject->GetInternalFieldCount(); |
| 302 | for (int i = 0; i < v8::kInternalFieldsInWeakCallback; ++i) { |
| 303 | if (field_count == i) break; |
| 304 | auto field = jsobject->GetInternalField(i); |
| 305 | if (field->IsSmi()) internal_fields[i] = field; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 306 | } |
| 307 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 308 | |
| 309 | // Zap with something dangerous. |
| 310 | *location() = reinterpret_cast<Object*>(0x6057ca11); |
| 311 | |
| 312 | typedef v8::WeakCallbackInfo<void> Data; |
| 313 | auto callback = reinterpret_cast<Data::Callback>(weak_callback_); |
| 314 | pending_phantom_callbacks->Add( |
| 315 | PendingPhantomCallback(this, callback, parameter(), internal_fields)); |
| 316 | DCHECK(IsInUse()); |
| 317 | set_state(NEAR_DEATH); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 318 | } |
| 319 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 320 | bool PostGarbageCollectionProcessing(Isolate* isolate) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 321 | // Handles only weak handles (not phantom) that are dying. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 322 | if (state() != Node::PENDING) return false; |
| 323 | if (weak_callback_ == NULL) { |
| 324 | Release(); |
Kristian Monsen | 50ef84f | 2010-07-29 15:18:00 +0100 | [diff] [blame] | 325 | return false; |
| 326 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 327 | set_state(NEAR_DEATH); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 328 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 329 | // Check that we are not passing a finalized external string to |
| 330 | // the callback. |
| 331 | DCHECK(!object_->IsExternalOneByteString() || |
| 332 | ExternalOneByteString::cast(object_)->resource() != NULL); |
| 333 | DCHECK(!object_->IsExternalTwoByteString() || |
| 334 | ExternalTwoByteString::cast(object_)->resource() != NULL); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 335 | if (weakness_type() != NORMAL_WEAK) return false; |
| 336 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 337 | // Leaving V8. |
| 338 | VMState<EXTERNAL> vmstate(isolate); |
| 339 | HandleScope handle_scope(isolate); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 340 | Object** object = location(); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 341 | Handle<Object> handle(*object, isolate); |
| 342 | v8::WeakCallbackData<v8::Value, void> data( |
| 343 | reinterpret_cast<v8::Isolate*>(isolate), parameter(), |
| 344 | v8::Utils::ToLocal(handle)); |
| 345 | set_parameter(NULL); |
| 346 | weak_callback_(data); |
| 347 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 348 | // Absence of explicit cleanup or revival of weak handle |
Kristian Monsen | 50ef84f | 2010-07-29 15:18:00 +0100 | [diff] [blame] | 349 | // in most of the cases would lead to memory leak. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 350 | CHECK(state() != NEAR_DEATH); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 351 | return true; |
| 352 | } |
| 353 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 354 | inline GlobalHandles* GetGlobalHandles(); |
| 355 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 356 | private: |
| 357 | inline NodeBlock* FindBlock(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 358 | inline void IncreaseBlockUses(); |
| 359 | inline void DecreaseBlockUses(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 360 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 361 | // Storage for object pointer. |
| 362 | // Placed first to avoid offset computation. |
| 363 | Object* object_; |
| 364 | |
| 365 | // Next word stores class_id, index, state, and independent. |
| 366 | // Note: the most aligned fields should go first. |
| 367 | |
| 368 | // Wrapper class ID. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 369 | uint16_t class_id_; |
| 370 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 371 | // Index in the containing handle block. |
| 372 | uint8_t index_; |
| 373 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 374 | // This stores three flags (independent, partially_dependent and |
| 375 | // in_new_space_list) and a State. |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 376 | class NodeState : public BitField<State, 0, 3> {}; |
| 377 | class IsIndependent : public BitField<bool, 3, 1> {}; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 378 | // The following two fields are mutually exclusive |
| 379 | class IsActive : public BitField<bool, 4, 1> {}; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 380 | class IsPartiallyDependent : public BitField<bool, 4, 1> {}; |
| 381 | class IsInNewSpaceList : public BitField<bool, 5, 1> {}; |
| 382 | class NodeWeaknessType : public BitField<WeaknessType, 6, 2> {}; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 383 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 384 | uint8_t flags_; |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 385 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 386 | // Handle specific callback - might be a weak reference in disguise. |
| 387 | WeakCallback weak_callback_; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 388 | |
| 389 | // Provided data for callback. In FREE state, this is used for |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 390 | // the free list link. |
| 391 | union { |
| 392 | void* parameter; |
| 393 | Node* next_free; |
| 394 | } parameter_or_next_free_; |
| 395 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 396 | DISALLOW_COPY_AND_ASSIGN(Node); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 397 | }; |
| 398 | |
| 399 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 400 | class GlobalHandles::NodeBlock { |
| 401 | public: |
| 402 | static const int kSize = 256; |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 403 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 404 | explicit NodeBlock(GlobalHandles* global_handles, NodeBlock* next) |
| 405 | : next_(next), |
| 406 | used_nodes_(0), |
| 407 | next_used_(NULL), |
| 408 | prev_used_(NULL), |
| 409 | global_handles_(global_handles) {} |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 410 | |
| 411 | void PutNodesOnFreeList(Node** first_free) { |
| 412 | for (int i = kSize - 1; i >= 0; --i) { |
| 413 | nodes_[i].Initialize(i, first_free); |
| 414 | } |
| 415 | } |
| 416 | |
| 417 | Node* node_at(int index) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 418 | DCHECK(0 <= index && index < kSize); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 419 | return &nodes_[index]; |
| 420 | } |
| 421 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 422 | void IncreaseUses() { |
| 423 | DCHECK(used_nodes_ < kSize); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 424 | if (used_nodes_++ == 0) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 425 | NodeBlock* old_first = global_handles_->first_used_block_; |
| 426 | global_handles_->first_used_block_ = this; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 427 | next_used_ = old_first; |
| 428 | prev_used_ = NULL; |
| 429 | if (old_first == NULL) return; |
| 430 | old_first->prev_used_ = this; |
| 431 | } |
| 432 | } |
| 433 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 434 | void DecreaseUses() { |
| 435 | DCHECK(used_nodes_ > 0); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 436 | if (--used_nodes_ == 0) { |
| 437 | if (next_used_ != NULL) next_used_->prev_used_ = prev_used_; |
| 438 | if (prev_used_ != NULL) prev_used_->next_used_ = next_used_; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 439 | if (this == global_handles_->first_used_block_) { |
| 440 | global_handles_->first_used_block_ = next_used_; |
Ben Murdoch | bb769b2 | 2010-08-11 14:56:33 +0100 | [diff] [blame] | 441 | } |
| 442 | } |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 443 | } |
Ben Murdoch | bb769b2 | 2010-08-11 14:56:33 +0100 | [diff] [blame] | 444 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 445 | GlobalHandles* global_handles() { return global_handles_; } |
| 446 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 447 | // Next block in the list of all blocks. |
| 448 | NodeBlock* next() const { return next_; } |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 449 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 450 | // Next/previous block in the list of blocks with used nodes. |
| 451 | NodeBlock* next_used() const { return next_used_; } |
| 452 | NodeBlock* prev_used() const { return prev_used_; } |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 453 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 454 | private: |
| 455 | Node nodes_[kSize]; |
| 456 | NodeBlock* const next_; |
| 457 | int used_nodes_; |
| 458 | NodeBlock* next_used_; |
| 459 | NodeBlock* prev_used_; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 460 | GlobalHandles* global_handles_; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 461 | }; |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 462 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 463 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 464 | GlobalHandles* GlobalHandles::Node::GetGlobalHandles() { |
| 465 | return FindBlock()->global_handles(); |
| 466 | } |
| 467 | |
| 468 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 469 | GlobalHandles::NodeBlock* GlobalHandles::Node::FindBlock() { |
| 470 | intptr_t ptr = reinterpret_cast<intptr_t>(this); |
| 471 | ptr = ptr - index_ * sizeof(Node); |
| 472 | NodeBlock* block = reinterpret_cast<NodeBlock*>(ptr); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 473 | DCHECK(block->node_at(index_) == this); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 474 | return block; |
| 475 | } |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 476 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 477 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 478 | void GlobalHandles::Node::IncreaseBlockUses() { |
| 479 | NodeBlock* node_block = FindBlock(); |
| 480 | node_block->IncreaseUses(); |
| 481 | GlobalHandles* global_handles = node_block->global_handles(); |
| 482 | global_handles->isolate()->counters()->global_handles()->Increment(); |
| 483 | global_handles->number_of_global_handles_++; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 484 | } |
| 485 | |
| 486 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 487 | void GlobalHandles::Node::DecreaseBlockUses() { |
| 488 | NodeBlock* node_block = FindBlock(); |
| 489 | GlobalHandles* global_handles = node_block->global_handles(); |
| 490 | parameter_or_next_free_.next_free = global_handles->first_free_; |
| 491 | global_handles->first_free_ = this; |
| 492 | node_block->DecreaseUses(); |
| 493 | global_handles->isolate()->counters()->global_handles()->Decrement(); |
| 494 | global_handles->number_of_global_handles_--; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 495 | } |
| 496 | |
| 497 | |
| 498 | class GlobalHandles::NodeIterator { |
| 499 | public: |
| 500 | explicit NodeIterator(GlobalHandles* global_handles) |
| 501 | : block_(global_handles->first_used_block_), |
| 502 | index_(0) {} |
| 503 | |
| 504 | bool done() const { return block_ == NULL; } |
| 505 | |
| 506 | Node* node() const { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 507 | DCHECK(!done()); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 508 | return block_->node_at(index_); |
| 509 | } |
| 510 | |
| 511 | void Advance() { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 512 | DCHECK(!done()); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 513 | if (++index_ < NodeBlock::kSize) return; |
| 514 | index_ = 0; |
| 515 | block_ = block_->next_used(); |
| 516 | } |
| 517 | |
| 518 | private: |
| 519 | NodeBlock* block_; |
| 520 | int index_; |
| 521 | |
| 522 | DISALLOW_COPY_AND_ASSIGN(NodeIterator); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 523 | }; |
| 524 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 525 | class GlobalHandles::PendingPhantomCallbacksSecondPassTask |
| 526 | : public v8::internal::CancelableTask { |
| 527 | public: |
| 528 | // Takes ownership of the contents of pending_phantom_callbacks, leaving it in |
| 529 | // the same state it would be after a call to Clear(). |
| 530 | PendingPhantomCallbacksSecondPassTask( |
| 531 | List<PendingPhantomCallback>* pending_phantom_callbacks, Isolate* isolate) |
| 532 | : CancelableTask(isolate) { |
| 533 | pending_phantom_callbacks_.Swap(pending_phantom_callbacks); |
| 534 | } |
| 535 | |
| 536 | void RunInternal() override { |
| 537 | isolate()->heap()->CallGCPrologueCallbacks( |
| 538 | GCType::kGCTypeProcessWeakCallbacks, kNoGCCallbackFlags); |
| 539 | InvokeSecondPassPhantomCallbacks(&pending_phantom_callbacks_, isolate()); |
| 540 | isolate()->heap()->CallGCEpilogueCallbacks( |
| 541 | GCType::kGCTypeProcessWeakCallbacks, kNoGCCallbackFlags); |
| 542 | } |
| 543 | |
| 544 | private: |
| 545 | List<PendingPhantomCallback> pending_phantom_callbacks_; |
| 546 | |
| 547 | DISALLOW_COPY_AND_ASSIGN(PendingPhantomCallbacksSecondPassTask); |
| 548 | }; |
| 549 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 550 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 551 | GlobalHandles::GlobalHandles(Isolate* isolate) |
| 552 | : isolate_(isolate), |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 553 | number_of_global_handles_(0), |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 554 | first_block_(NULL), |
| 555 | first_used_block_(NULL), |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 556 | first_free_(NULL), |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 557 | post_gc_processing_count_(0), |
| 558 | object_group_connections_(kObjectGroupConnectionsCapacity) {} |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 559 | |
| 560 | |
| 561 | GlobalHandles::~GlobalHandles() { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 562 | NodeBlock* block = first_block_; |
| 563 | while (block != NULL) { |
| 564 | NodeBlock* tmp = block->next(); |
| 565 | delete block; |
| 566 | block = tmp; |
| 567 | } |
| 568 | first_block_ = NULL; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 569 | } |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 570 | |
| 571 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 572 | Handle<Object> GlobalHandles::Create(Object* value) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 573 | if (first_free_ == NULL) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 574 | first_block_ = new NodeBlock(this, first_block_); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 575 | first_block_->PutNodesOnFreeList(&first_free_); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 576 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 577 | DCHECK(first_free_ != NULL); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 578 | // Take the first node in the free list. |
| 579 | Node* result = first_free_; |
| 580 | first_free_ = result->next_free(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 581 | result->Acquire(value); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 582 | if (isolate_->heap()->InNewSpace(value) && |
| 583 | !result->is_in_new_space_list()) { |
| 584 | new_space_nodes_.Add(result); |
| 585 | result->set_in_new_space_list(true); |
| 586 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 587 | return result->handle(); |
| 588 | } |
| 589 | |
| 590 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 591 | Handle<Object> GlobalHandles::CopyGlobal(Object** location) { |
| 592 | DCHECK(location != NULL); |
| 593 | return Node::FromLocation(location)->GetGlobalHandles()->Create(*location); |
| 594 | } |
| 595 | |
| 596 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 597 | void GlobalHandles::Destroy(Object** location) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 598 | if (location != NULL) Node::FromLocation(location)->Release(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 599 | } |
| 600 | |
| 601 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 602 | void GlobalHandles::MakeWeak(Object** location, void* parameter, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 603 | WeakCallback weak_callback) { |
| 604 | Node::FromLocation(location)->MakeWeak(parameter, weak_callback); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 605 | } |
| 606 | |
| 607 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 608 | typedef v8::WeakCallbackInfo<void>::Callback GenericCallback; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 609 | |
| 610 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 611 | void GlobalHandles::MakeWeak(Object** location, void* parameter, |
| 612 | GenericCallback phantom_callback, |
| 613 | v8::WeakCallbackType type) { |
| 614 | Node::FromLocation(location)->MakeWeak(parameter, phantom_callback, type); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 615 | } |
| 616 | |
| 617 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 618 | void* GlobalHandles::ClearWeakness(Object** location) { |
| 619 | return Node::FromLocation(location)->ClearWeakness(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 620 | } |
| 621 | |
| 622 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 623 | void GlobalHandles::MarkIndependent(Object** location) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 624 | Node::FromLocation(location)->MarkIndependent(); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 625 | } |
| 626 | |
| 627 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 628 | void GlobalHandles::MarkPartiallyDependent(Object** location) { |
| 629 | Node::FromLocation(location)->MarkPartiallyDependent(); |
| 630 | } |
| 631 | |
| 632 | |
| 633 | bool GlobalHandles::IsIndependent(Object** location) { |
| 634 | return Node::FromLocation(location)->is_independent(); |
| 635 | } |
| 636 | |
| 637 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 638 | bool GlobalHandles::IsNearDeath(Object** location) { |
| 639 | return Node::FromLocation(location)->IsNearDeath(); |
| 640 | } |
| 641 | |
| 642 | |
| 643 | bool GlobalHandles::IsWeak(Object** location) { |
| 644 | return Node::FromLocation(location)->IsWeak(); |
| 645 | } |
| 646 | |
| 647 | |
| 648 | void GlobalHandles::IterateWeakRoots(ObjectVisitor* v) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 649 | for (NodeIterator it(this); !it.done(); it.Advance()) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 650 | Node* node = it.node(); |
| 651 | if (node->IsWeakRetainer()) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 652 | // Pending weak phantom handles die immediately. Everything else survives. |
| 653 | if (node->state() == Node::PENDING && |
| 654 | node->weakness_type() != NORMAL_WEAK) { |
| 655 | node->CollectPhantomCallbackData(isolate(), |
| 656 | &pending_phantom_callbacks_); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 657 | } else { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 658 | v->VisitPointer(node->location()); |
| 659 | } |
| 660 | } |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 661 | } |
| 662 | } |
| 663 | |
| 664 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 665 | void GlobalHandles::IdentifyWeakHandles(WeakSlotCallback f) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 666 | for (NodeIterator it(this); !it.done(); it.Advance()) { |
| 667 | if (it.node()->IsWeak() && f(it.node()->location())) { |
| 668 | it.node()->MarkPending(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 669 | } |
| 670 | } |
| 671 | } |
| 672 | |
| 673 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 674 | void GlobalHandles::IterateNewSpaceStrongAndDependentRoots(ObjectVisitor* v) { |
| 675 | for (int i = 0; i < new_space_nodes_.length(); ++i) { |
| 676 | Node* node = new_space_nodes_[i]; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 677 | if (FLAG_scavenge_reclaim_unmodified_objects) { |
| 678 | if (node->IsStrongRetainer() || |
| 679 | (node->IsWeakRetainer() && !node->is_independent() && |
| 680 | node->is_active())) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 681 | v->VisitPointer(node->location()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 682 | } |
| 683 | } else { |
| 684 | if (node->IsStrongRetainer() || |
| 685 | (node->IsWeakRetainer() && !node->is_independent() && |
| 686 | !node->is_partially_dependent())) { |
| 687 | v->VisitPointer(node->location()); |
| 688 | } |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 689 | } |
| 690 | } |
| 691 | } |
| 692 | |
| 693 | |
| 694 | void GlobalHandles::IdentifyNewSpaceWeakIndependentHandles( |
| 695 | WeakSlotCallbackWithHeap f) { |
| 696 | for (int i = 0; i < new_space_nodes_.length(); ++i) { |
| 697 | Node* node = new_space_nodes_[i]; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 698 | DCHECK(node->is_in_new_space_list()); |
| 699 | if ((node->is_independent() || node->is_partially_dependent()) && |
| 700 | node->IsWeak() && f(isolate_->heap(), node->location())) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 701 | node->MarkPending(); |
| 702 | } |
| 703 | } |
| 704 | } |
| 705 | |
| 706 | |
| 707 | void GlobalHandles::IterateNewSpaceWeakIndependentRoots(ObjectVisitor* v) { |
| 708 | for (int i = 0; i < new_space_nodes_.length(); ++i) { |
| 709 | Node* node = new_space_nodes_[i]; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 710 | DCHECK(node->is_in_new_space_list()); |
| 711 | if ((node->is_independent() || node->is_partially_dependent()) && |
| 712 | node->IsWeakRetainer()) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 713 | // Pending weak phantom handles die immediately. Everything else survives. |
| 714 | if (node->state() == Node::PENDING && |
| 715 | node->weakness_type() != NORMAL_WEAK) { |
| 716 | node->CollectPhantomCallbackData(isolate(), |
| 717 | &pending_phantom_callbacks_); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 718 | } else { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 719 | v->VisitPointer(node->location()); |
| 720 | } |
| 721 | } |
| 722 | } |
| 723 | } |
| 724 | |
| 725 | |
| 726 | void GlobalHandles::IdentifyWeakUnmodifiedObjects( |
| 727 | WeakSlotCallback is_unmodified) { |
| 728 | for (int i = 0; i < new_space_nodes_.length(); ++i) { |
| 729 | Node* node = new_space_nodes_[i]; |
| 730 | if (node->IsWeak() && !is_unmodified(node->location())) { |
| 731 | node->set_active(true); |
| 732 | } |
| 733 | } |
| 734 | } |
| 735 | |
| 736 | |
| 737 | void GlobalHandles::MarkNewSpaceWeakUnmodifiedObjectsPending( |
| 738 | WeakSlotCallbackWithHeap is_unscavenged) { |
| 739 | for (int i = 0; i < new_space_nodes_.length(); ++i) { |
| 740 | Node* node = new_space_nodes_[i]; |
| 741 | DCHECK(node->is_in_new_space_list()); |
| 742 | if ((node->is_independent() || !node->is_active()) && node->IsWeak() && |
| 743 | is_unscavenged(isolate_->heap(), node->location())) { |
| 744 | node->MarkPending(); |
| 745 | } |
| 746 | } |
| 747 | } |
| 748 | |
| 749 | |
| 750 | void GlobalHandles::IterateNewSpaceWeakUnmodifiedRoots(ObjectVisitor* v) { |
| 751 | for (int i = 0; i < new_space_nodes_.length(); ++i) { |
| 752 | Node* node = new_space_nodes_[i]; |
| 753 | DCHECK(node->is_in_new_space_list()); |
| 754 | if ((node->is_independent() || !node->is_active()) && |
| 755 | node->IsWeakRetainer()) { |
| 756 | // Pending weak phantom handles die immediately. Everything else survives. |
| 757 | if (node->state() == Node::PENDING && |
| 758 | node->weakness_type() != NORMAL_WEAK) { |
| 759 | node->CollectPhantomCallbackData(isolate(), |
| 760 | &pending_phantom_callbacks_); |
| 761 | } else { |
| 762 | v->VisitPointer(node->location()); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 763 | } |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 764 | } |
| 765 | } |
| 766 | } |
| 767 | |
| 768 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 769 | bool GlobalHandles::IterateObjectGroups(ObjectVisitor* v, |
| 770 | WeakSlotCallbackWithHeap can_skip) { |
| 771 | ComputeObjectGroupsAndImplicitReferences(); |
| 772 | int last = 0; |
| 773 | bool any_group_was_visited = false; |
| 774 | for (int i = 0; i < object_groups_.length(); i++) { |
| 775 | ObjectGroup* entry = object_groups_.at(i); |
| 776 | DCHECK(entry != NULL); |
| 777 | |
| 778 | Object*** objects = entry->objects; |
| 779 | bool group_should_be_visited = false; |
| 780 | for (size_t j = 0; j < entry->length; j++) { |
| 781 | Object* object = *objects[j]; |
| 782 | if (object->IsHeapObject()) { |
| 783 | if (!can_skip(isolate_->heap(), &object)) { |
| 784 | group_should_be_visited = true; |
| 785 | break; |
| 786 | } |
| 787 | } |
| 788 | } |
| 789 | |
| 790 | if (!group_should_be_visited) { |
| 791 | object_groups_[last++] = entry; |
| 792 | continue; |
| 793 | } |
| 794 | |
| 795 | // An object in the group requires visiting, so iterate over all |
| 796 | // objects in the group. |
| 797 | for (size_t j = 0; j < entry->length; ++j) { |
| 798 | Object* object = *objects[j]; |
| 799 | if (object->IsHeapObject()) { |
| 800 | v->VisitPointer(&object); |
| 801 | any_group_was_visited = true; |
| 802 | } |
| 803 | } |
| 804 | |
| 805 | // Once the entire group has been iterated over, set the object |
| 806 | // group to NULL so it won't be processed again. |
| 807 | delete entry; |
| 808 | object_groups_.at(i) = NULL; |
| 809 | } |
| 810 | object_groups_.Rewind(last); |
| 811 | return any_group_was_visited; |
| 812 | } |
| 813 | |
| 814 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 815 | void GlobalHandles::InvokeSecondPassPhantomCallbacks( |
| 816 | List<PendingPhantomCallback>* callbacks, Isolate* isolate) { |
| 817 | while (callbacks->length() != 0) { |
| 818 | auto callback = callbacks->RemoveLast(); |
| 819 | DCHECK(callback.node() == nullptr); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 820 | // Fire second pass callback |
| 821 | callback.Invoke(isolate); |
| 822 | } |
| 823 | } |
| 824 | |
| 825 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 826 | int GlobalHandles::PostScavengeProcessing( |
| 827 | const int initial_post_gc_processing_count) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 828 | int freed_nodes = 0; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 829 | for (int i = 0; i < new_space_nodes_.length(); ++i) { |
| 830 | Node* node = new_space_nodes_[i]; |
| 831 | DCHECK(node->is_in_new_space_list()); |
| 832 | if (!node->IsRetainer()) { |
| 833 | // Free nodes do not have weak callbacks. Do not use them to compute |
| 834 | // the freed_nodes. |
| 835 | continue; |
| 836 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 837 | // Skip dependent or unmodified handles. Their weak callbacks might expect |
| 838 | // to be |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 839 | // called between two global garbage collection callbacks which |
| 840 | // are not called for minor collections. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 841 | if (FLAG_scavenge_reclaim_unmodified_objects) { |
| 842 | if (!node->is_independent() && (node->is_active())) { |
| 843 | node->set_active(false); |
| 844 | continue; |
| 845 | } |
| 846 | node->set_active(false); |
| 847 | } else { |
| 848 | if (!node->is_independent() && !node->is_partially_dependent()) { |
| 849 | continue; |
| 850 | } |
| 851 | node->clear_partially_dependent(); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 852 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 853 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 854 | if (node->PostGarbageCollectionProcessing(isolate_)) { |
| 855 | if (initial_post_gc_processing_count != post_gc_processing_count_) { |
| 856 | // Weak callback triggered another GC and another round of |
| 857 | // PostGarbageCollection processing. The current node might |
| 858 | // have been deleted in that round, so we need to bail out (or |
| 859 | // restart the processing). |
| 860 | return freed_nodes; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 861 | } |
| 862 | } |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 863 | if (!node->IsRetainer()) { |
| 864 | freed_nodes++; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 865 | } |
| 866 | } |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 867 | return freed_nodes; |
| 868 | } |
| 869 | |
| 870 | |
| 871 | int GlobalHandles::PostMarkSweepProcessing( |
| 872 | const int initial_post_gc_processing_count) { |
| 873 | int freed_nodes = 0; |
| 874 | for (NodeIterator it(this); !it.done(); it.Advance()) { |
| 875 | if (!it.node()->IsRetainer()) { |
| 876 | // Free nodes do not have weak callbacks. Do not use them to compute |
| 877 | // the freed_nodes. |
| 878 | continue; |
| 879 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 880 | if (FLAG_scavenge_reclaim_unmodified_objects) { |
| 881 | it.node()->set_active(false); |
| 882 | } else { |
| 883 | it.node()->clear_partially_dependent(); |
| 884 | } |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 885 | if (it.node()->PostGarbageCollectionProcessing(isolate_)) { |
| 886 | if (initial_post_gc_processing_count != post_gc_processing_count_) { |
| 887 | // See the comment above. |
| 888 | return freed_nodes; |
| 889 | } |
| 890 | } |
| 891 | if (!it.node()->IsRetainer()) { |
| 892 | freed_nodes++; |
| 893 | } |
| 894 | } |
| 895 | return freed_nodes; |
| 896 | } |
| 897 | |
| 898 | |
| 899 | void GlobalHandles::UpdateListOfNewSpaceNodes() { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 900 | int last = 0; |
| 901 | for (int i = 0; i < new_space_nodes_.length(); ++i) { |
| 902 | Node* node = new_space_nodes_[i]; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 903 | DCHECK(node->is_in_new_space_list()); |
| 904 | if (node->IsRetainer()) { |
| 905 | if (isolate_->heap()->InNewSpace(node->object())) { |
| 906 | new_space_nodes_[last++] = node; |
| 907 | isolate_->heap()->IncrementNodesCopiedInNewSpace(); |
| 908 | } else { |
| 909 | node->set_in_new_space_list(false); |
| 910 | isolate_->heap()->IncrementNodesPromoted(); |
| 911 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 912 | } else { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 913 | node->set_in_new_space_list(false); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 914 | isolate_->heap()->IncrementNodesDiedInNewSpace(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 915 | } |
| 916 | } |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 917 | new_space_nodes_.Rewind(last); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 918 | new_space_nodes_.Trim(); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 919 | } |
| 920 | |
| 921 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 922 | int GlobalHandles::DispatchPendingPhantomCallbacks( |
| 923 | bool synchronous_second_pass) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 924 | int freed_nodes = 0; |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 925 | List<PendingPhantomCallback> second_pass_callbacks; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 926 | { |
| 927 | // The initial pass callbacks must simply clear the nodes. |
| 928 | for (auto i = pending_phantom_callbacks_.begin(); |
| 929 | i != pending_phantom_callbacks_.end(); ++i) { |
| 930 | auto callback = i; |
| 931 | // Skip callbacks that have already been processed once. |
| 932 | if (callback->node() == nullptr) continue; |
| 933 | callback->Invoke(isolate()); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 934 | if (callback->callback()) second_pass_callbacks.Add(*callback); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 935 | freed_nodes++; |
| 936 | } |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 937 | } |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 938 | pending_phantom_callbacks_.Clear(); |
| 939 | if (second_pass_callbacks.length() > 0) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 940 | if (FLAG_optimize_for_size || FLAG_predictable || synchronous_second_pass) { |
| 941 | isolate()->heap()->CallGCPrologueCallbacks( |
| 942 | GCType::kGCTypeProcessWeakCallbacks, kNoGCCallbackFlags); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 943 | InvokeSecondPassPhantomCallbacks(&second_pass_callbacks, isolate()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 944 | isolate()->heap()->CallGCEpilogueCallbacks( |
| 945 | GCType::kGCTypeProcessWeakCallbacks, kNoGCCallbackFlags); |
| 946 | } else { |
| 947 | auto task = new PendingPhantomCallbacksSecondPassTask( |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 948 | &second_pass_callbacks, isolate()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 949 | V8::GetCurrentPlatform()->CallOnForegroundThread( |
| 950 | reinterpret_cast<v8::Isolate*>(isolate()), task); |
| 951 | } |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 952 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 953 | return freed_nodes; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 954 | } |
| 955 | |
| 956 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 957 | void GlobalHandles::PendingPhantomCallback::Invoke(Isolate* isolate) { |
| 958 | Data::Callback* callback_addr = nullptr; |
| 959 | if (node_ != nullptr) { |
| 960 | // Initialize for first pass callback. |
| 961 | DCHECK(node_->state() == Node::NEAR_DEATH); |
| 962 | callback_addr = &callback_; |
| 963 | } |
| 964 | Data data(reinterpret_cast<v8::Isolate*>(isolate), parameter_, |
| 965 | internal_fields_, callback_addr); |
| 966 | Data::Callback callback = callback_; |
| 967 | callback_ = nullptr; |
| 968 | callback(data); |
| 969 | if (node_ != nullptr) { |
| 970 | // Transition to second pass state. |
| 971 | DCHECK(node_->state() == Node::FREE); |
| 972 | node_ = nullptr; |
| 973 | } |
| 974 | } |
| 975 | |
| 976 | |
| 977 | int GlobalHandles::PostGarbageCollectionProcessing( |
| 978 | GarbageCollector collector, const v8::GCCallbackFlags gc_callback_flags) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 979 | // Process weak global handle callbacks. This must be done after the |
| 980 | // GC is completely done, because the callbacks may invoke arbitrary |
| 981 | // API functions. |
| 982 | DCHECK(isolate_->heap()->gc_state() == Heap::NOT_IN_GC); |
| 983 | const int initial_post_gc_processing_count = ++post_gc_processing_count_; |
| 984 | int freed_nodes = 0; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 985 | bool synchronous_second_pass = |
| 986 | (gc_callback_flags & |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 987 | (kGCCallbackFlagForced | kGCCallbackFlagCollectAllAvailableGarbage | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 988 | kGCCallbackFlagSynchronousPhantomCallbackProcessing)) != 0; |
| 989 | freed_nodes += DispatchPendingPhantomCallbacks(synchronous_second_pass); |
| 990 | if (initial_post_gc_processing_count != post_gc_processing_count_) { |
| 991 | // If the callbacks caused a nested GC, then return. See comment in |
| 992 | // PostScavengeProcessing. |
| 993 | return freed_nodes; |
| 994 | } |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 995 | if (collector == SCAVENGER) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 996 | freed_nodes += PostScavengeProcessing(initial_post_gc_processing_count); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 997 | } else { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 998 | freed_nodes += PostMarkSweepProcessing(initial_post_gc_processing_count); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 999 | } |
| 1000 | if (initial_post_gc_processing_count != post_gc_processing_count_) { |
| 1001 | // If the callbacks caused a nested GC, then return. See comment in |
| 1002 | // PostScavengeProcessing. |
| 1003 | return freed_nodes; |
| 1004 | } |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 1005 | if (initial_post_gc_processing_count == post_gc_processing_count_) { |
| 1006 | UpdateListOfNewSpaceNodes(); |
| 1007 | } |
| 1008 | return freed_nodes; |
| 1009 | } |
| 1010 | |
| 1011 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 1012 | void GlobalHandles::IterateStrongRoots(ObjectVisitor* v) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 1013 | for (NodeIterator it(this); !it.done(); it.Advance()) { |
| 1014 | if (it.node()->IsStrongRetainer()) { |
| 1015 | v->VisitPointer(it.node()->location()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1016 | } |
| 1017 | } |
| 1018 | } |
| 1019 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 1020 | |
| 1021 | void GlobalHandles::IterateAllRoots(ObjectVisitor* v) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 1022 | for (NodeIterator it(this); !it.done(); it.Advance()) { |
| 1023 | if (it.node()->IsRetainer()) { |
| 1024 | v->VisitPointer(it.node()->location()); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1025 | } |
| 1026 | } |
| 1027 | } |
| 1028 | |
| 1029 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1030 | void GlobalHandles::IterateAllRootsWithClassIds(ObjectVisitor* v) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 1031 | for (NodeIterator it(this); !it.done(); it.Advance()) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1032 | if (it.node()->IsRetainer() && it.node()->has_wrapper_class_id()) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 1033 | v->VisitEmbedderReference(it.node()->location(), |
| 1034 | it.node()->wrapper_class_id()); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1035 | } |
| 1036 | } |
| 1037 | } |
| 1038 | |
| 1039 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1040 | void GlobalHandles::IterateAllRootsInNewSpaceWithClassIds(ObjectVisitor* v) { |
| 1041 | for (int i = 0; i < new_space_nodes_.length(); ++i) { |
| 1042 | Node* node = new_space_nodes_[i]; |
| 1043 | if (node->IsRetainer() && node->has_wrapper_class_id()) { |
| 1044 | v->VisitEmbedderReference(node->location(), |
| 1045 | node->wrapper_class_id()); |
| 1046 | } |
| 1047 | } |
| 1048 | } |
| 1049 | |
| 1050 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1051 | void GlobalHandles::IterateWeakRootsInNewSpaceWithClassIds(ObjectVisitor* v) { |
| 1052 | for (int i = 0; i < new_space_nodes_.length(); ++i) { |
| 1053 | Node* node = new_space_nodes_[i]; |
| 1054 | if (node->has_wrapper_class_id() && node->IsWeak()) { |
| 1055 | v->VisitEmbedderReference(node->location(), node->wrapper_class_id()); |
| 1056 | } |
| 1057 | } |
| 1058 | } |
| 1059 | |
| 1060 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1061 | int GlobalHandles::NumberOfWeakHandles() { |
| 1062 | int count = 0; |
| 1063 | for (NodeIterator it(this); !it.done(); it.Advance()) { |
| 1064 | if (it.node()->IsWeakRetainer()) { |
| 1065 | count++; |
| 1066 | } |
| 1067 | } |
| 1068 | return count; |
| 1069 | } |
| 1070 | |
| 1071 | |
| 1072 | int GlobalHandles::NumberOfGlobalObjectWeakHandles() { |
| 1073 | int count = 0; |
| 1074 | for (NodeIterator it(this); !it.done(); it.Advance()) { |
| 1075 | if (it.node()->IsWeakRetainer() && |
| 1076 | it.node()->object()->IsJSGlobalObject()) { |
| 1077 | count++; |
| 1078 | } |
| 1079 | } |
| 1080 | return count; |
| 1081 | } |
| 1082 | |
| 1083 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 1084 | void GlobalHandles::RecordStats(HeapStats* stats) { |
| 1085 | *stats->global_handle_count = 0; |
| 1086 | *stats->weak_global_handle_count = 0; |
| 1087 | *stats->pending_global_handle_count = 0; |
| 1088 | *stats->near_death_global_handle_count = 0; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 1089 | *stats->free_global_handle_count = 0; |
| 1090 | for (NodeIterator it(this); !it.done(); it.Advance()) { |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 1091 | *stats->global_handle_count += 1; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 1092 | if (it.node()->state() == Node::WEAK) { |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 1093 | *stats->weak_global_handle_count += 1; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 1094 | } else if (it.node()->state() == Node::PENDING) { |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 1095 | *stats->pending_global_handle_count += 1; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 1096 | } else if (it.node()->state() == Node::NEAR_DEATH) { |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 1097 | *stats->near_death_global_handle_count += 1; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 1098 | } else if (it.node()->state() == Node::FREE) { |
| 1099 | *stats->free_global_handle_count += 1; |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 1100 | } |
| 1101 | } |
| 1102 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1103 | |
| 1104 | #ifdef DEBUG |
| 1105 | |
| 1106 | void GlobalHandles::PrintStats() { |
| 1107 | int total = 0; |
| 1108 | int weak = 0; |
| 1109 | int pending = 0; |
| 1110 | int near_death = 0; |
| 1111 | int destroyed = 0; |
| 1112 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 1113 | for (NodeIterator it(this); !it.done(); it.Advance()) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1114 | total++; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 1115 | if (it.node()->state() == Node::WEAK) weak++; |
| 1116 | if (it.node()->state() == Node::PENDING) pending++; |
| 1117 | if (it.node()->state() == Node::NEAR_DEATH) near_death++; |
| 1118 | if (it.node()->state() == Node::FREE) destroyed++; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1119 | } |
| 1120 | |
| 1121 | PrintF("Global Handle Statistics:\n"); |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 1122 | PrintF(" allocated memory = %" V8_PTR_PREFIX "dB\n", sizeof(Node) * total); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1123 | PrintF(" # weak = %d\n", weak); |
| 1124 | PrintF(" # pending = %d\n", pending); |
| 1125 | PrintF(" # near_death = %d\n", near_death); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 1126 | PrintF(" # free = %d\n", destroyed); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1127 | PrintF(" # total = %d\n", total); |
| 1128 | } |
| 1129 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1130 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1131 | void GlobalHandles::Print() { |
| 1132 | PrintF("Global handles:\n"); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 1133 | for (NodeIterator it(this); !it.done(); it.Advance()) { |
| 1134 | PrintF(" handle %p to %p%s\n", |
| 1135 | reinterpret_cast<void*>(it.node()->location()), |
| 1136 | reinterpret_cast<void*>(it.node()->object()), |
| 1137 | it.node()->IsWeak() ? " (weak)" : ""); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1138 | } |
| 1139 | } |
| 1140 | |
| 1141 | #endif |
| 1142 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1143 | |
| 1144 | |
| 1145 | void GlobalHandles::AddObjectGroup(Object*** handles, |
| 1146 | size_t length, |
| 1147 | v8::RetainedObjectInfo* info) { |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1148 | #ifdef DEBUG |
| 1149 | for (size_t i = 0; i < length; ++i) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1150 | DCHECK(!Node::FromLocation(handles[i])->is_independent()); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1151 | } |
| 1152 | #endif |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1153 | if (length == 0) { |
| 1154 | if (info != NULL) info->Dispose(); |
| 1155 | return; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1156 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1157 | ObjectGroup* group = new ObjectGroup(length); |
| 1158 | for (size_t i = 0; i < length; ++i) |
| 1159 | group->objects[i] = handles[i]; |
| 1160 | group->info = info; |
| 1161 | object_groups_.Add(group); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1162 | } |
| 1163 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1164 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1165 | void GlobalHandles::SetObjectGroupId(Object** handle, |
| 1166 | UniqueId id) { |
| 1167 | object_group_connections_.Add(ObjectGroupConnection(id, handle)); |
| 1168 | } |
| 1169 | |
| 1170 | |
| 1171 | void GlobalHandles::SetRetainedObjectInfo(UniqueId id, |
| 1172 | RetainedObjectInfo* info) { |
| 1173 | retainer_infos_.Add(ObjectGroupRetainerInfo(id, info)); |
| 1174 | } |
| 1175 | |
| 1176 | |
| 1177 | void GlobalHandles::SetReferenceFromGroup(UniqueId id, Object** child) { |
| 1178 | DCHECK(!Node::FromLocation(child)->is_independent()); |
| 1179 | implicit_ref_connections_.Add(ObjectGroupConnection(id, child)); |
| 1180 | } |
| 1181 | |
| 1182 | |
| 1183 | void GlobalHandles::SetReference(HeapObject** parent, Object** child) { |
| 1184 | DCHECK(!Node::FromLocation(child)->is_independent()); |
| 1185 | ImplicitRefGroup* group = new ImplicitRefGroup(parent, 1); |
| 1186 | group->children[0] = child; |
| 1187 | implicit_ref_groups_.Add(group); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1188 | } |
| 1189 | |
| 1190 | |
| 1191 | void GlobalHandles::RemoveObjectGroups() { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1192 | for (int i = 0; i < object_groups_.length(); i++) |
| 1193 | delete object_groups_.at(i); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1194 | object_groups_.Clear(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1195 | for (int i = 0; i < retainer_infos_.length(); ++i) |
| 1196 | retainer_infos_[i].info->Dispose(); |
| 1197 | retainer_infos_.Clear(); |
| 1198 | object_group_connections_.Clear(); |
| 1199 | object_group_connections_.Initialize(kObjectGroupConnectionsCapacity); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1200 | } |
| 1201 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1202 | |
| 1203 | void GlobalHandles::RemoveImplicitRefGroups() { |
| 1204 | for (int i = 0; i < implicit_ref_groups_.length(); i++) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1205 | delete implicit_ref_groups_.at(i); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1206 | } |
| 1207 | implicit_ref_groups_.Clear(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1208 | implicit_ref_connections_.Clear(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1209 | } |
| 1210 | |
| 1211 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 1212 | void GlobalHandles::TearDown() { |
| 1213 | // TODO(1428): invoke weak callbacks. |
| 1214 | } |
| 1215 | |
| 1216 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1217 | void GlobalHandles::ComputeObjectGroupsAndImplicitReferences() { |
| 1218 | if (object_group_connections_.length() == 0) { |
| 1219 | for (int i = 0; i < retainer_infos_.length(); ++i) |
| 1220 | retainer_infos_[i].info->Dispose(); |
| 1221 | retainer_infos_.Clear(); |
| 1222 | implicit_ref_connections_.Clear(); |
| 1223 | return; |
| 1224 | } |
| 1225 | |
| 1226 | object_group_connections_.Sort(); |
| 1227 | retainer_infos_.Sort(); |
| 1228 | implicit_ref_connections_.Sort(); |
| 1229 | |
| 1230 | int info_index = 0; // For iterating retainer_infos_. |
| 1231 | UniqueId current_group_id(0); |
| 1232 | int current_group_start = 0; |
| 1233 | |
| 1234 | int current_implicit_refs_start = 0; |
| 1235 | int current_implicit_refs_end = 0; |
| 1236 | for (int i = 0; i <= object_group_connections_.length(); ++i) { |
| 1237 | if (i == 0) |
| 1238 | current_group_id = object_group_connections_[i].id; |
| 1239 | if (i == object_group_connections_.length() || |
| 1240 | current_group_id != object_group_connections_[i].id) { |
| 1241 | // Group detected: objects in indices [current_group_start, i[. |
| 1242 | |
| 1243 | // Find out which implicit references are related to this group. (We want |
| 1244 | // to ignore object groups which only have 1 object, but that object is |
| 1245 | // needed as a representative object for the implicit refrerence group.) |
| 1246 | while (current_implicit_refs_start < implicit_ref_connections_.length() && |
| 1247 | implicit_ref_connections_[current_implicit_refs_start].id < |
| 1248 | current_group_id) |
| 1249 | ++current_implicit_refs_start; |
| 1250 | current_implicit_refs_end = current_implicit_refs_start; |
| 1251 | while (current_implicit_refs_end < implicit_ref_connections_.length() && |
| 1252 | implicit_ref_connections_[current_implicit_refs_end].id == |
| 1253 | current_group_id) |
| 1254 | ++current_implicit_refs_end; |
| 1255 | |
| 1256 | if (current_implicit_refs_end > current_implicit_refs_start) { |
| 1257 | // Find a representative object for the implicit references. |
| 1258 | HeapObject** representative = NULL; |
| 1259 | for (int j = current_group_start; j < i; ++j) { |
| 1260 | Object** object = object_group_connections_[j].object; |
| 1261 | if ((*object)->IsHeapObject()) { |
| 1262 | representative = reinterpret_cast<HeapObject**>(object); |
| 1263 | break; |
| 1264 | } |
| 1265 | } |
| 1266 | if (representative) { |
| 1267 | ImplicitRefGroup* group = new ImplicitRefGroup( |
| 1268 | representative, |
| 1269 | current_implicit_refs_end - current_implicit_refs_start); |
| 1270 | for (int j = current_implicit_refs_start; |
| 1271 | j < current_implicit_refs_end; |
| 1272 | ++j) { |
| 1273 | group->children[j - current_implicit_refs_start] = |
| 1274 | implicit_ref_connections_[j].object; |
| 1275 | } |
| 1276 | implicit_ref_groups_.Add(group); |
| 1277 | } |
| 1278 | current_implicit_refs_start = current_implicit_refs_end; |
| 1279 | } |
| 1280 | |
| 1281 | // Find a RetainedObjectInfo for the group. |
| 1282 | RetainedObjectInfo* info = NULL; |
| 1283 | while (info_index < retainer_infos_.length() && |
| 1284 | retainer_infos_[info_index].id < current_group_id) { |
| 1285 | retainer_infos_[info_index].info->Dispose(); |
| 1286 | ++info_index; |
| 1287 | } |
| 1288 | if (info_index < retainer_infos_.length() && |
| 1289 | retainer_infos_[info_index].id == current_group_id) { |
| 1290 | // This object group has an associated ObjectGroupRetainerInfo. |
| 1291 | info = retainer_infos_[info_index].info; |
| 1292 | ++info_index; |
| 1293 | } |
| 1294 | |
| 1295 | // Ignore groups which only contain one object. |
| 1296 | if (i > current_group_start + 1) { |
| 1297 | ObjectGroup* group = new ObjectGroup(i - current_group_start); |
| 1298 | for (int j = current_group_start; j < i; ++j) { |
| 1299 | group->objects[j - current_group_start] = |
| 1300 | object_group_connections_[j].object; |
| 1301 | } |
| 1302 | group->info = info; |
| 1303 | object_groups_.Add(group); |
| 1304 | } else if (info) { |
| 1305 | info->Dispose(); |
| 1306 | } |
| 1307 | |
| 1308 | if (i < object_group_connections_.length()) { |
| 1309 | current_group_id = object_group_connections_[i].id; |
| 1310 | current_group_start = i; |
| 1311 | } |
| 1312 | } |
| 1313 | } |
| 1314 | object_group_connections_.Clear(); |
| 1315 | object_group_connections_.Initialize(kObjectGroupConnectionsCapacity); |
| 1316 | retainer_infos_.Clear(); |
| 1317 | implicit_ref_connections_.Clear(); |
| 1318 | } |
| 1319 | |
| 1320 | |
| 1321 | EternalHandles::EternalHandles() : size_(0) { |
| 1322 | for (unsigned i = 0; i < arraysize(singleton_handles_); i++) { |
| 1323 | singleton_handles_[i] = kInvalidIndex; |
| 1324 | } |
| 1325 | } |
| 1326 | |
| 1327 | |
| 1328 | EternalHandles::~EternalHandles() { |
| 1329 | for (int i = 0; i < blocks_.length(); i++) delete[] blocks_[i]; |
| 1330 | } |
| 1331 | |
| 1332 | |
| 1333 | void EternalHandles::IterateAllRoots(ObjectVisitor* visitor) { |
| 1334 | int limit = size_; |
| 1335 | for (int i = 0; i < blocks_.length(); i++) { |
| 1336 | DCHECK(limit > 0); |
| 1337 | Object** block = blocks_[i]; |
| 1338 | visitor->VisitPointers(block, block + Min(limit, kSize)); |
| 1339 | limit -= kSize; |
| 1340 | } |
| 1341 | } |
| 1342 | |
| 1343 | |
| 1344 | void EternalHandles::IterateNewSpaceRoots(ObjectVisitor* visitor) { |
| 1345 | for (int i = 0; i < new_space_indices_.length(); i++) { |
| 1346 | visitor->VisitPointer(GetLocation(new_space_indices_[i])); |
| 1347 | } |
| 1348 | } |
| 1349 | |
| 1350 | |
| 1351 | void EternalHandles::PostGarbageCollectionProcessing(Heap* heap) { |
| 1352 | int last = 0; |
| 1353 | for (int i = 0; i < new_space_indices_.length(); i++) { |
| 1354 | int index = new_space_indices_[i]; |
| 1355 | if (heap->InNewSpace(*GetLocation(index))) { |
| 1356 | new_space_indices_[last++] = index; |
| 1357 | } |
| 1358 | } |
| 1359 | new_space_indices_.Rewind(last); |
| 1360 | } |
| 1361 | |
| 1362 | |
| 1363 | void EternalHandles::Create(Isolate* isolate, Object* object, int* index) { |
| 1364 | DCHECK_EQ(kInvalidIndex, *index); |
| 1365 | if (object == NULL) return; |
| 1366 | DCHECK_NE(isolate->heap()->the_hole_value(), object); |
| 1367 | int block = size_ >> kShift; |
| 1368 | int offset = size_ & kMask; |
| 1369 | // need to resize |
| 1370 | if (offset == 0) { |
| 1371 | Object** next_block = new Object*[kSize]; |
| 1372 | Object* the_hole = isolate->heap()->the_hole_value(); |
| 1373 | MemsetPointer(next_block, the_hole, kSize); |
| 1374 | blocks_.Add(next_block); |
| 1375 | } |
| 1376 | DCHECK_EQ(isolate->heap()->the_hole_value(), blocks_[block][offset]); |
| 1377 | blocks_[block][offset] = object; |
| 1378 | if (isolate->heap()->InNewSpace(object)) { |
| 1379 | new_space_indices_.Add(size_); |
| 1380 | } |
| 1381 | *index = size_++; |
| 1382 | } |
| 1383 | |
| 1384 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1385 | } // namespace internal |
| 1386 | } // namespace v8 |