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>
18
19 #include <errno.h>
20 #include <stdlib.h>
21
22 #include <async_safe/log.h>
23
24 const std::nothrow_t std::nothrow = {};
25
operator new(std::size_t size)26 void* operator new(std::size_t size) {
27 void* p = malloc(size);
28 if (p == nullptr) {
29 async_safe_fatal("new failed to allocate %zu bytes", size);
30 }
31 return p;
32 }
33
operator new[](std::size_t size)34 void* operator new[](std::size_t size) {
35 void* p = malloc(size);
36 if (p == nullptr) {
37 async_safe_fatal("new[] failed to allocate %zu bytes", size);
38 }
39 return p;
40 }
41
operator delete(void * ptr)42 void operator delete(void* ptr) throw() {
43 free(ptr);
44 }
45
operator delete[](void * ptr)46 void operator delete[](void* ptr) throw() {
47 free(ptr);
48 }
49
operator new(std::size_t size,const std::nothrow_t &)50 void* operator new(std::size_t size, const std::nothrow_t&) {
51 return malloc(size);
52 }
53
operator new[](std::size_t size,const std::nothrow_t &)54 void* operator new[](std::size_t size, const std::nothrow_t&) {
55 return malloc(size);
56 }
57
operator delete(void * ptr,const std::nothrow_t &)58 void operator delete(void* ptr, const std::nothrow_t&) throw() {
59 free(ptr);
60 }
61
operator delete[](void * ptr,const std::nothrow_t &)62 void operator delete[](void* ptr, const std::nothrow_t&) throw() {
63 free(ptr);
64 }
65