| OLD | NEW |
| 1 // Copyright 2006-2008 the V8 project authors. All rights reserved. | 1 // Copyright 2006-2008 the V8 project authors. All rights reserved. |
| 2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
| 3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
| 4 // met: | 4 // met: |
| 5 // | 5 // |
| 6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
| 7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
| 8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
| 9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
| 10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
| (...skipping 281 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 292 | 292 |
| 293 | 293 |
| 294 static int NumberOfWeakCalls = 0; | 294 static int NumberOfWeakCalls = 0; |
| 295 static void WeakPointerCallback(v8::Persistent<v8::Value> handle, void* id) { | 295 static void WeakPointerCallback(v8::Persistent<v8::Value> handle, void* id) { |
| 296 ASSERT(id == reinterpret_cast<void*>(1234)); | 296 ASSERT(id == reinterpret_cast<void*>(1234)); |
| 297 NumberOfWeakCalls++; | 297 NumberOfWeakCalls++; |
| 298 handle.Dispose(); | 298 handle.Dispose(); |
| 299 } | 299 } |
| 300 | 300 |
| 301 TEST(ObjectGroups) { | 301 TEST(ObjectGroups) { |
| 302 InitializeVM(); |
| 302 GlobalHandles* global_handles = Isolate::Current()->global_handles(); | 303 GlobalHandles* global_handles = Isolate::Current()->global_handles(); |
| 303 InitializeVM(); | |
| 304 | 304 |
| 305 NumberOfWeakCalls = 0; | 305 NumberOfWeakCalls = 0; |
| 306 v8::HandleScope handle_scope; | 306 v8::HandleScope handle_scope; |
| 307 | 307 |
| 308 Handle<Object> g1s1 = | 308 Handle<Object> g1s1 = |
| 309 global_handles->Create(HEAP->AllocateFixedArray(1)->ToObjectChecked()); | 309 global_handles->Create(HEAP->AllocateFixedArray(1)->ToObjectChecked()); |
| 310 Handle<Object> g1s2 = | 310 Handle<Object> g1s2 = |
| 311 global_handles->Create(HEAP->AllocateFixedArray(1)->ToObjectChecked()); | 311 global_handles->Create(HEAP->AllocateFixedArray(1)->ToObjectChecked()); |
| 312 Handle<Object> g1c1 = | 312 Handle<Object> g1c1 = |
| 313 global_handles->Create(HEAP->AllocateFixedArray(1)->ToObjectChecked()); | 313 global_handles->Create(HEAP->AllocateFixedArray(1)->ToObjectChecked()); |
| 314 global_handles->MakeWeak(g1s1.location(), | 314 global_handles->MakeWeak(g1s1.location(), |
| 315 reinterpret_cast<void*>(1234), | 315 reinterpret_cast<void*>(1234), |
| 316 &WeakPointerCallback); | 316 &WeakPointerCallback); |
| 317 global_handles->MakeWeak(g1s2.location(), | 317 global_handles->MakeWeak(g1s2.location(), |
| 318 reinterpret_cast<void*>(1234), | 318 reinterpret_cast<void*>(1234), |
| 319 &WeakPointerCallback); | 319 &WeakPointerCallback); |
| 320 global_handles->MakeWeak(g1c1.location(), | 320 global_handles->MakeWeak(g1c1.location(), |
| 321 reinterpret_cast<void*>(1234), | 321 reinterpret_cast<void*>(1234), |
| 322 &WeakPointerCallback); | 322 &WeakPointerCallback); |
| 323 | 323 |
| (...skipping 18 matching lines...) Expand all Loading... |
| 342 // Connect group 1 and 2, make a cycle. | 342 // Connect group 1 and 2, make a cycle. |
| 343 Handle<FixedArray>::cast(g1s2)->set(0, *g2s2); | 343 Handle<FixedArray>::cast(g1s2)->set(0, *g2s2); |
| 344 Handle<FixedArray>::cast(g2s1)->set(0, *g1s1); | 344 Handle<FixedArray>::cast(g2s1)->set(0, *g1s1); |
| 345 | 345 |
| 346 { | 346 { |
| 347 Object** g1_objects[] = { g1s1.location(), g1s2.location() }; | 347 Object** g1_objects[] = { g1s1.location(), g1s2.location() }; |
| 348 Object** g1_children[] = { g1c1.location() }; | 348 Object** g1_children[] = { g1c1.location() }; |
| 349 Object** g2_objects[] = { g2s1.location(), g2s2.location() }; | 349 Object** g2_objects[] = { g2s1.location(), g2s2.location() }; |
| 350 Object** g2_children[] = { g2c1.location() }; | 350 Object** g2_children[] = { g2c1.location() }; |
| 351 global_handles->AddObjectGroup(g1_objects, 2, NULL); | 351 global_handles->AddObjectGroup(g1_objects, 2, NULL); |
| 352 global_handles->AddImplicitReferences(HeapObject::cast(*g1s1), | 352 global_handles->AddImplicitReferences( |
| 353 g1_children, 1); | 353 Handle<HeapObject>::cast(g1s1).location(), g1_children, 1); |
| 354 global_handles->AddObjectGroup(g2_objects, 2, NULL); | 354 global_handles->AddObjectGroup(g2_objects, 2, NULL); |
| 355 global_handles->AddImplicitReferences(HeapObject::cast(*g2s2), | 355 global_handles->AddImplicitReferences( |
| 356 g2_children, 1); | 356 Handle<HeapObject>::cast(g2s2).location(), g2_children, 1); |
| 357 } | 357 } |
| 358 // Do a full GC | 358 // Do a full GC |
| 359 HEAP->CollectGarbage(OLD_POINTER_SPACE); | 359 HEAP->CollectGarbage(OLD_POINTER_SPACE); |
| 360 | 360 |
| 361 // All object should be alive. | 361 // All object should be alive. |
| 362 CHECK_EQ(0, NumberOfWeakCalls); | 362 CHECK_EQ(0, NumberOfWeakCalls); |
| 363 | 363 |
| 364 // Weaken the root. | 364 // Weaken the root. |
| 365 global_handles->MakeWeak(root.location(), | 365 global_handles->MakeWeak(root.location(), |
| 366 reinterpret_cast<void*>(1234), | 366 reinterpret_cast<void*>(1234), |
| 367 &WeakPointerCallback); | 367 &WeakPointerCallback); |
| 368 // But make children strong roots---all the objects (except for children) | 368 // But make children strong roots---all the objects (except for children) |
| 369 // should be collectable now. | 369 // should be collectable now. |
| 370 global_handles->ClearWeakness(g1c1.location()); | 370 global_handles->ClearWeakness(g1c1.location()); |
| 371 global_handles->ClearWeakness(g2c1.location()); | 371 global_handles->ClearWeakness(g2c1.location()); |
| 372 | 372 |
| 373 // Groups are deleted, rebuild groups. | 373 // Groups are deleted, rebuild groups. |
| 374 { | 374 { |
| 375 Object** g1_objects[] = { g1s1.location(), g1s2.location() }; | 375 Object** g1_objects[] = { g1s1.location(), g1s2.location() }; |
| 376 Object** g1_children[] = { g1c1.location() }; | 376 Object** g1_children[] = { g1c1.location() }; |
| 377 Object** g2_objects[] = { g2s1.location(), g2s2.location() }; | 377 Object** g2_objects[] = { g2s1.location(), g2s2.location() }; |
| 378 Object** g2_children[] = { g2c1.location() }; | 378 Object** g2_children[] = { g2c1.location() }; |
| 379 global_handles->AddObjectGroup(g1_objects, 2, NULL); | 379 global_handles->AddObjectGroup(g1_objects, 2, NULL); |
| 380 global_handles->AddImplicitReferences(HeapObject::cast(*g1s1), | 380 global_handles->AddImplicitReferences( |
| 381 g1_children, 1); | 381 Handle<HeapObject>::cast(g1s1).location(), g1_children, 1); |
| 382 global_handles->AddObjectGroup(g2_objects, 2, NULL); | 382 global_handles->AddObjectGroup(g2_objects, 2, NULL); |
| 383 global_handles->AddImplicitReferences(HeapObject::cast(*g2s2), | 383 global_handles->AddImplicitReferences( |
| 384 g2_children, 1); | 384 Handle<HeapObject>::cast(g2s2).location(), g2_children, 1); |
| 385 } | 385 } |
| 386 | 386 |
| 387 HEAP->CollectGarbage(OLD_POINTER_SPACE); | 387 HEAP->CollectGarbage(OLD_POINTER_SPACE); |
| 388 | 388 |
| 389 // All objects should be gone. 5 global handles in total. | 389 // All objects should be gone. 5 global handles in total. |
| 390 CHECK_EQ(5, NumberOfWeakCalls); | 390 CHECK_EQ(5, NumberOfWeakCalls); |
| 391 | 391 |
| 392 // And now make children weak again and collect them. | 392 // And now make children weak again and collect them. |
| 393 global_handles->MakeWeak(g1c1.location(), | 393 global_handles->MakeWeak(g1c1.location(), |
| 394 reinterpret_cast<void*>(1234), | 394 reinterpret_cast<void*>(1234), |
| 395 &WeakPointerCallback); | 395 &WeakPointerCallback); |
| 396 global_handles->MakeWeak(g2c1.location(), | 396 global_handles->MakeWeak(g2c1.location(), |
| 397 reinterpret_cast<void*>(1234), | 397 reinterpret_cast<void*>(1234), |
| 398 &WeakPointerCallback); | 398 &WeakPointerCallback); |
| 399 | 399 |
| 400 HEAP->CollectGarbage(OLD_POINTER_SPACE); | 400 HEAP->CollectGarbage(OLD_POINTER_SPACE); |
| 401 CHECK_EQ(7, NumberOfWeakCalls); | 401 CHECK_EQ(7, NumberOfWeakCalls); |
| 402 } | 402 } |
| 403 |
| 404 |
| 405 class TestRetainedObjectInfo : public v8::RetainedObjectInfo { |
| 406 public: |
| 407 TestRetainedObjectInfo() : has_been_disposed_(false) {} |
| 408 |
| 409 bool has_been_disposed() { return has_been_disposed_; } |
| 410 |
| 411 virtual void Dispose() { |
| 412 ASSERT(!has_been_disposed_); |
| 413 has_been_disposed_ = true; |
| 414 } |
| 415 |
| 416 virtual bool IsEquivalent(v8::RetainedObjectInfo* other) { |
| 417 return other == this; |
| 418 } |
| 419 |
| 420 virtual intptr_t GetHash() { return 0; }; |
| 421 |
| 422 virtual const char* GetLabel() { return "whatever"; } |
| 423 |
| 424 private: |
| 425 bool has_been_disposed_; |
| 426 }; |
| 427 |
| 428 |
| 429 TEST(EmptyObjectGroups) { |
| 430 InitializeVM(); |
| 431 GlobalHandles* global_handles = Isolate::Current()->global_handles(); |
| 432 |
| 433 v8::HandleScope handle_scope; |
| 434 |
| 435 Handle<Object> object = |
| 436 global_handles->Create(HEAP->AllocateFixedArray(1)->ToObjectChecked()); |
| 437 |
| 438 TestRetainedObjectInfo info; |
| 439 global_handles->AddObjectGroup(NULL, 0, &info); |
| 440 ASSERT(info.has_been_disposed()); |
| 441 |
| 442 global_handles->AddImplicitReferences( |
| 443 Handle<HeapObject>::cast(object).location(), NULL, 0); |
| 444 } |
| OLD | NEW |