| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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 #include "base/process/memory.h" | 5 #include "base/process/memory.h" |
| 6 | 6 |
| 7 #include <CoreFoundation/CoreFoundation.h> | 7 #include <CoreFoundation/CoreFoundation.h> |
| 8 #include <errno.h> | 8 #include <errno.h> |
| 9 #include <mach/mach.h> | 9 #include <mach/mach.h> |
| 10 #include <mach/mach_vm.h> | 10 #include <mach/mach_vm.h> |
| (...skipping 420 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 431 | 431 |
| 432 // === C++ operator new === | 432 // === C++ operator new === |
| 433 | 433 |
| 434 void oom_killer_new() { | 434 void oom_killer_new() { |
| 435 debug::BreakDebugger(); | 435 debug::BreakDebugger(); |
| 436 } | 436 } |
| 437 | 437 |
| 438 // === Core Foundation CFAllocators === | 438 // === Core Foundation CFAllocators === |
| 439 | 439 |
| 440 bool CanGetContextForCFAllocator() { | 440 bool CanGetContextForCFAllocator() { |
| 441 return !base::mac::IsOSYosemiteOrLater(); | 441 return !base::mac::IsOSLaterThanYosemite_DontCallThis(); |
| 442 } | 442 } |
| 443 | 443 |
| 444 CFAllocatorContext* ContextForCFAllocator(CFAllocatorRef allocator) { | 444 CFAllocatorContext* ContextForCFAllocator(CFAllocatorRef allocator) { |
| 445 if (base::mac::IsOSSnowLeopard()) { | 445 if (base::mac::IsOSSnowLeopard()) { |
| 446 ChromeCFAllocatorLeopards* our_allocator = | 446 ChromeCFAllocatorLeopards* our_allocator = |
| 447 const_cast<ChromeCFAllocatorLeopards*>( | 447 const_cast<ChromeCFAllocatorLeopards*>( |
| 448 reinterpret_cast<const ChromeCFAllocatorLeopards*>(allocator)); | 448 reinterpret_cast<const ChromeCFAllocatorLeopards*>(allocator)); |
| 449 return &our_allocator->_context; | 449 return &our_allocator->_context; |
| 450 } else if (base::mac::IsOSLion() || | 450 } else if (base::mac::IsOSLion() || |
| 451 base::mac::IsOSMountainLion() || | 451 base::mac::IsOSMountainLion() || |
| 452 base::mac::IsOSMavericks()) { | 452 base::mac::IsOSMavericks() || |
| 453 base::mac::IsOSYosemite()) { |
| 453 ChromeCFAllocatorLions* our_allocator = | 454 ChromeCFAllocatorLions* our_allocator = |
| 454 const_cast<ChromeCFAllocatorLions*>( | 455 const_cast<ChromeCFAllocatorLions*>( |
| 455 reinterpret_cast<const ChromeCFAllocatorLions*>(allocator)); | 456 reinterpret_cast<const ChromeCFAllocatorLions*>(allocator)); |
| 456 return &our_allocator->_context; | 457 return &our_allocator->_context; |
| 457 } else { | 458 } else { |
| 458 return NULL; | 459 return NULL; |
| 459 } | 460 } |
| 460 } | 461 } |
| 461 | 462 |
| 462 CFAllocatorAllocateCallBack g_old_cfallocator_system_default; | 463 CFAllocatorAllocateCallBack g_old_cfallocator_system_default; |
| (...skipping 252 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 715 << "Failed to get kCFAllocatorMalloc allocation function."; | 716 << "Failed to get kCFAllocatorMalloc allocation function."; |
| 716 context->allocate = oom_killer_cfallocator_malloc; | 717 context->allocate = oom_killer_cfallocator_malloc; |
| 717 | 718 |
| 718 context = ContextForCFAllocator(kCFAllocatorMallocZone); | 719 context = ContextForCFAllocator(kCFAllocatorMallocZone); |
| 719 CHECK(context) << "Failed to get context for kCFAllocatorMallocZone."; | 720 CHECK(context) << "Failed to get context for kCFAllocatorMallocZone."; |
| 720 g_old_cfallocator_malloc_zone = context->allocate; | 721 g_old_cfallocator_malloc_zone = context->allocate; |
| 721 CHECK(g_old_cfallocator_malloc_zone) | 722 CHECK(g_old_cfallocator_malloc_zone) |
| 722 << "Failed to get kCFAllocatorMallocZone allocation function."; | 723 << "Failed to get kCFAllocatorMallocZone allocation function."; |
| 723 context->allocate = oom_killer_cfallocator_malloc_zone; | 724 context->allocate = oom_killer_cfallocator_malloc_zone; |
| 724 } else { | 725 } else { |
| 725 NSLog(@"Internals of CFAllocator not known; out-of-memory failures via " | 726 DLOG(WARNING) << "Internals of CFAllocator not known; out-of-memory " |
| 726 "CFAllocator will not result in termination. http://crbug.com/45650"); | 727 "failures via CFAllocator will not result in termination. " |
| 728 "http://crbug.com/45650"; |
| 727 } | 729 } |
| 728 #endif | 730 #endif |
| 729 | 731 |
| 730 // === Cocoa NSObject allocation === | 732 // === Cocoa NSObject allocation === |
| 731 | 733 |
| 732 // Note that both +[NSObject new] and +[NSObject alloc] call through to | 734 // Note that both +[NSObject new] and +[NSObject alloc] call through to |
| 733 // +[NSObject allocWithZone:]. | 735 // +[NSObject allocWithZone:]. |
| 734 | 736 |
| 735 CHECK(!g_old_allocWithZone) | 737 CHECK(!g_old_allocWithZone) |
| 736 << "Old allocator unexpectedly non-null"; | 738 << "Old allocator unexpectedly non-null"; |
| 737 | 739 |
| 738 Class nsobject_class = [NSObject class]; | 740 Class nsobject_class = [NSObject class]; |
| 739 Method orig_method = class_getClassMethod(nsobject_class, | 741 Method orig_method = class_getClassMethod(nsobject_class, |
| 740 @selector(allocWithZone:)); | 742 @selector(allocWithZone:)); |
| 741 g_old_allocWithZone = reinterpret_cast<allocWithZone_t>( | 743 g_old_allocWithZone = reinterpret_cast<allocWithZone_t>( |
| 742 method_getImplementation(orig_method)); | 744 method_getImplementation(orig_method)); |
| 743 CHECK(g_old_allocWithZone) | 745 CHECK(g_old_allocWithZone) |
| 744 << "Failed to get allocWithZone allocation function."; | 746 << "Failed to get allocWithZone allocation function."; |
| 745 method_setImplementation(orig_method, | 747 method_setImplementation(orig_method, |
| 746 reinterpret_cast<IMP>(oom_killer_allocWithZone)); | 748 reinterpret_cast<IMP>(oom_killer_allocWithZone)); |
| 747 } | 749 } |
| 748 | 750 |
| 749 } // namespace base | 751 } // namespace base |
| OLD | NEW |