Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1 | // Copyright 2009 the V8 project authors. All rights reserved. |
| 2 | // Redistribution and use in source and binary forms, with or without |
| 3 | // modification, are permitted provided that the following conditions are |
| 4 | // met: |
| 5 | // |
| 6 | // * Redistributions of source code must retain the above copyright |
| 7 | // notice, this list of conditions and the following disclaimer. |
| 8 | // * Redistributions in binary form must reproduce the above |
| 9 | // copyright notice, this list of conditions and the following |
| 10 | // disclaimer in the documentation and/or other materials provided |
| 11 | // with the distribution. |
| 12 | // * Neither the name of Google Inc. nor the names of its |
| 13 | // contributors may be used to endorse or promote products derived |
| 14 | // from this software without specific prior written permission. |
| 15 | // |
| 16 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | |
| 28 | #include "v8.h" |
| 29 | |
| 30 | #include "api.h" |
| 31 | #include "global-handles.h" |
| 32 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 33 | #include "vm-state-inl.h" |
| 34 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 35 | namespace v8 { |
| 36 | namespace internal { |
| 37 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 38 | |
| 39 | ObjectGroup::~ObjectGroup() { |
| 40 | if (info_ != NULL) info_->Dispose(); |
| 41 | } |
| 42 | |
| 43 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 44 | class GlobalHandles::Node { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 45 | public: |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 46 | // State transition diagram: |
| 47 | // FREE -> NORMAL <-> WEAK -> PENDING -> NEAR_DEATH -> { NORMAL, WEAK, FREE } |
| 48 | enum State { |
| 49 | FREE, |
| 50 | NORMAL, // Normal global handle. |
| 51 | WEAK, // Flagged as weak but not yet finalized. |
| 52 | PENDING, // Has been recognized as only reachable by weak handles. |
| 53 | NEAR_DEATH // Callback has informed the handle is near death. |
| 54 | }; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 55 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 56 | // Maps handle location (slot) to the containing node. |
| 57 | static Node* FromLocation(Object** location) { |
| 58 | ASSERT(OFFSET_OF(Node, object_) == 0); |
| 59 | return reinterpret_cast<Node*>(location); |
| 60 | } |
| 61 | |
| 62 | Node() {} |
| 63 | |
| 64 | #ifdef DEBUG |
| 65 | ~Node() { |
| 66 | // TODO(1428): if it's a weak handle we should have invoked its callback. |
| 67 | // Zap the values for eager trapping. |
| 68 | object_ = NULL; |
| 69 | class_id_ = v8::HeapProfiler::kPersistentHandleNoClassId; |
| 70 | index_ = 0; |
| 71 | independent_ = false; |
| 72 | in_new_space_list_ = false; |
| 73 | parameter_or_next_free_.next_free = NULL; |
| 74 | callback_ = NULL; |
| 75 | } |
| 76 | #endif |
| 77 | |
| 78 | void Initialize(int index, Node** first_free) { |
| 79 | index_ = static_cast<uint8_t>(index); |
| 80 | ASSERT(static_cast<int>(index_) == index); |
| 81 | state_ = FREE; |
| 82 | in_new_space_list_ = false; |
| 83 | parameter_or_next_free_.next_free = *first_free; |
| 84 | *first_free = this; |
| 85 | } |
| 86 | |
| 87 | void Acquire(Object* object, GlobalHandles* global_handles) { |
| 88 | ASSERT(state_ == FREE); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 89 | object_ = object; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 90 | class_id_ = v8::HeapProfiler::kPersistentHandleNoClassId; |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 91 | independent_ = false; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 92 | state_ = NORMAL; |
| 93 | parameter_or_next_free_.parameter = NULL; |
| 94 | callback_ = NULL; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 95 | IncreaseBlockUses(global_handles); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 96 | } |
| 97 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 98 | void Release(GlobalHandles* global_handles) { |
| 99 | ASSERT(state_ != FREE); |
| 100 | if (IsWeakRetainer()) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 101 | global_handles->number_of_weak_handles_--; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 102 | if (object_->IsJSGlobalObject()) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 103 | global_handles->number_of_global_object_weak_handles_--; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 104 | } |
| 105 | } |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 106 | state_ = FREE; |
| 107 | parameter_or_next_free_.next_free = global_handles->first_free_; |
| 108 | global_handles->first_free_ = this; |
| 109 | DecreaseBlockUses(global_handles); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 110 | } |
| 111 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 112 | // Object slot accessors. |
| 113 | Object* object() const { return object_; } |
| 114 | Object** location() { return &object_; } |
| 115 | Handle<Object> handle() { return Handle<Object>(location()); } |
| 116 | |
| 117 | // Wrapper class ID accessors. |
| 118 | bool has_wrapper_class_id() const { |
| 119 | return class_id_ != v8::HeapProfiler::kPersistentHandleNoClassId; |
| 120 | } |
| 121 | uint16_t wrapper_class_id() const { return class_id_; } |
| 122 | void set_wrapper_class_id(uint16_t class_id) { |
| 123 | class_id_ = class_id; |
| 124 | } |
| 125 | |
| 126 | // State accessors. |
| 127 | |
| 128 | State state() const { return state_; } |
| 129 | |
| 130 | bool IsNearDeath() const { |
| 131 | // Check for PENDING to ensure correct answer when processing callbacks. |
| 132 | return state_ == PENDING || state_ == NEAR_DEATH; |
| 133 | } |
| 134 | |
| 135 | bool IsWeak() const { return state_ == WEAK; } |
| 136 | |
| 137 | bool IsRetainer() const { return state_ != FREE; } |
| 138 | |
| 139 | bool IsStrongRetainer() const { return state_ == NORMAL; } |
| 140 | |
| 141 | bool IsWeakRetainer() const { |
| 142 | return state_ == WEAK || state_ == PENDING || state_ == NEAR_DEATH; |
| 143 | } |
| 144 | |
| 145 | void MarkPending() { |
| 146 | ASSERT(state_ == WEAK); |
| 147 | state_ = PENDING; |
| 148 | } |
| 149 | |
| 150 | // Independent flag accessors. |
| 151 | void MarkIndependent() { |
| 152 | ASSERT(state_ != FREE); |
| 153 | independent_ = true; |
| 154 | } |
| 155 | bool is_independent() const { return independent_; } |
| 156 | |
| 157 | // In-new-space-list flag accessors. |
| 158 | void set_in_new_space_list(bool v) { in_new_space_list_ = v; } |
| 159 | bool is_in_new_space_list() const { return in_new_space_list_; } |
| 160 | |
| 161 | // Callback accessor. |
| 162 | WeakReferenceCallback callback() { return callback_; } |
| 163 | |
| 164 | // Callback parameter accessors. |
| 165 | void set_parameter(void* parameter) { |
| 166 | ASSERT(state_ != FREE); |
| 167 | parameter_or_next_free_.parameter = parameter; |
| 168 | } |
| 169 | void* parameter() const { |
| 170 | ASSERT(state_ != FREE); |
| 171 | return parameter_or_next_free_.parameter; |
| 172 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 173 | |
| 174 | // Accessors for next free node in the free list. |
| 175 | Node* next_free() { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 176 | ASSERT(state_ == FREE); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 177 | return parameter_or_next_free_.next_free; |
| 178 | } |
| 179 | void set_next_free(Node* value) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 180 | ASSERT(state_ == FREE); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 181 | parameter_or_next_free_.next_free = value; |
| 182 | } |
| 183 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 184 | void MakeWeak(GlobalHandles* global_handles, |
| 185 | void* parameter, |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 186 | WeakReferenceCallback callback) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 187 | ASSERT(state_ != FREE); |
| 188 | if (!IsWeakRetainer()) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 189 | global_handles->number_of_weak_handles_++; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 190 | if (object_->IsJSGlobalObject()) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 191 | global_handles->number_of_global_object_weak_handles_++; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 192 | } |
| 193 | } |
| 194 | state_ = WEAK; |
| 195 | set_parameter(parameter); |
| 196 | callback_ = callback; |
| 197 | } |
| 198 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 199 | void ClearWeakness(GlobalHandles* global_handles) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 200 | ASSERT(state_ != FREE); |
| 201 | if (IsWeakRetainer()) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 202 | global_handles->number_of_weak_handles_--; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 203 | if (object_->IsJSGlobalObject()) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 204 | global_handles->number_of_global_object_weak_handles_--; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 205 | } |
| 206 | } |
| 207 | state_ = NORMAL; |
| 208 | set_parameter(NULL); |
| 209 | } |
| 210 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 211 | bool PostGarbageCollectionProcessing(Isolate* isolate, |
| 212 | GlobalHandles* global_handles) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 213 | if (state_ != Node::PENDING) return false; |
Kristian Monsen | 50ef84f | 2010-07-29 15:18:00 +0100 | [diff] [blame] | 214 | WeakReferenceCallback func = callback(); |
| 215 | if (func == NULL) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 216 | Release(global_handles); |
Kristian Monsen | 50ef84f | 2010-07-29 15:18:00 +0100 | [diff] [blame] | 217 | return false; |
| 218 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 219 | void* par = parameter(); |
| 220 | state_ = NEAR_DEATH; |
| 221 | set_parameter(NULL); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 222 | |
| 223 | v8::Persistent<v8::Object> object = ToApi<v8::Object>(handle()); |
| 224 | { |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 225 | // Check that we are not passing a finalized external string to |
| 226 | // the callback. |
| 227 | ASSERT(!object_->IsExternalAsciiString() || |
| 228 | ExternalAsciiString::cast(object_)->resource() != NULL); |
| 229 | ASSERT(!object_->IsExternalTwoByteString() || |
| 230 | ExternalTwoByteString::cast(object_)->resource() != NULL); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 231 | // Leaving V8. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 232 | VMState state(isolate, EXTERNAL); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 233 | func(object, par); |
| 234 | } |
Kristian Monsen | 50ef84f | 2010-07-29 15:18:00 +0100 | [diff] [blame] | 235 | // Absense of explicit cleanup or revival of weak handle |
| 236 | // in most of the cases would lead to memory leak. |
| 237 | ASSERT(state_ != NEAR_DEATH); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 238 | return true; |
| 239 | } |
| 240 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 241 | private: |
| 242 | inline NodeBlock* FindBlock(); |
| 243 | inline void IncreaseBlockUses(GlobalHandles* global_handles); |
| 244 | inline void DecreaseBlockUses(GlobalHandles* global_handles); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 245 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 246 | // Storage for object pointer. |
| 247 | // Placed first to avoid offset computation. |
| 248 | Object* object_; |
| 249 | |
| 250 | // Next word stores class_id, index, state, and independent. |
| 251 | // Note: the most aligned fields should go first. |
| 252 | |
| 253 | // Wrapper class ID. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 254 | uint16_t class_id_; |
| 255 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 256 | // Index in the containing handle block. |
| 257 | uint8_t index_; |
| 258 | |
| 259 | // Need one more bit for MSVC as it treats enums as signed. |
| 260 | State state_ : 4; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 261 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 262 | bool independent_ : 1; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 263 | bool in_new_space_list_ : 1; |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 264 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 265 | // Handle specific callback. |
| 266 | WeakReferenceCallback callback_; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 267 | |
| 268 | // Provided data for callback. In FREE state, this is used for |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 269 | // the free list link. |
| 270 | union { |
| 271 | void* parameter; |
| 272 | Node* next_free; |
| 273 | } parameter_or_next_free_; |
| 274 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 275 | DISALLOW_COPY_AND_ASSIGN(Node); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 276 | }; |
| 277 | |
| 278 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 279 | class GlobalHandles::NodeBlock { |
| 280 | public: |
| 281 | static const int kSize = 256; |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 282 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 283 | explicit NodeBlock(NodeBlock* next) |
| 284 | : next_(next), used_nodes_(0), next_used_(NULL), prev_used_(NULL) {} |
| 285 | |
| 286 | void PutNodesOnFreeList(Node** first_free) { |
| 287 | for (int i = kSize - 1; i >= 0; --i) { |
| 288 | nodes_[i].Initialize(i, first_free); |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | Node* node_at(int index) { |
| 293 | ASSERT(0 <= index && index < kSize); |
| 294 | return &nodes_[index]; |
| 295 | } |
| 296 | |
| 297 | void IncreaseUses(GlobalHandles* global_handles) { |
| 298 | ASSERT(used_nodes_ < kSize); |
| 299 | if (used_nodes_++ == 0) { |
| 300 | NodeBlock* old_first = global_handles->first_used_block_; |
| 301 | global_handles->first_used_block_ = this; |
| 302 | next_used_ = old_first; |
| 303 | prev_used_ = NULL; |
| 304 | if (old_first == NULL) return; |
| 305 | old_first->prev_used_ = this; |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | void DecreaseUses(GlobalHandles* global_handles) { |
| 310 | ASSERT(used_nodes_ > 0); |
| 311 | if (--used_nodes_ == 0) { |
| 312 | if (next_used_ != NULL) next_used_->prev_used_ = prev_used_; |
| 313 | if (prev_used_ != NULL) prev_used_->next_used_ = next_used_; |
| 314 | if (this == global_handles->first_used_block_) { |
| 315 | global_handles->first_used_block_ = next_used_; |
Ben Murdoch | bb769b2 | 2010-08-11 14:56:33 +0100 | [diff] [blame] | 316 | } |
| 317 | } |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 318 | } |
Ben Murdoch | bb769b2 | 2010-08-11 14:56:33 +0100 | [diff] [blame] | 319 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 320 | // Next block in the list of all blocks. |
| 321 | NodeBlock* next() const { return next_; } |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 322 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 323 | // Next/previous block in the list of blocks with used nodes. |
| 324 | NodeBlock* next_used() const { return next_used_; } |
| 325 | NodeBlock* prev_used() const { return prev_used_; } |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 326 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 327 | private: |
| 328 | Node nodes_[kSize]; |
| 329 | NodeBlock* const next_; |
| 330 | int used_nodes_; |
| 331 | NodeBlock* next_used_; |
| 332 | NodeBlock* prev_used_; |
| 333 | }; |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 334 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 335 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 336 | GlobalHandles::NodeBlock* GlobalHandles::Node::FindBlock() { |
| 337 | intptr_t ptr = reinterpret_cast<intptr_t>(this); |
| 338 | ptr = ptr - index_ * sizeof(Node); |
| 339 | NodeBlock* block = reinterpret_cast<NodeBlock*>(ptr); |
| 340 | ASSERT(block->node_at(index_) == this); |
| 341 | return block; |
| 342 | } |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 343 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 344 | |
| 345 | void GlobalHandles::Node::IncreaseBlockUses(GlobalHandles* global_handles) { |
| 346 | FindBlock()->IncreaseUses(global_handles); |
| 347 | } |
| 348 | |
| 349 | |
| 350 | void GlobalHandles::Node::DecreaseBlockUses(GlobalHandles* global_handles) { |
| 351 | FindBlock()->DecreaseUses(global_handles); |
| 352 | } |
| 353 | |
| 354 | |
| 355 | class GlobalHandles::NodeIterator { |
| 356 | public: |
| 357 | explicit NodeIterator(GlobalHandles* global_handles) |
| 358 | : block_(global_handles->first_used_block_), |
| 359 | index_(0) {} |
| 360 | |
| 361 | bool done() const { return block_ == NULL; } |
| 362 | |
| 363 | Node* node() const { |
| 364 | ASSERT(!done()); |
| 365 | return block_->node_at(index_); |
| 366 | } |
| 367 | |
| 368 | void Advance() { |
| 369 | ASSERT(!done()); |
| 370 | if (++index_ < NodeBlock::kSize) return; |
| 371 | index_ = 0; |
| 372 | block_ = block_->next_used(); |
| 373 | } |
| 374 | |
| 375 | private: |
| 376 | NodeBlock* block_; |
| 377 | int index_; |
| 378 | |
| 379 | DISALLOW_COPY_AND_ASSIGN(NodeIterator); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 380 | }; |
| 381 | |
| 382 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 383 | GlobalHandles::GlobalHandles(Isolate* isolate) |
| 384 | : isolate_(isolate), |
| 385 | number_of_weak_handles_(0), |
| 386 | number_of_global_object_weak_handles_(0), |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 387 | first_block_(NULL), |
| 388 | first_used_block_(NULL), |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 389 | first_free_(NULL), |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 390 | post_gc_processing_count_(0) {} |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 391 | |
| 392 | |
| 393 | GlobalHandles::~GlobalHandles() { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 394 | NodeBlock* block = first_block_; |
| 395 | while (block != NULL) { |
| 396 | NodeBlock* tmp = block->next(); |
| 397 | delete block; |
| 398 | block = tmp; |
| 399 | } |
| 400 | first_block_ = NULL; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 401 | } |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 402 | |
| 403 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 404 | Handle<Object> GlobalHandles::Create(Object* value) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 405 | isolate_->counters()->global_handles()->Increment(); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 406 | if (first_free_ == NULL) { |
| 407 | first_block_ = new NodeBlock(first_block_); |
| 408 | first_block_->PutNodesOnFreeList(&first_free_); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 409 | } |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 410 | ASSERT(first_free_ != NULL); |
| 411 | // Take the first node in the free list. |
| 412 | Node* result = first_free_; |
| 413 | first_free_ = result->next_free(); |
| 414 | result->Acquire(value, this); |
| 415 | if (isolate_->heap()->InNewSpace(value) && |
| 416 | !result->is_in_new_space_list()) { |
| 417 | new_space_nodes_.Add(result); |
| 418 | result->set_in_new_space_list(true); |
| 419 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 420 | return result->handle(); |
| 421 | } |
| 422 | |
| 423 | |
| 424 | void GlobalHandles::Destroy(Object** location) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 425 | isolate_->counters()->global_handles()->Decrement(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 426 | if (location == NULL) return; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 427 | Node::FromLocation(location)->Release(this); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 428 | } |
| 429 | |
| 430 | |
| 431 | void GlobalHandles::MakeWeak(Object** location, void* parameter, |
| 432 | WeakReferenceCallback callback) { |
| 433 | ASSERT(callback != NULL); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 434 | Node::FromLocation(location)->MakeWeak(this, parameter, callback); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 435 | } |
| 436 | |
| 437 | |
| 438 | void GlobalHandles::ClearWeakness(Object** location) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 439 | Node::FromLocation(location)->ClearWeakness(this); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 440 | } |
| 441 | |
| 442 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 443 | void GlobalHandles::MarkIndependent(Object** location) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 444 | Node::FromLocation(location)->MarkIndependent(); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 445 | } |
| 446 | |
| 447 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 448 | bool GlobalHandles::IsNearDeath(Object** location) { |
| 449 | return Node::FromLocation(location)->IsNearDeath(); |
| 450 | } |
| 451 | |
| 452 | |
| 453 | bool GlobalHandles::IsWeak(Object** location) { |
| 454 | return Node::FromLocation(location)->IsWeak(); |
| 455 | } |
| 456 | |
| 457 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 458 | void GlobalHandles::SetWrapperClassId(Object** location, uint16_t class_id) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 459 | Node::FromLocation(location)->set_wrapper_class_id(class_id); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 460 | } |
| 461 | |
| 462 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 463 | void GlobalHandles::IterateWeakRoots(ObjectVisitor* v) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 464 | for (NodeIterator it(this); !it.done(); it.Advance()) { |
| 465 | if (it.node()->IsWeakRetainer()) v->VisitPointer(it.node()->location()); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 466 | } |
| 467 | } |
| 468 | |
| 469 | |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 470 | void GlobalHandles::IterateWeakRoots(WeakReferenceGuest f, |
| 471 | WeakReferenceCallback callback) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 472 | for (NodeIterator it(this); !it.done(); it.Advance()) { |
| 473 | if (it.node()->IsWeak() && it.node()->callback() == callback) { |
| 474 | f(it.node()->object(), it.node()->parameter()); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 475 | } |
| 476 | } |
| 477 | } |
| 478 | |
| 479 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 480 | void GlobalHandles::IdentifyWeakHandles(WeakSlotCallback f) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 481 | for (NodeIterator it(this); !it.done(); it.Advance()) { |
| 482 | if (it.node()->IsWeak() && f(it.node()->location())) { |
| 483 | it.node()->MarkPending(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 484 | } |
| 485 | } |
| 486 | } |
| 487 | |
| 488 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 489 | void GlobalHandles::IterateNewSpaceStrongAndDependentRoots(ObjectVisitor* v) { |
| 490 | for (int i = 0; i < new_space_nodes_.length(); ++i) { |
| 491 | Node* node = new_space_nodes_[i]; |
| 492 | if (node->IsStrongRetainer() || |
| 493 | (node->IsWeakRetainer() && !node->is_independent())) { |
| 494 | v->VisitPointer(node->location()); |
| 495 | } |
| 496 | } |
| 497 | } |
| 498 | |
| 499 | |
| 500 | void GlobalHandles::IdentifyNewSpaceWeakIndependentHandles( |
| 501 | WeakSlotCallbackWithHeap f) { |
| 502 | for (int i = 0; i < new_space_nodes_.length(); ++i) { |
| 503 | Node* node = new_space_nodes_[i]; |
| 504 | ASSERT(node->is_in_new_space_list()); |
| 505 | if (node->is_independent() && node->IsWeak() && |
| 506 | f(isolate_->heap(), node->location())) { |
| 507 | node->MarkPending(); |
| 508 | } |
| 509 | } |
| 510 | } |
| 511 | |
| 512 | |
| 513 | void GlobalHandles::IterateNewSpaceWeakIndependentRoots(ObjectVisitor* v) { |
| 514 | for (int i = 0; i < new_space_nodes_.length(); ++i) { |
| 515 | Node* node = new_space_nodes_[i]; |
| 516 | ASSERT(node->is_in_new_space_list()); |
| 517 | if (node->is_independent() && node->IsWeakRetainer()) { |
| 518 | v->VisitPointer(node->location()); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 519 | } |
| 520 | } |
| 521 | } |
| 522 | |
| 523 | |
| 524 | bool GlobalHandles::PostGarbageCollectionProcessing( |
| 525 | GarbageCollector collector) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 526 | // Process weak global handle callbacks. This must be done after the |
| 527 | // GC is completely done, because the callbacks may invoke arbitrary |
| 528 | // API functions. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 529 | ASSERT(isolate_->heap()->gc_state() == Heap::NOT_IN_GC); |
| 530 | const int initial_post_gc_processing_count = ++post_gc_processing_count_; |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 531 | bool next_gc_likely_to_collect_more = false; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 532 | if (collector == SCAVENGER) { |
| 533 | for (int i = 0; i < new_space_nodes_.length(); ++i) { |
| 534 | Node* node = new_space_nodes_[i]; |
| 535 | ASSERT(node->is_in_new_space_list()); |
| 536 | // Skip dependent handles. Their weak callbacks might expect to be |
| 537 | // called between two global garbage collection callbacks which |
| 538 | // are not called for minor collections. |
| 539 | if (!node->is_independent()) continue; |
| 540 | if (node->PostGarbageCollectionProcessing(isolate_, this)) { |
| 541 | if (initial_post_gc_processing_count != post_gc_processing_count_) { |
| 542 | // Weak callback triggered another GC and another round of |
| 543 | // PostGarbageCollection processing. The current node might |
| 544 | // have been deleted in that round, so we need to bail out (or |
| 545 | // restart the processing). |
| 546 | return next_gc_likely_to_collect_more; |
| 547 | } |
| 548 | } |
| 549 | if (!node->IsRetainer()) { |
| 550 | next_gc_likely_to_collect_more = true; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 551 | } |
| 552 | } |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 553 | } else { |
| 554 | for (NodeIterator it(this); !it.done(); it.Advance()) { |
| 555 | if (it.node()->PostGarbageCollectionProcessing(isolate_, this)) { |
| 556 | if (initial_post_gc_processing_count != post_gc_processing_count_) { |
| 557 | // See the comment above. |
| 558 | return next_gc_likely_to_collect_more; |
| 559 | } |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 560 | } |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 561 | if (!it.node()->IsRetainer()) { |
| 562 | next_gc_likely_to_collect_more = true; |
| 563 | } |
| 564 | } |
| 565 | } |
| 566 | // Update the list of new space nodes. |
| 567 | int last = 0; |
| 568 | for (int i = 0; i < new_space_nodes_.length(); ++i) { |
| 569 | Node* node = new_space_nodes_[i]; |
| 570 | ASSERT(node->is_in_new_space_list()); |
| 571 | if (node->IsRetainer() && isolate_->heap()->InNewSpace(node->object())) { |
| 572 | new_space_nodes_[last++] = node; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 573 | } else { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 574 | node->set_in_new_space_list(false); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 575 | } |
| 576 | } |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 577 | new_space_nodes_.Rewind(last); |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 578 | return next_gc_likely_to_collect_more; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 579 | } |
| 580 | |
| 581 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 582 | void GlobalHandles::IterateStrongRoots(ObjectVisitor* v) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 583 | for (NodeIterator it(this); !it.done(); it.Advance()) { |
| 584 | if (it.node()->IsStrongRetainer()) { |
| 585 | v->VisitPointer(it.node()->location()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 586 | } |
| 587 | } |
| 588 | } |
| 589 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 590 | |
| 591 | void GlobalHandles::IterateAllRoots(ObjectVisitor* v) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 592 | for (NodeIterator it(this); !it.done(); it.Advance()) { |
| 593 | if (it.node()->IsRetainer()) { |
| 594 | v->VisitPointer(it.node()->location()); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 595 | } |
| 596 | } |
| 597 | } |
| 598 | |
| 599 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 600 | void GlobalHandles::IterateAllRootsWithClassIds(ObjectVisitor* v) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 601 | for (NodeIterator it(this); !it.done(); it.Advance()) { |
| 602 | if (it.node()->has_wrapper_class_id() && it.node()->IsRetainer()) { |
| 603 | v->VisitEmbedderReference(it.node()->location(), |
| 604 | it.node()->wrapper_class_id()); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 605 | } |
| 606 | } |
| 607 | } |
| 608 | |
| 609 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 610 | void GlobalHandles::RecordStats(HeapStats* stats) { |
| 611 | *stats->global_handle_count = 0; |
| 612 | *stats->weak_global_handle_count = 0; |
| 613 | *stats->pending_global_handle_count = 0; |
| 614 | *stats->near_death_global_handle_count = 0; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 615 | *stats->free_global_handle_count = 0; |
| 616 | for (NodeIterator it(this); !it.done(); it.Advance()) { |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 617 | *stats->global_handle_count += 1; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 618 | if (it.node()->state() == Node::WEAK) { |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 619 | *stats->weak_global_handle_count += 1; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 620 | } else if (it.node()->state() == Node::PENDING) { |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 621 | *stats->pending_global_handle_count += 1; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 622 | } else if (it.node()->state() == Node::NEAR_DEATH) { |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 623 | *stats->near_death_global_handle_count += 1; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 624 | } else if (it.node()->state() == Node::FREE) { |
| 625 | *stats->free_global_handle_count += 1; |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 626 | } |
| 627 | } |
| 628 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 629 | |
| 630 | #ifdef DEBUG |
| 631 | |
| 632 | void GlobalHandles::PrintStats() { |
| 633 | int total = 0; |
| 634 | int weak = 0; |
| 635 | int pending = 0; |
| 636 | int near_death = 0; |
| 637 | int destroyed = 0; |
| 638 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 639 | for (NodeIterator it(this); !it.done(); it.Advance()) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 640 | total++; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 641 | if (it.node()->state() == Node::WEAK) weak++; |
| 642 | if (it.node()->state() == Node::PENDING) pending++; |
| 643 | if (it.node()->state() == Node::NEAR_DEATH) near_death++; |
| 644 | if (it.node()->state() == Node::FREE) destroyed++; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 645 | } |
| 646 | |
| 647 | PrintF("Global Handle Statistics:\n"); |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 648 | PrintF(" allocated memory = %" V8_PTR_PREFIX "dB\n", sizeof(Node) * total); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 649 | PrintF(" # weak = %d\n", weak); |
| 650 | PrintF(" # pending = %d\n", pending); |
| 651 | PrintF(" # near_death = %d\n", near_death); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 652 | PrintF(" # free = %d\n", destroyed); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 653 | PrintF(" # total = %d\n", total); |
| 654 | } |
| 655 | |
| 656 | void GlobalHandles::Print() { |
| 657 | PrintF("Global handles:\n"); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 658 | for (NodeIterator it(this); !it.done(); it.Advance()) { |
| 659 | PrintF(" handle %p to %p%s\n", |
| 660 | reinterpret_cast<void*>(it.node()->location()), |
| 661 | reinterpret_cast<void*>(it.node()->object()), |
| 662 | it.node()->IsWeak() ? " (weak)" : ""); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 663 | } |
| 664 | } |
| 665 | |
| 666 | #endif |
| 667 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 668 | |
| 669 | |
| 670 | void GlobalHandles::AddObjectGroup(Object*** handles, |
| 671 | size_t length, |
| 672 | v8::RetainedObjectInfo* info) { |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 673 | #ifdef DEBUG |
| 674 | for (size_t i = 0; i < length; ++i) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 675 | ASSERT(!Node::FromLocation(handles[i])->is_independent()); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 676 | } |
| 677 | #endif |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 678 | if (length == 0) { |
| 679 | if (info != NULL) info->Dispose(); |
| 680 | return; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 681 | } |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 682 | object_groups_.Add(ObjectGroup::New(handles, length, info)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 683 | } |
| 684 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 685 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 686 | void GlobalHandles::AddImplicitReferences(HeapObject** parent, |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 687 | Object*** children, |
| 688 | size_t length) { |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 689 | #ifdef DEBUG |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 690 | ASSERT(!Node::FromLocation(BitCast<Object**>(parent))->is_independent()); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 691 | for (size_t i = 0; i < length; ++i) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 692 | ASSERT(!Node::FromLocation(children[i])->is_independent()); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 693 | } |
| 694 | #endif |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 695 | if (length == 0) return; |
| 696 | implicit_ref_groups_.Add(ImplicitRefGroup::New(parent, children, length)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 697 | } |
| 698 | |
| 699 | |
| 700 | void GlobalHandles::RemoveObjectGroups() { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 701 | for (int i = 0; i < object_groups_.length(); i++) { |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 702 | object_groups_.at(i)->Dispose(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 703 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 704 | object_groups_.Clear(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 705 | } |
| 706 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 707 | |
| 708 | void GlobalHandles::RemoveImplicitRefGroups() { |
| 709 | for (int i = 0; i < implicit_ref_groups_.length(); i++) { |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 710 | implicit_ref_groups_.at(i)->Dispose(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 711 | } |
| 712 | implicit_ref_groups_.Clear(); |
| 713 | } |
| 714 | |
| 715 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 716 | void GlobalHandles::TearDown() { |
| 717 | // TODO(1428): invoke weak callbacks. |
| 718 | } |
| 719 | |
| 720 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 721 | } } // namespace v8::internal |