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 return darwin_version == 9 || | |
627 darwin_version == 10 || | |
628 darwin_version == 11; | |
629 } | |
630 | |
631 CFAllocatorContext* ContextForCFAllocator(CFAllocatorRef allocator, | |
632 long darwin_version) { | |
633 if (darwin_version == 9 || darwin_version == 10) { | |
634 ChromeCFAllocator9and10* our_allocator = | |
635 const_cast<ChromeCFAllocator9and10*>( | |
636 reinterpret_cast<const ChromeCFAllocator9and10*>(allocator)); | |
637 return &our_allocator->_context; | |
638 } else if (darwin_version == 11) { | |
639 ChromeCFAllocator11* our_allocator = | |
Mark Mentovai
2011/03/17 21:37:44
Warn as discussed?
Avi (use Gerrit)
2011/03/17 22:09:17
Done.
| |
640 const_cast<ChromeCFAllocator11*>( | |
641 reinterpret_cast<const ChromeCFAllocator11*>(allocator)); | |
642 return &our_allocator->_context; | |
643 } else { | |
644 return NULL; | |
645 } | |
646 } | |
626 | 647 |
627 CFAllocatorAllocateCallBack g_old_cfallocator_system_default; | 648 CFAllocatorAllocateCallBack g_old_cfallocator_system_default; |
628 CFAllocatorAllocateCallBack g_old_cfallocator_malloc; | 649 CFAllocatorAllocateCallBack g_old_cfallocator_malloc; |
629 CFAllocatorAllocateCallBack g_old_cfallocator_malloc_zone; | 650 CFAllocatorAllocateCallBack g_old_cfallocator_malloc_zone; |
630 | 651 |
631 void* oom_killer_cfallocator_system_default(CFIndex alloc_size, | 652 void* oom_killer_cfallocator_system_default(CFIndex alloc_size, |
632 CFOptionFlags hint, | 653 CFOptionFlags hint, |
633 void* info) { | 654 void* info) { |
634 void* result = g_old_cfallocator_system_default(alloc_size, hint, info); | 655 void* result = g_old_cfallocator_system_default(alloc_size, hint, info); |
635 if (!result) | 656 if (!result) |
(...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
826 // === Core Foundation CFAllocators === | 847 // === Core Foundation CFAllocators === |
827 | 848 |
828 // This will not catch allocation done by custom allocators, but will catch | 849 // This will not catch allocation done by custom allocators, but will catch |
829 // all allocation done by system-provided ones. | 850 // all allocation done by system-provided ones. |
830 | 851 |
831 CHECK(!g_old_cfallocator_system_default && !g_old_cfallocator_malloc && | 852 CHECK(!g_old_cfallocator_system_default && !g_old_cfallocator_malloc && |
832 !g_old_cfallocator_malloc_zone) | 853 !g_old_cfallocator_malloc_zone) |
833 << "Old allocators unexpectedly non-null"; | 854 << "Old allocators unexpectedly non-null"; |
834 | 855 |
835 bool cf_allocator_internals_known = | 856 bool cf_allocator_internals_known = |
836 darwin_version == 9 || darwin_version == 10; | 857 CanGetContextForCFAllocator(darwin_version); |
837 | 858 |
838 if (cf_allocator_internals_known) { | 859 if (cf_allocator_internals_known) { |
839 ChromeCFAllocatorRef allocator = const_cast<ChromeCFAllocatorRef>( | 860 CFAllocatorContext* context = |
840 reinterpret_cast<const ChromeCFAllocator*>(kCFAllocatorSystemDefault)); | 861 ContextForCFAllocator(kCFAllocatorSystemDefault, darwin_version); |
841 g_old_cfallocator_system_default = allocator->_context.allocate; | 862 CHECK(context) << "Failed to get context for kCFAllocatorSystemDefault."; |
863 g_old_cfallocator_system_default = context->allocate; | |
842 CHECK(g_old_cfallocator_system_default) | 864 CHECK(g_old_cfallocator_system_default) |
843 << "Failed to get kCFAllocatorSystemDefault allocation function."; | 865 << "Failed to get kCFAllocatorSystemDefault allocation function."; |
844 allocator->_context.allocate = oom_killer_cfallocator_system_default; | 866 context->allocate = oom_killer_cfallocator_system_default; |
845 | 867 |
846 allocator = const_cast<ChromeCFAllocatorRef>( | 868 context = ContextForCFAllocator(kCFAllocatorMalloc, darwin_version); |
847 reinterpret_cast<const ChromeCFAllocator*>(kCFAllocatorMalloc)); | 869 CHECK(context) << "Failed to get context for kCFAllocatorMalloc."; |
848 g_old_cfallocator_malloc = allocator->_context.allocate; | 870 g_old_cfallocator_malloc = context->allocate; |
849 CHECK(g_old_cfallocator_malloc) | 871 CHECK(g_old_cfallocator_malloc) |
850 << "Failed to get kCFAllocatorMalloc allocation function."; | 872 << "Failed to get kCFAllocatorMalloc allocation function."; |
851 allocator->_context.allocate = oom_killer_cfallocator_malloc; | 873 context->allocate = oom_killer_cfallocator_malloc; |
852 | 874 |
853 allocator = const_cast<ChromeCFAllocatorRef>( | 875 context = ContextForCFAllocator(kCFAllocatorMallocZone, darwin_version); |
854 reinterpret_cast<const ChromeCFAllocator*>(kCFAllocatorMallocZone)); | 876 CHECK(context) << "Failed to get context for kCFAllocatorMallocZone."; |
855 g_old_cfallocator_malloc_zone = allocator->_context.allocate; | 877 g_old_cfallocator_malloc_zone = context->allocate; |
856 CHECK(g_old_cfallocator_malloc_zone) | 878 CHECK(g_old_cfallocator_malloc_zone) |
857 << "Failed to get kCFAllocatorMallocZone allocation function."; | 879 << "Failed to get kCFAllocatorMallocZone allocation function."; |
858 allocator->_context.allocate = oom_killer_cfallocator_malloc_zone; | 880 context->allocate = oom_killer_cfallocator_malloc_zone; |
859 } else { | 881 } else { |
860 NSLog(@"Internals of CFAllocator not known; out-of-memory failures via " | 882 NSLog(@"Internals of CFAllocator not known; out-of-memory failures via " |
861 "CFAllocator will not result in termination. http://crbug.com/45650"); | 883 "CFAllocator will not result in termination. http://crbug.com/45650"); |
862 } | 884 } |
863 | 885 |
864 // === Cocoa NSObject allocation === | 886 // === Cocoa NSObject allocation === |
865 | 887 |
866 // Note that both +[NSObject new] and +[NSObject alloc] call through to | 888 // Note that both +[NSObject new] and +[NSObject alloc] call through to |
867 // +[NSObject allocWithZone:]. | 889 // +[NSObject allocWithZone:]. |
868 | 890 |
(...skipping 18 matching lines...) Expand all Loading... | |
887 if (sysctl(mib, 4, &info, &length, NULL, 0) < 0) { | 909 if (sysctl(mib, 4, &info, &length, NULL, 0) < 0) { |
888 PLOG(ERROR) << "sysctl"; | 910 PLOG(ERROR) << "sysctl"; |
889 return -1; | 911 return -1; |
890 } | 912 } |
891 if (length == 0) | 913 if (length == 0) |
892 return -1; | 914 return -1; |
893 return info.kp_eproc.e_ppid; | 915 return info.kp_eproc.e_ppid; |
894 } | 916 } |
895 | 917 |
896 } // namespace base | 918 } // namespace base |
OLD | NEW |