| OLD | NEW |
| 1 // Copyright (c) 2008 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 | 5 |
| 6 #include "base/process_util.h" | 6 #include "base/process_util.h" |
| 7 | 7 |
| 8 #import <Cocoa/Cocoa.h> | 8 #import <Cocoa/Cocoa.h> |
| 9 #include <crt_externs.h> | 9 #include <crt_externs.h> |
| 10 #include <dlfcn.h> | 10 #include <dlfcn.h> |
| 11 #include <mach/mach.h> | 11 #include <mach/mach.h> |
| (...skipping 603 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 615 } | 615 } |
| 616 | 616 |
| 617 // === C++ operator new === | 617 // === C++ operator new === |
| 618 | 618 |
| 619 void oom_killer_new() { | 619 void oom_killer_new() { |
| 620 debug::BreakDebugger(); | 620 debug::BreakDebugger(); |
| 621 } | 621 } |
| 622 | 622 |
| 623 // === Core Foundation CFAllocators === | 623 // === Core Foundation CFAllocators === |
| 624 | 624 |
| 625 typedef ChromeCFAllocator* ChromeCFAllocatorRef; | 625 bool CanGetContextForCFAllocator(long darwin_version) { |
| 626 // TODO(avi): remove at final release; http://crbug.com/74589 |
| 627 if (darwin_version == 11) { |
| 628 NSLog(@"Unsure about the internals of CFAllocator but going to patch them " |
| 629 "anyway. Watch out for crashes inside of CFAllocatorAllocate."); |
| 630 } |
| 631 return darwin_version == 9 || |
| 632 darwin_version == 10 || |
| 633 darwin_version == 11; |
| 634 } |
| 635 |
| 636 CFAllocatorContext* ContextForCFAllocator(CFAllocatorRef allocator, |
| 637 long darwin_version) { |
| 638 if (darwin_version == 9 || darwin_version == 10) { |
| 639 ChromeCFAllocator9and10* our_allocator = |
| 640 const_cast<ChromeCFAllocator9and10*>( |
| 641 reinterpret_cast<const ChromeCFAllocator9and10*>(allocator)); |
| 642 return &our_allocator->_context; |
| 643 } else if (darwin_version == 11) { |
| 644 ChromeCFAllocator11* our_allocator = |
| 645 const_cast<ChromeCFAllocator11*>( |
| 646 reinterpret_cast<const ChromeCFAllocator11*>(allocator)); |
| 647 return &our_allocator->_context; |
| 648 } else { |
| 649 return NULL; |
| 650 } |
| 651 } |
| 626 | 652 |
| 627 CFAllocatorAllocateCallBack g_old_cfallocator_system_default; | 653 CFAllocatorAllocateCallBack g_old_cfallocator_system_default; |
| 628 CFAllocatorAllocateCallBack g_old_cfallocator_malloc; | 654 CFAllocatorAllocateCallBack g_old_cfallocator_malloc; |
| 629 CFAllocatorAllocateCallBack g_old_cfallocator_malloc_zone; | 655 CFAllocatorAllocateCallBack g_old_cfallocator_malloc_zone; |
| 630 | 656 |
| 631 void* oom_killer_cfallocator_system_default(CFIndex alloc_size, | 657 void* oom_killer_cfallocator_system_default(CFIndex alloc_size, |
| 632 CFOptionFlags hint, | 658 CFOptionFlags hint, |
| 633 void* info) { | 659 void* info) { |
| 634 void* result = g_old_cfallocator_system_default(alloc_size, hint, info); | 660 void* result = g_old_cfallocator_system_default(alloc_size, hint, info); |
| 635 if (!result) | 661 if (!result) |
| (...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 826 // === Core Foundation CFAllocators === | 852 // === Core Foundation CFAllocators === |
| 827 | 853 |
| 828 // This will not catch allocation done by custom allocators, but will catch | 854 // This will not catch allocation done by custom allocators, but will catch |
| 829 // all allocation done by system-provided ones. | 855 // all allocation done by system-provided ones. |
| 830 | 856 |
| 831 CHECK(!g_old_cfallocator_system_default && !g_old_cfallocator_malloc && | 857 CHECK(!g_old_cfallocator_system_default && !g_old_cfallocator_malloc && |
| 832 !g_old_cfallocator_malloc_zone) | 858 !g_old_cfallocator_malloc_zone) |
| 833 << "Old allocators unexpectedly non-null"; | 859 << "Old allocators unexpectedly non-null"; |
| 834 | 860 |
| 835 bool cf_allocator_internals_known = | 861 bool cf_allocator_internals_known = |
| 836 darwin_version == 9 || darwin_version == 10; | 862 CanGetContextForCFAllocator(darwin_version); |
| 837 | 863 |
| 838 if (cf_allocator_internals_known) { | 864 if (cf_allocator_internals_known) { |
| 839 ChromeCFAllocatorRef allocator = const_cast<ChromeCFAllocatorRef>( | 865 CFAllocatorContext* context = |
| 840 reinterpret_cast<const ChromeCFAllocator*>(kCFAllocatorSystemDefault)); | 866 ContextForCFAllocator(kCFAllocatorSystemDefault, darwin_version); |
| 841 g_old_cfallocator_system_default = allocator->_context.allocate; | 867 CHECK(context) << "Failed to get context for kCFAllocatorSystemDefault."; |
| 868 g_old_cfallocator_system_default = context->allocate; |
| 842 CHECK(g_old_cfallocator_system_default) | 869 CHECK(g_old_cfallocator_system_default) |
| 843 << "Failed to get kCFAllocatorSystemDefault allocation function."; | 870 << "Failed to get kCFAllocatorSystemDefault allocation function."; |
| 844 allocator->_context.allocate = oom_killer_cfallocator_system_default; | 871 context->allocate = oom_killer_cfallocator_system_default; |
| 845 | 872 |
| 846 allocator = const_cast<ChromeCFAllocatorRef>( | 873 context = ContextForCFAllocator(kCFAllocatorMalloc, darwin_version); |
| 847 reinterpret_cast<const ChromeCFAllocator*>(kCFAllocatorMalloc)); | 874 CHECK(context) << "Failed to get context for kCFAllocatorMalloc."; |
| 848 g_old_cfallocator_malloc = allocator->_context.allocate; | 875 g_old_cfallocator_malloc = context->allocate; |
| 849 CHECK(g_old_cfallocator_malloc) | 876 CHECK(g_old_cfallocator_malloc) |
| 850 << "Failed to get kCFAllocatorMalloc allocation function."; | 877 << "Failed to get kCFAllocatorMalloc allocation function."; |
| 851 allocator->_context.allocate = oom_killer_cfallocator_malloc; | 878 context->allocate = oom_killer_cfallocator_malloc; |
| 852 | 879 |
| 853 allocator = const_cast<ChromeCFAllocatorRef>( | 880 context = ContextForCFAllocator(kCFAllocatorMallocZone, darwin_version); |
| 854 reinterpret_cast<const ChromeCFAllocator*>(kCFAllocatorMallocZone)); | 881 CHECK(context) << "Failed to get context for kCFAllocatorMallocZone."; |
| 855 g_old_cfallocator_malloc_zone = allocator->_context.allocate; | 882 g_old_cfallocator_malloc_zone = context->allocate; |
| 856 CHECK(g_old_cfallocator_malloc_zone) | 883 CHECK(g_old_cfallocator_malloc_zone) |
| 857 << "Failed to get kCFAllocatorMallocZone allocation function."; | 884 << "Failed to get kCFAllocatorMallocZone allocation function."; |
| 858 allocator->_context.allocate = oom_killer_cfallocator_malloc_zone; | 885 context->allocate = oom_killer_cfallocator_malloc_zone; |
| 859 } else { | 886 } else { |
| 860 NSLog(@"Internals of CFAllocator not known; out-of-memory failures via " | 887 NSLog(@"Internals of CFAllocator not known; out-of-memory failures via " |
| 861 "CFAllocator will not result in termination. http://crbug.com/45650"); | 888 "CFAllocator will not result in termination. http://crbug.com/45650"); |
| 862 } | 889 } |
| 863 | 890 |
| 864 // === Cocoa NSObject allocation === | 891 // === Cocoa NSObject allocation === |
| 865 | 892 |
| 866 // Note that both +[NSObject new] and +[NSObject alloc] call through to | 893 // Note that both +[NSObject new] and +[NSObject alloc] call through to |
| 867 // +[NSObject allocWithZone:]. | 894 // +[NSObject allocWithZone:]. |
| 868 | 895 |
| (...skipping 18 matching lines...) Expand all Loading... |
| 887 if (sysctl(mib, 4, &info, &length, NULL, 0) < 0) { | 914 if (sysctl(mib, 4, &info, &length, NULL, 0) < 0) { |
| 888 PLOG(ERROR) << "sysctl"; | 915 PLOG(ERROR) << "sysctl"; |
| 889 return -1; | 916 return -1; |
| 890 } | 917 } |
| 891 if (length == 0) | 918 if (length == 0) |
| 892 return -1; | 919 return -1; |
| 893 return info.kp_eproc.e_ppid; | 920 return info.kp_eproc.e_ppid; |
| 894 } | 921 } |
| 895 | 922 |
| 896 } // namespace base | 923 } // namespace base |
| OLD | NEW |