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 471 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
482 id oom_killer_allocWithZone(id self, SEL _cmd, NSZone* zone) | 482 id oom_killer_allocWithZone(id self, SEL _cmd, NSZone* zone) |
483 { | 483 { |
484 id result = g_old_allocWithZone(self, _cmd, zone); | 484 id result = g_old_allocWithZone(self, _cmd, zone); |
485 if (!result) | 485 if (!result) |
486 debug::BreakDebugger(); | 486 debug::BreakDebugger(); |
487 return result; | 487 return result; |
488 } | 488 } |
489 | 489 |
490 } // namespace | 490 } // namespace |
491 | 491 |
492 void* UncheckedMalloc(size_t size) { | 492 bool UncheckedMalloc(size_t size, void** result) { |
493 if (g_old_malloc) { | 493 if (g_old_malloc) { |
494 #if ARCH_CPU_32_BITS | 494 #if ARCH_CPU_32_BITS |
495 ScopedClearErrno clear_errno; | 495 ScopedClearErrno clear_errno; |
496 ThreadLocalBooleanAutoReset flag(g_unchecked_alloc.Pointer(), true); | 496 ThreadLocalBooleanAutoReset flag(g_unchecked_alloc.Pointer(), true); |
497 #endif // ARCH_CPU_32_BITS | 497 #endif // ARCH_CPU_32_BITS |
498 return g_old_malloc(malloc_default_zone(), size); | 498 return *result = g_old_malloc(malloc_default_zone(), size); |
| 499 } else { |
| 500 *result = malloc(size); |
499 } | 501 } |
500 return malloc(size); | 502 |
| 503 return *result; |
501 } | 504 } |
502 | 505 |
503 void* UncheckedCalloc(size_t num_items, size_t size) { | 506 bool UncheckedCalloc(size_t num_items, size_t size, void** result) { |
504 if (g_old_calloc) { | 507 if (g_old_calloc) { |
505 #if ARCH_CPU_32_BITS | 508 #if ARCH_CPU_32_BITS |
506 ScopedClearErrno clear_errno; | 509 ScopedClearErrno clear_errno; |
507 ThreadLocalBooleanAutoReset flag(g_unchecked_alloc.Pointer(), true); | 510 ThreadLocalBooleanAutoReset flag(g_unchecked_alloc.Pointer(), true); |
508 #endif // ARCH_CPU_32_BITS | 511 #endif // ARCH_CPU_32_BITS |
509 return g_old_calloc(malloc_default_zone(), num_items, size); | 512 *result = g_old_calloc(malloc_default_zone(), num_items, size); |
| 513 } else { |
| 514 *result = calloc(num_items, size); |
510 } | 515 } |
511 return calloc(num_items, size); | 516 |
| 517 return *result; |
| 518 } |
| 519 |
| 520 void* UncheckedMalloc(size_t size) { |
| 521 void* address; |
| 522 bool result ALLOW_UNUSED = UncheckedMalloc(size, &address); |
| 523 return address; |
| 524 } |
| 525 |
| 526 void* UncheckedCalloc(size_t num_items, size_t size) { |
| 527 void* address; |
| 528 bool result ALLOW_UNUSED = UncheckedCalloc(num_items, size, &address); |
| 529 return address; |
512 } | 530 } |
513 | 531 |
514 void EnableTerminationOnOutOfMemory() { | 532 void EnableTerminationOnOutOfMemory() { |
515 if (g_oom_killer_enabled) | 533 if (g_oom_killer_enabled) |
516 return; | 534 return; |
517 | 535 |
518 g_oom_killer_enabled = true; | 536 g_oom_killer_enabled = true; |
519 | 537 |
520 // === C malloc/calloc/valloc/realloc/posix_memalign === | 538 // === C malloc/calloc/valloc/realloc/posix_memalign === |
521 | 539 |
(...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
708 @selector(allocWithZone:)); | 726 @selector(allocWithZone:)); |
709 g_old_allocWithZone = reinterpret_cast<allocWithZone_t>( | 727 g_old_allocWithZone = reinterpret_cast<allocWithZone_t>( |
710 method_getImplementation(orig_method)); | 728 method_getImplementation(orig_method)); |
711 CHECK(g_old_allocWithZone) | 729 CHECK(g_old_allocWithZone) |
712 << "Failed to get allocWithZone allocation function."; | 730 << "Failed to get allocWithZone allocation function."; |
713 method_setImplementation(orig_method, | 731 method_setImplementation(orig_method, |
714 reinterpret_cast<IMP>(oom_killer_allocWithZone)); | 732 reinterpret_cast<IMP>(oom_killer_allocWithZone)); |
715 } | 733 } |
716 | 734 |
717 } // namespace base | 735 } // namespace base |
OLD | NEW |