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 *result = g_old_malloc(malloc_default_zone(), size); |
| 512 } else { |
| 513 *result = malloc(size); |
512 } | 514 } |
513 return malloc(size); | 515 |
| 516 return *result != NULL; |
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 != NULL; |
| 531 } |
| 532 |
| 533 void* UncheckedMalloc(size_t size) { |
| 534 void* address; |
| 535 return UncheckedMalloc(size, &address) ? address : NULL; |
| 536 } |
| 537 |
| 538 void* UncheckedCalloc(size_t num_items, size_t size) { |
| 539 void* address; |
| 540 return UncheckedCalloc(num_items, size, &address) ? address : NULL; |
525 } | 541 } |
526 | 542 |
527 void EnableTerminationOnOutOfMemory() { | 543 void EnableTerminationOnOutOfMemory() { |
528 if (g_oom_killer_enabled) | 544 if (g_oom_killer_enabled) |
529 return; | 545 return; |
530 | 546 |
531 g_oom_killer_enabled = true; | 547 g_oom_killer_enabled = true; |
532 | 548 |
533 // === C malloc/calloc/valloc/realloc/posix_memalign === | 549 // === C malloc/calloc/valloc/realloc/posix_memalign === |
534 | 550 |
(...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
721 @selector(allocWithZone:)); | 737 @selector(allocWithZone:)); |
722 g_old_allocWithZone = reinterpret_cast<allocWithZone_t>( | 738 g_old_allocWithZone = reinterpret_cast<allocWithZone_t>( |
723 method_getImplementation(orig_method)); | 739 method_getImplementation(orig_method)); |
724 CHECK(g_old_allocWithZone) | 740 CHECK(g_old_allocWithZone) |
725 << "Failed to get allocWithZone allocation function."; | 741 << "Failed to get allocWithZone allocation function."; |
726 method_setImplementation(orig_method, | 742 method_setImplementation(orig_method, |
727 reinterpret_cast<IMP>(oom_killer_allocWithZone)); | 743 reinterpret_cast<IMP>(oom_killer_allocWithZone)); |
728 } | 744 } |
729 | 745 |
730 } // namespace base | 746 } // namespace base |
OLD | NEW |