ager@chromium.org | 71daaf6 | 2009-04-01 07:22:49 +0000 | [diff] [blame] | 1 | // Copyright 2009 the V8 project authors. All rights reserved. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 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 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 33 | namespace v8 { |
| 34 | namespace internal { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 35 | |
| 36 | class GlobalHandles::Node : public Malloced { |
| 37 | public: |
| 38 | |
| 39 | void Initialize(Object* object) { |
| 40 | // Set the initial value of the handle. |
| 41 | object_ = object; |
| 42 | state_ = NORMAL; |
| 43 | parameter_or_next_free_.parameter = NULL; |
| 44 | callback_ = NULL; |
| 45 | } |
| 46 | |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 47 | Node() { |
| 48 | state_ = DESTROYED; |
| 49 | } |
| 50 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 51 | explicit Node(Object* object) { |
| 52 | Initialize(object); |
| 53 | // Initialize link structure. |
| 54 | next_ = NULL; |
| 55 | } |
| 56 | |
| 57 | ~Node() { |
| 58 | if (state_ != DESTROYED) Destroy(); |
| 59 | #ifdef DEBUG |
| 60 | // Zap the values for eager trapping. |
| 61 | object_ = NULL; |
| 62 | next_ = NULL; |
| 63 | parameter_or_next_free_.next_free = NULL; |
| 64 | #endif |
| 65 | } |
| 66 | |
| 67 | void Destroy() { |
| 68 | if (state_ == WEAK || IsNearDeath()) { |
| 69 | GlobalHandles::number_of_weak_handles_--; |
| 70 | if (object_->IsJSGlobalObject()) { |
| 71 | GlobalHandles::number_of_global_object_weak_handles_--; |
| 72 | } |
| 73 | } |
| 74 | state_ = DESTROYED; |
| 75 | } |
| 76 | |
| 77 | // Accessors for next_. |
| 78 | Node* next() { return next_; } |
| 79 | void set_next(Node* value) { next_ = value; } |
| 80 | Node** next_addr() { return &next_; } |
| 81 | |
| 82 | // Accessors for next free node in the free list. |
| 83 | Node* next_free() { |
| 84 | ASSERT(state_ == DESTROYED); |
| 85 | return parameter_or_next_free_.next_free; |
| 86 | } |
| 87 | void set_next_free(Node* value) { |
| 88 | ASSERT(state_ == DESTROYED); |
| 89 | parameter_or_next_free_.next_free = value; |
| 90 | } |
| 91 | |
| 92 | // Returns a link from the handle. |
| 93 | static Node* FromLocation(Object** location) { |
| 94 | ASSERT(OFFSET_OF(Node, object_) == 0); |
| 95 | return reinterpret_cast<Node*>(location); |
| 96 | } |
| 97 | |
| 98 | // Returns the handle. |
| 99 | Handle<Object> handle() { return Handle<Object>(&object_); } |
| 100 | |
| 101 | // Make this handle weak. |
| 102 | void MakeWeak(void* parameter, WeakReferenceCallback callback) { |
| 103 | LOG(HandleEvent("GlobalHandle::MakeWeak", handle().location())); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 104 | ASSERT(state_ != DESTROYED); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 105 | if (state_ != WEAK && !IsNearDeath()) { |
| 106 | GlobalHandles::number_of_weak_handles_++; |
| 107 | if (object_->IsJSGlobalObject()) { |
| 108 | GlobalHandles::number_of_global_object_weak_handles_++; |
| 109 | } |
| 110 | } |
| 111 | state_ = WEAK; |
| 112 | set_parameter(parameter); |
| 113 | callback_ = callback; |
| 114 | } |
| 115 | |
| 116 | void ClearWeakness() { |
| 117 | LOG(HandleEvent("GlobalHandle::ClearWeakness", handle().location())); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 118 | ASSERT(state_ != DESTROYED); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 119 | if (state_ == WEAK || IsNearDeath()) { |
| 120 | GlobalHandles::number_of_weak_handles_--; |
| 121 | if (object_->IsJSGlobalObject()) { |
| 122 | GlobalHandles::number_of_global_object_weak_handles_--; |
| 123 | } |
| 124 | } |
| 125 | state_ = NORMAL; |
| 126 | set_parameter(NULL); |
| 127 | } |
| 128 | |
| 129 | bool IsNearDeath() { |
| 130 | // Check for PENDING to ensure correct answer when processing callbacks. |
| 131 | return state_ == PENDING || state_ == NEAR_DEATH; |
| 132 | } |
| 133 | |
| 134 | bool IsWeak() { |
| 135 | return state_ == WEAK; |
| 136 | } |
| 137 | |
| 138 | // Returns the id for this weak handle. |
| 139 | void set_parameter(void* parameter) { |
| 140 | ASSERT(state_ != DESTROYED); |
| 141 | parameter_or_next_free_.parameter = parameter; |
| 142 | } |
| 143 | void* parameter() { |
| 144 | ASSERT(state_ != DESTROYED); |
| 145 | return parameter_or_next_free_.parameter; |
| 146 | } |
| 147 | |
| 148 | // Returns the callback for this weak handle. |
| 149 | WeakReferenceCallback callback() { return callback_; } |
| 150 | |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 151 | bool PostGarbageCollectionProcessing() { |
| 152 | if (state_ != Node::PENDING) return false; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 153 | LOG(HandleEvent("GlobalHandle::Processing", handle().location())); |
fschneider@chromium.org | ed78ffd | 2010-07-21 11:05:19 +0000 | [diff] [blame] | 154 | WeakReferenceCallback func = callback(); |
| 155 | if (func == NULL) { |
| 156 | Destroy(); |
| 157 | return false; |
| 158 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 159 | void* par = parameter(); |
| 160 | state_ = NEAR_DEATH; |
| 161 | set_parameter(NULL); |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 162 | |
| 163 | v8::Persistent<v8::Object> object = ToApi<v8::Object>(handle()); |
| 164 | { |
| 165 | // Forbid reuse of destroyed nodes as they might be already deallocated. |
| 166 | // It's fine though to reuse nodes that were destroyed in weak callback |
| 167 | // as those cannot be deallocated until we are back from the callback. |
| 168 | set_first_free(NULL); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 169 | if (first_deallocated()) { |
| 170 | first_deallocated()->set_next(head()); |
| 171 | } |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 172 | // Check that we are not passing a finalized external string to |
| 173 | // the callback. |
| 174 | ASSERT(!object_->IsExternalAsciiString() || |
| 175 | ExternalAsciiString::cast(object_)->resource() != NULL); |
| 176 | ASSERT(!object_->IsExternalTwoByteString() || |
| 177 | ExternalTwoByteString::cast(object_)->resource() != NULL); |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 178 | // Leaving V8. |
| 179 | VMState state(EXTERNAL); |
| 180 | func(object, par); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 181 | } |
fschneider@chromium.org | ed78ffd | 2010-07-21 11:05:19 +0000 | [diff] [blame] | 182 | // Absense of explicit cleanup or revival of weak handle |
| 183 | // in most of the cases would lead to memory leak. |
| 184 | ASSERT(state_ != NEAR_DEATH); |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 185 | return true; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | // Place the handle address first to avoid offset computation. |
| 189 | Object* object_; // Storage for object pointer. |
| 190 | |
| 191 | // Transition diagram: |
| 192 | // NORMAL <-> WEAK -> PENDING -> NEAR_DEATH -> { NORMAL, WEAK, DESTROYED } |
| 193 | enum State { |
| 194 | NORMAL, // Normal global handle. |
| 195 | WEAK, // Flagged as weak but not yet finalized. |
| 196 | PENDING, // Has been recognized as only reachable by weak handles. |
| 197 | NEAR_DEATH, // Callback has informed the handle is near death. |
| 198 | DESTROYED |
| 199 | }; |
| 200 | State state_; |
| 201 | |
| 202 | private: |
| 203 | // Handle specific callback. |
| 204 | WeakReferenceCallback callback_; |
| 205 | // Provided data for callback. In DESTROYED state, this is used for |
| 206 | // the free list link. |
| 207 | union { |
| 208 | void* parameter; |
| 209 | Node* next_free; |
| 210 | } parameter_or_next_free_; |
| 211 | |
| 212 | // Linkage for the list. |
| 213 | Node* next_; |
| 214 | |
| 215 | public: |
| 216 | TRACK_MEMORY("GlobalHandles::Node") |
| 217 | }; |
| 218 | |
| 219 | |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 220 | class GlobalHandles::Pool BASE_EMBEDDED { |
| 221 | public: |
| 222 | Pool() { |
| 223 | current_ = new Chunk(); |
| 224 | current_->previous = NULL; |
| 225 | next_ = current_->nodes; |
| 226 | limit_ = current_->nodes + kNodesPerChunk; |
| 227 | } |
| 228 | |
whesse@chromium.org | e90029b | 2010-08-02 11:52:17 +0000 | [diff] [blame] | 229 | ~Pool() { |
| 230 | if (current_ != NULL) { |
| 231 | Release(); |
| 232 | } |
| 233 | } |
| 234 | |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 235 | Node* Allocate() { |
| 236 | if (next_ < limit_) { |
| 237 | return next_++; |
| 238 | } |
| 239 | return SlowAllocate(); |
| 240 | } |
| 241 | |
| 242 | void Release() { |
| 243 | Chunk* current = current_; |
| 244 | ASSERT(current != NULL); // At least a single block must by allocated |
| 245 | do { |
| 246 | Chunk* previous = current->previous; |
| 247 | delete current; |
| 248 | current = previous; |
| 249 | } while (current != NULL); |
| 250 | current_ = NULL; |
| 251 | next_ = limit_ = NULL; |
| 252 | } |
| 253 | |
| 254 | private: |
| 255 | static const int kNodesPerChunk = (1 << 12) - 1; |
| 256 | struct Chunk : public Malloced { |
| 257 | Chunk* previous; |
| 258 | Node nodes[kNodesPerChunk]; |
| 259 | }; |
| 260 | |
| 261 | Node* SlowAllocate() { |
| 262 | Chunk* chunk = new Chunk(); |
| 263 | chunk->previous = current_; |
| 264 | current_ = chunk; |
| 265 | |
| 266 | Node* new_nodes = current_->nodes; |
| 267 | next_ = new_nodes + 1; |
| 268 | limit_ = new_nodes + kNodesPerChunk; |
| 269 | return new_nodes; |
| 270 | } |
| 271 | |
| 272 | Chunk* current_; |
| 273 | Node* next_; |
| 274 | Node* limit_; |
| 275 | }; |
| 276 | |
| 277 | |
| 278 | static GlobalHandles::Pool pool_; |
| 279 | |
| 280 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 281 | Handle<Object> GlobalHandles::Create(Object* value) { |
| 282 | Counters::global_handles.Increment(); |
| 283 | Node* result; |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 284 | if (first_free()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 285 | // Take the first node in the free list. |
| 286 | result = first_free(); |
| 287 | set_first_free(result->next_free()); |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 288 | } else if (first_deallocated()) { |
| 289 | // Next try deallocated list |
| 290 | result = first_deallocated(); |
| 291 | set_first_deallocated(result->next_free()); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 292 | ASSERT(result->next() == head()); |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 293 | set_head(result); |
| 294 | } else { |
| 295 | // Allocate a new node. |
| 296 | result = pool_.Allocate(); |
| 297 | result->set_next(head()); |
| 298 | set_head(result); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 299 | } |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 300 | result->Initialize(value); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 301 | return result->handle(); |
| 302 | } |
| 303 | |
| 304 | |
| 305 | void GlobalHandles::Destroy(Object** location) { |
| 306 | Counters::global_handles.Decrement(); |
| 307 | if (location == NULL) return; |
| 308 | Node* node = Node::FromLocation(location); |
| 309 | node->Destroy(); |
| 310 | // Link the destroyed. |
| 311 | node->set_next_free(first_free()); |
| 312 | set_first_free(node); |
| 313 | } |
| 314 | |
| 315 | |
| 316 | void GlobalHandles::MakeWeak(Object** location, void* parameter, |
| 317 | WeakReferenceCallback callback) { |
| 318 | ASSERT(callback != NULL); |
| 319 | Node::FromLocation(location)->MakeWeak(parameter, callback); |
| 320 | } |
| 321 | |
| 322 | |
| 323 | void GlobalHandles::ClearWeakness(Object** location) { |
| 324 | Node::FromLocation(location)->ClearWeakness(); |
| 325 | } |
| 326 | |
| 327 | |
| 328 | bool GlobalHandles::IsNearDeath(Object** location) { |
| 329 | return Node::FromLocation(location)->IsNearDeath(); |
| 330 | } |
| 331 | |
| 332 | |
| 333 | bool GlobalHandles::IsWeak(Object** location) { |
| 334 | return Node::FromLocation(location)->IsWeak(); |
| 335 | } |
| 336 | |
| 337 | |
| 338 | void GlobalHandles::IterateWeakRoots(ObjectVisitor* v) { |
| 339 | // Traversal of GC roots in the global handle list that are marked as |
| 340 | // WEAK or PENDING. |
| 341 | for (Node* current = head_; current != NULL; current = current->next()) { |
| 342 | if (current->state_ == Node::WEAK |
| 343 | || current->state_ == Node::PENDING |
| 344 | || current->state_ == Node::NEAR_DEATH) { |
| 345 | v->VisitPointer(¤t->object_); |
| 346 | } |
| 347 | } |
| 348 | } |
| 349 | |
| 350 | |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 351 | void GlobalHandles::IterateWeakRoots(WeakReferenceGuest f, |
| 352 | WeakReferenceCallback callback) { |
| 353 | for (Node* current = head_; current != NULL; current = current->next()) { |
| 354 | if (current->IsWeak() && current->callback() == callback) { |
| 355 | f(current->object_, current->parameter()); |
| 356 | } |
| 357 | } |
| 358 | } |
| 359 | |
| 360 | |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 361 | void GlobalHandles::IdentifyWeakHandles(WeakSlotCallback f) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 362 | for (Node* current = head_; current != NULL; current = current->next()) { |
| 363 | if (current->state_ == Node::WEAK) { |
| 364 | if (f(¤t->object_)) { |
| 365 | current->state_ = Node::PENDING; |
| 366 | LOG(HandleEvent("GlobalHandle::Pending", current->handle().location())); |
| 367 | } |
| 368 | } |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 373 | int post_gc_processing_count = 0; |
| 374 | |
erik.corry@gmail.com | d88afa2 | 2010-09-15 12:33:05 +0000 | [diff] [blame] | 375 | bool GlobalHandles::PostGarbageCollectionProcessing() { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 376 | // Process weak global handle callbacks. This must be done after the |
| 377 | // GC is completely done, because the callbacks may invoke arbitrary |
| 378 | // API functions. |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 379 | // At the same time deallocate all DESTROYED nodes. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 380 | ASSERT(Heap::gc_state() == Heap::NOT_IN_GC); |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 381 | const int initial_post_gc_processing_count = ++post_gc_processing_count; |
erik.corry@gmail.com | d88afa2 | 2010-09-15 12:33:05 +0000 | [diff] [blame] | 382 | bool weak_callback_invoked = false; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 383 | Node** p = &head_; |
| 384 | while (*p != NULL) { |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 385 | if ((*p)->PostGarbageCollectionProcessing()) { |
| 386 | if (initial_post_gc_processing_count != post_gc_processing_count) { |
| 387 | // Weak callback triggered another GC and another round of |
| 388 | // PostGarbageCollection processing. The current node might |
| 389 | // have been deleted in that round, so we need to bail out (or |
| 390 | // restart the processing). |
| 391 | break; |
| 392 | } |
erik.corry@gmail.com | d88afa2 | 2010-09-15 12:33:05 +0000 | [diff] [blame] | 393 | weak_callback_invoked = true; |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 394 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 395 | if ((*p)->state_ == Node::DESTROYED) { |
| 396 | // Delete the link. |
| 397 | Node* node = *p; |
| 398 | *p = node->next(); // Update the link. |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 399 | if (first_deallocated()) { |
| 400 | first_deallocated()->set_next(node); |
| 401 | } |
| 402 | node->set_next_free(first_deallocated()); |
| 403 | set_first_deallocated(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 404 | } else { |
| 405 | p = (*p)->next_addr(); |
| 406 | } |
| 407 | } |
| 408 | set_first_free(NULL); |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 409 | if (first_deallocated()) { |
| 410 | first_deallocated()->set_next(head()); |
| 411 | } |
erik.corry@gmail.com | d88afa2 | 2010-09-15 12:33:05 +0000 | [diff] [blame] | 412 | return weak_callback_invoked; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 416 | void GlobalHandles::IterateStrongRoots(ObjectVisitor* v) { |
| 417 | // Traversal of global handles marked as NORMAL. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 418 | for (Node* current = head_; current != NULL; current = current->next()) { |
| 419 | if (current->state_ == Node::NORMAL) { |
| 420 | v->VisitPointer(¤t->object_); |
| 421 | } |
| 422 | } |
| 423 | } |
| 424 | |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 425 | |
| 426 | void GlobalHandles::IterateAllRoots(ObjectVisitor* v) { |
| 427 | for (Node* current = head_; current != NULL; current = current->next()) { |
| 428 | if (current->state_ != Node::DESTROYED) { |
| 429 | v->VisitPointer(¤t->object_); |
| 430 | } |
| 431 | } |
| 432 | } |
| 433 | |
| 434 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 435 | void GlobalHandles::TearDown() { |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 436 | // Reset all the lists. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 437 | set_head(NULL); |
| 438 | set_first_free(NULL); |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 439 | set_first_deallocated(NULL); |
| 440 | pool_.Release(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 441 | } |
| 442 | |
| 443 | |
| 444 | int GlobalHandles::number_of_weak_handles_ = 0; |
| 445 | int GlobalHandles::number_of_global_object_weak_handles_ = 0; |
| 446 | |
| 447 | GlobalHandles::Node* GlobalHandles::head_ = NULL; |
| 448 | GlobalHandles::Node* GlobalHandles::first_free_ = NULL; |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 449 | GlobalHandles::Node* GlobalHandles::first_deallocated_ = NULL; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 450 | |
ager@chromium.org | 6012123 | 2009-12-03 11:25:37 +0000 | [diff] [blame] | 451 | void GlobalHandles::RecordStats(HeapStats* stats) { |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 452 | *stats->global_handle_count = 0; |
| 453 | *stats->weak_global_handle_count = 0; |
| 454 | *stats->pending_global_handle_count = 0; |
| 455 | *stats->near_death_global_handle_count = 0; |
| 456 | *stats->destroyed_global_handle_count = 0; |
ager@chromium.org | 6012123 | 2009-12-03 11:25:37 +0000 | [diff] [blame] | 457 | for (Node* current = head_; current != NULL; current = current->next()) { |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 458 | *stats->global_handle_count += 1; |
ager@chromium.org | 6012123 | 2009-12-03 11:25:37 +0000 | [diff] [blame] | 459 | if (current->state_ == Node::WEAK) { |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 460 | *stats->weak_global_handle_count += 1; |
ager@chromium.org | 6012123 | 2009-12-03 11:25:37 +0000 | [diff] [blame] | 461 | } else if (current->state_ == Node::PENDING) { |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 462 | *stats->pending_global_handle_count += 1; |
ager@chromium.org | 6012123 | 2009-12-03 11:25:37 +0000 | [diff] [blame] | 463 | } else if (current->state_ == Node::NEAR_DEATH) { |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 464 | *stats->near_death_global_handle_count += 1; |
ager@chromium.org | 6012123 | 2009-12-03 11:25:37 +0000 | [diff] [blame] | 465 | } else if (current->state_ == Node::DESTROYED) { |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 466 | *stats->destroyed_global_handle_count += 1; |
ager@chromium.org | 6012123 | 2009-12-03 11:25:37 +0000 | [diff] [blame] | 467 | } |
| 468 | } |
| 469 | } |
| 470 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 471 | #ifdef DEBUG |
| 472 | |
| 473 | void GlobalHandles::PrintStats() { |
| 474 | int total = 0; |
| 475 | int weak = 0; |
| 476 | int pending = 0; |
| 477 | int near_death = 0; |
| 478 | int destroyed = 0; |
| 479 | |
| 480 | for (Node* current = head_; current != NULL; current = current->next()) { |
| 481 | total++; |
| 482 | if (current->state_ == Node::WEAK) weak++; |
| 483 | if (current->state_ == Node::PENDING) pending++; |
| 484 | if (current->state_ == Node::NEAR_DEATH) near_death++; |
| 485 | if (current->state_ == Node::DESTROYED) destroyed++; |
| 486 | } |
| 487 | |
| 488 | PrintF("Global Handle Statistics:\n"); |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame^] | 489 | PrintF(" allocated memory = %" V8_PTR_PREFIX "dB\n", sizeof(Node) * total); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 490 | PrintF(" # weak = %d\n", weak); |
| 491 | PrintF(" # pending = %d\n", pending); |
| 492 | PrintF(" # near_death = %d\n", near_death); |
| 493 | PrintF(" # destroyed = %d\n", destroyed); |
| 494 | PrintF(" # total = %d\n", total); |
| 495 | } |
| 496 | |
| 497 | void GlobalHandles::Print() { |
| 498 | PrintF("Global handles:\n"); |
| 499 | for (Node* current = head_; current != NULL; current = current->next()) { |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame^] | 500 | PrintF(" handle %p to %p (weak=%d)\n", |
| 501 | reinterpret_cast<void*>(current->handle().location()), |
| 502 | reinterpret_cast<void*>(*current->handle()), |
| 503 | current->state_ == Node::WEAK); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 504 | } |
| 505 | } |
| 506 | |
| 507 | #endif |
| 508 | |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 509 | List<ObjectGroup*>* GlobalHandles::ObjectGroups() { |
| 510 | // Lazily initialize the list to avoid startup time static constructors. |
| 511 | static List<ObjectGroup*> groups(4); |
| 512 | return &groups; |
| 513 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 514 | |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 515 | void GlobalHandles::AddGroup(Object*** handles, size_t length) { |
| 516 | ObjectGroup* new_entry = new ObjectGroup(length); |
| 517 | for (size_t i = 0; i < length; ++i) |
| 518 | new_entry->objects_.Add(handles[i]); |
| 519 | ObjectGroups()->Add(new_entry); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 520 | } |
| 521 | |
| 522 | |
| 523 | void GlobalHandles::RemoveObjectGroups() { |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 524 | List<ObjectGroup*>* object_groups = ObjectGroups(); |
| 525 | for (int i = 0; i< object_groups->length(); i++) { |
| 526 | delete object_groups->at(i); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 527 | } |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 528 | object_groups->Clear(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 529 | } |
| 530 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 531 | } } // namespace v8::internal |