OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #import "chrome/common/mac/objc_zombie.h" | 5 #import "chrome/common/mac/objc_zombie.h" |
6 | 6 |
7 #include <dlfcn.h> | 7 #include <dlfcn.h> |
8 #include <execinfo.h> | 8 #include <execinfo.h> |
9 #include <mach-o/dyld.h> | 9 #include <mach-o/dyld.h> |
10 #include <mach-o/nlist.h> | 10 #include <mach-o/nlist.h> |
(...skipping 432 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
443 } | 443 } |
444 | 444 |
445 @end | 445 @end |
446 | 446 |
447 namespace ObjcEvilDoers { | 447 namespace ObjcEvilDoers { |
448 | 448 |
449 bool ZombieEnable(bool zombieAllObjects, | 449 bool ZombieEnable(bool zombieAllObjects, |
450 size_t zombieCount) { | 450 size_t zombieCount) { |
451 // Only allow enable/disable on the main thread, just to keep things | 451 // Only allow enable/disable on the main thread, just to keep things |
452 // simple. | 452 // simple. |
453 CHECK([NSThread isMainThread]); | 453 DCHECK([NSThread isMainThread]); |
454 | 454 |
455 if (!ZombieInit()) | 455 if (!ZombieInit()) |
456 return false; | 456 return false; |
457 | 457 |
458 g_zombieAllObjects = zombieAllObjects; | 458 g_zombieAllObjects = zombieAllObjects; |
459 | 459 |
460 // Replace the implementation of -[NSObject dealloc]. | 460 // Replace the implementation of -[NSObject dealloc]. |
461 Method m = class_getInstanceMethod([NSObject class], @selector(dealloc)); | 461 Method m = class_getInstanceMethod([NSObject class], @selector(dealloc)); |
462 if (!m) | 462 if (!m) |
463 return false; | 463 return false; |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
520 } | 520 } |
521 free(oldZombies); | 521 free(oldZombies); |
522 } | 522 } |
523 | 523 |
524 return true; | 524 return true; |
525 } | 525 } |
526 | 526 |
527 void ZombieDisable() { | 527 void ZombieDisable() { |
528 // Only allow enable/disable on the main thread, just to keep things | 528 // Only allow enable/disable on the main thread, just to keep things |
529 // simple. | 529 // simple. |
530 CHECK([NSThread isMainThread]); | 530 DCHECK([NSThread isMainThread]); |
531 | 531 |
532 // |ZombieInit()| was never called. | 532 // |ZombieInit()| was never called. |
533 if (!g_originalDeallocIMP) | 533 if (!g_originalDeallocIMP) |
534 return; | 534 return; |
535 | 535 |
536 // Put back the original implementation of -[NSObject dealloc]. | 536 // Put back the original implementation of -[NSObject dealloc]. |
537 Method m = class_getInstanceMethod([NSObject class], @selector(dealloc)); | 537 Method m = class_getInstanceMethod([NSObject class], @selector(dealloc)); |
538 CHECK(m); | 538 DCHECK(m); |
539 method_setImplementation(m, g_originalDeallocIMP); | 539 method_setImplementation(m, g_originalDeallocIMP); |
540 | 540 |
541 // Can safely grab this because it only happens on the main thread. | 541 // Can safely grab this because it only happens on the main thread. |
542 const size_t oldCount = g_zombieCount; | 542 const size_t oldCount = g_zombieCount; |
543 ZombieRecord* oldZombies = g_zombies; | 543 ZombieRecord* oldZombies = g_zombies; |
544 | 544 |
545 { | 545 { |
546 base::AutoLock pin(lock_); // In case any |-dealloc| are in-progress. | 546 base::AutoLock pin(lock_); // In case any |-dealloc| are in-progress. |
547 g_zombieCount = 0; | 547 g_zombieCount = 0; |
548 g_zombies = NULL; | 548 g_zombies = NULL; |
549 } | 549 } |
550 | 550 |
551 // Free any remaining zombies. | 551 // Free any remaining zombies. |
552 if (oldZombies) { | 552 if (oldZombies) { |
553 for (size_t i = 0; i < oldCount; ++i) { | 553 for (size_t i = 0; i < oldCount; ++i) { |
554 if (oldZombies[i].object) | 554 if (oldZombies[i].object) |
555 object_dispose(oldZombies[i].object); | 555 object_dispose(oldZombies[i].object); |
556 } | 556 } |
557 free(oldZombies); | 557 free(oldZombies); |
558 } | 558 } |
559 } | 559 } |
560 | 560 |
561 } // namespace ObjcEvilDoers | 561 } // namespace ObjcEvilDoers |
OLD | NEW |