| 7 | #include "base/allocator/allocator_interception_mac.h" |
| 8 | #include "base/allocator/allocator_shim.h" |
| 9 | #include "base/allocator/buildflags.h" |
| 10 | #include "build/build_config.h" |
| 11 |
| 12 | namespace base { |
| 13 |
| 14 | namespace { |
| 15 | void oom_killer_new() { |
| 16 | TerminateBecauseOutOfMemory(0); |
| 17 | } |
| 18 | } // namespace |
| 19 |
| 20 | void EnableTerminationOnHeapCorruption() { |
| 21 | #if !ARCH_CPU_64_BITS |
| 22 | DLOG(WARNING) << "EnableTerminationOnHeapCorruption only works on 64-bit"; |
| 23 | #endif |
| 24 | } |
| 25 |
| 26 | bool UncheckedMalloc(size_t size, void** result) { |
| 27 | return allocator::UncheckedMallocMac(size, result); |
| 28 | } |
| 29 |
| 30 | bool UncheckedCalloc(size_t num_items, size_t size, void** result) { |
| 31 | return allocator::UncheckedCallocMac(num_items, size, result); |
| 32 | } |
| 33 |
| 34 | void EnableTerminationOnOutOfMemory() { |
| 35 | // Step 1: Enable OOM killer on C++ failures. |
| 36 | std::set_new_handler(oom_killer_new); |
| 37 |
| 38 | // Step 2: Enable OOM killer on C-malloc failures for the default zone (if we |
| 39 | // have a shim). |
| 40 | #if BUILDFLAG(USE_ALLOCATOR_SHIM) |
| 41 | allocator::SetCallNewHandlerOnMallocFailure(true); |
| 42 | #endif |
| 43 |
| 44 | // Step 3: Enable OOM killer on all other malloc zones (or just "all" without |
| 45 | // "other" if shim is disabled). |
| 46 | allocator::InterceptAllocationsMac(); |
| 47 | } |
| 48 |
| 49 | } // namespace base |
| 50 |