Elliott Hughes | 15b641a | 2014-05-14 18:18:55 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include <new> |
Elliott Hughes | f44b232 | 2016-05-26 17:35:00 -0700 | [diff] [blame] | 18 | |
Elliott Hughes | 15b641a | 2014-05-14 18:18:55 -0700 | [diff] [blame] | 19 | #include <stdlib.h> |
| 20 | |
Christopher Ferris | 7a3681e | 2017-04-24 17:48:32 -0700 | [diff] [blame] | 21 | #include <async_safe/log.h> |
Dan Albert | 9897259 | 2014-05-30 16:00:53 -0700 | [diff] [blame] | 22 | |
Peter Collingbourne | 9397bdd | 2020-12-08 14:26:21 -0800 | [diff] [blame] | 23 | __attribute__((weak)) const std::nothrow_t std::nothrow = {}; |
Elliott Hughes | 15b641a | 2014-05-14 18:18:55 -0700 | [diff] [blame] | 24 | |
Elliott Hughes | ad44152 | 2024-06-05 12:26:56 +0000 | [diff] [blame^] | 25 | // We can't throw in bionic, so we go straight to the equivalent of |
| 26 | // std::terminate for these two instead. |
Elliott Hughes | 15b641a | 2014-05-14 18:18:55 -0700 | [diff] [blame] | 27 | void* operator new(std::size_t size) { |
| 28 | void* p = malloc(size); |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 29 | if (p == nullptr) { |
Christopher Ferris | 7a3681e | 2017-04-24 17:48:32 -0700 | [diff] [blame] | 30 | async_safe_fatal("new failed to allocate %zu bytes", size); |
Elliott Hughes | 15b641a | 2014-05-14 18:18:55 -0700 | [diff] [blame] | 31 | } |
| 32 | return p; |
| 33 | } |
Elliott Hughes | 15b641a | 2014-05-14 18:18:55 -0700 | [diff] [blame] | 34 | void* operator new[](std::size_t size) { |
| 35 | void* p = malloc(size); |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 36 | if (p == nullptr) { |
Christopher Ferris | 7a3681e | 2017-04-24 17:48:32 -0700 | [diff] [blame] | 37 | async_safe_fatal("new[] failed to allocate %zu bytes", size); |
Elliott Hughes | 15b641a | 2014-05-14 18:18:55 -0700 | [diff] [blame] | 38 | } |
| 39 | return p; |
| 40 | } |
| 41 | |
Elliott Hughes | ad44152 | 2024-06-05 12:26:56 +0000 | [diff] [blame^] | 42 | // These two are the "nothrow" variants, so we just return nullptr on failure. |
Elliott Hughes | 15b641a | 2014-05-14 18:18:55 -0700 | [diff] [blame] | 43 | void* operator new(std::size_t size, const std::nothrow_t&) { |
| 44 | return malloc(size); |
| 45 | } |
Elliott Hughes | 15b641a | 2014-05-14 18:18:55 -0700 | [diff] [blame] | 46 | void* operator new[](std::size_t size, const std::nothrow_t&) { |
| 47 | return malloc(size); |
| 48 | } |
| 49 | |
Elliott Hughes | ad44152 | 2024-06-05 12:26:56 +0000 | [diff] [blame^] | 50 | // free() can't throw anyway (except on heap corruption, which is always fatal), |
| 51 | // so there's no difference between the regular and "nothrow" variants here. |
| 52 | void operator delete(void* p) noexcept { free(p); } |
| 53 | void operator delete[](void* p) noexcept { free(p); } |
| 54 | void operator delete(void* p, const std::nothrow_t&) noexcept { free(p); } |
| 55 | void operator delete[](void* p, const std::nothrow_t&) noexcept { free(p); } |
Elliott Hughes | 15b641a | 2014-05-14 18:18:55 -0700 | [diff] [blame] | 56 | |
Elliott Hughes | ad44152 | 2024-06-05 12:26:56 +0000 | [diff] [blame^] | 57 | // TODO: these can use free_sized() once we have it (http://b/284321795). |
| 58 | void operator delete(void* p, std::size_t) noexcept { free(p); } |
| 59 | void operator delete[](void* p, std::size_t) noexcept { free(p); } |