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