blob: c9ce163f51ed91e206ceca94e2413638269c85a9 [file] [log] [blame]
Elliott Hughes15b641a2014-05-14 18:18:55 -07001/*
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 Hughesf44b2322016-05-26 17:35:00 -070018
19#include <errno.h>
Elliott Hughes15b641a2014-05-14 18:18:55 -070020#include <stdlib.h>
21
Christopher Ferris7a3681e2017-04-24 17:48:32 -070022#include <async_safe/log.h>
Dan Albert98972592014-05-30 16:00:53 -070023
Peter Collingbourne9397bdd2020-12-08 14:26:21 -080024__attribute__((weak)) const std::nothrow_t std::nothrow = {};
Elliott Hughes15b641a2014-05-14 18:18:55 -070025
26void* operator new(std::size_t size) {
27 void* p = malloc(size);
Yi Kong32bc0fc2018-08-02 17:31:13 -070028 if (p == nullptr) {
Christopher Ferris7a3681e2017-04-24 17:48:32 -070029 async_safe_fatal("new failed to allocate %zu bytes", size);
Elliott Hughes15b641a2014-05-14 18:18:55 -070030 }
31 return p;
32}
33
34void* operator new[](std::size_t size) {
35 void* p = malloc(size);
Yi Kong32bc0fc2018-08-02 17:31:13 -070036 if (p == nullptr) {
Christopher Ferris7a3681e2017-04-24 17:48:32 -070037 async_safe_fatal("new[] failed to allocate %zu bytes", size);
Elliott Hughes15b641a2014-05-14 18:18:55 -070038 }
39 return p;
40}
41
Chih-Hung Hsiehae558d62014-08-25 12:08:19 -070042void operator delete(void* ptr) throw() {
Elliott Hughes15b641a2014-05-14 18:18:55 -070043 free(ptr);
44}
45
Chih-Hung Hsiehae558d62014-08-25 12:08:19 -070046void operator delete[](void* ptr) throw() {
Elliott Hughes15b641a2014-05-14 18:18:55 -070047 free(ptr);
48}
49
50void* operator new(std::size_t size, const std::nothrow_t&) {
51 return malloc(size);
52}
53
54void* operator new[](std::size_t size, const std::nothrow_t&) {
55 return malloc(size);
56}
57
Chih-Hung Hsiehae558d62014-08-25 12:08:19 -070058void operator delete(void* ptr, const std::nothrow_t&) throw() {
Elliott Hughes15b641a2014-05-14 18:18:55 -070059 free(ptr);
60}
61
Chih-Hung Hsiehae558d62014-08-25 12:08:19 -070062void operator delete[](void* ptr, const std::nothrow_t&) throw() {
Elliott Hughes15b641a2014-05-14 18:18:55 -070063 free(ptr);
64}