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 484 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
495 id oom_killer_allocWithZone(id self, SEL _cmd, NSZone* zone) | 495 id oom_killer_allocWithZone(id self, SEL _cmd, NSZone* zone) |
496 { | 496 { |
497 id result = g_old_allocWithZone(self, _cmd, zone); | 497 id result = g_old_allocWithZone(self, _cmd, zone); |
498 if (!result) | 498 if (!result) |
499 debug::BreakDebugger(); | 499 debug::BreakDebugger(); |
500 return result; | 500 return result; |
501 } | 501 } |
502 | 502 |
503 } // namespace | 503 } // namespace |
504 | 504 |
505 void* UncheckedMalloc(size_t size) { | 505 bool UncheckedMalloc(size_t size, void** result) { |
506 if (g_old_malloc) { | 506 if (g_old_malloc) { |
507 #if ARCH_CPU_32_BITS | 507 #if ARCH_CPU_32_BITS |
508 ScopedClearErrno clear_errno; | 508 ScopedClearErrno clear_errno; |
509 ThreadLocalBooleanAutoReset flag(g_unchecked_alloc.Pointer(), true); | 509 ThreadLocalBooleanAutoReset flag(g_unchecked_alloc.Pointer(), true); |
510 #endif // ARCH_CPU_32_BITS | 510 #endif // ARCH_CPU_32_BITS |
511 return g_old_malloc(malloc_default_zone(), size); | 511 return *result = g_old_malloc(malloc_default_zone(), size); |
512 } else { | |
513 *result = malloc(size); | |
512 } | 514 } |
513 return malloc(size); | 515 |
516 return *result; | |
514 } | 517 } |
515 | 518 |
516 void* UncheckedCalloc(size_t num_items, size_t size) { | 519 bool UncheckedCalloc(size_t num_items, size_t size, void** result) { |
517 if (g_old_calloc) { | 520 if (g_old_calloc) { |
518 #if ARCH_CPU_32_BITS | 521 #if ARCH_CPU_32_BITS |
519 ScopedClearErrno clear_errno; | 522 ScopedClearErrno clear_errno; |
520 ThreadLocalBooleanAutoReset flag(g_unchecked_alloc.Pointer(), true); | 523 ThreadLocalBooleanAutoReset flag(g_unchecked_alloc.Pointer(), true); |
521 #endif // ARCH_CPU_32_BITS | 524 #endif // ARCH_CPU_32_BITS |
522 return g_old_calloc(malloc_default_zone(), num_items, size); | 525 *result = g_old_calloc(malloc_default_zone(), num_items, size); |
526 } else { | |
527 *result = calloc(num_items, size); | |
523 } | 528 } |
524 return calloc(num_items, size); | 529 |
530 return *result; | |
jar (doing other things)
2014/02/26 03:59:52
nit: Clearer might be:
return NULL != *result;
| |
531 } | |
532 | |
533 void* UncheckedMalloc(size_t size) { | |
534 void* address; | |
535 bool result ALLOW_UNUSED = UncheckedMalloc(size, &address); | |
jar (doing other things)
2014/02/26 03:59:52
It is a bit distressing to see both a bool return,
| |
536 return address; | |
537 } | |
538 | |
539 void* UncheckedCalloc(size_t num_items, size_t size) { | |
540 void* address; | |
541 bool result ALLOW_UNUSED = UncheckedCalloc(num_items, size, &address); | |
542 return address; | |
525 } | 543 } |
526 | 544 |
527 void EnableTerminationOnOutOfMemory() { | 545 void EnableTerminationOnOutOfMemory() { |
528 if (g_oom_killer_enabled) | 546 if (g_oom_killer_enabled) |
529 return; | 547 return; |
530 | 548 |
531 g_oom_killer_enabled = true; | 549 g_oom_killer_enabled = true; |
532 | 550 |
533 // === C malloc/calloc/valloc/realloc/posix_memalign === | 551 // === C malloc/calloc/valloc/realloc/posix_memalign === |
534 | 552 |
(...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
721 @selector(allocWithZone:)); | 739 @selector(allocWithZone:)); |
722 g_old_allocWithZone = reinterpret_cast<allocWithZone_t>( | 740 g_old_allocWithZone = reinterpret_cast<allocWithZone_t>( |
723 method_getImplementation(orig_method)); | 741 method_getImplementation(orig_method)); |
724 CHECK(g_old_allocWithZone) | 742 CHECK(g_old_allocWithZone) |
725 << "Failed to get allocWithZone allocation function."; | 743 << "Failed to get allocWithZone allocation function."; |
726 method_setImplementation(orig_method, | 744 method_setImplementation(orig_method, |
727 reinterpret_cast<IMP>(oom_killer_allocWithZone)); | 745 reinterpret_cast<IMP>(oom_killer_allocWithZone)); |
728 } | 746 } |
729 | 747 |
730 } // namespace base | 748 } // namespace base |
OLD | NEW |