1 //===-- scudo_new_delete.cpp ------------------------------------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 ///
10 /// Interceptors for operators new and delete.
11 ///
12 //===----------------------------------------------------------------------===//
13
14 #include "scudo_allocator.h"
15
16 #include "interception/interception.h"
17
18 #include <cstddef>
19
20 using namespace __scudo;
21
22 #define CXX_OPERATOR_ATTRIBUTE INTERCEPTOR_ATTRIBUTE
23
24 // Fake std::nothrow_t to avoid including <new>.
25 namespace std {
26 struct nothrow_t {};
27 } // namespace std
28
29 CXX_OPERATOR_ATTRIBUTE
operator new(size_t size)30 void *operator new(size_t size) {
31 return scudoMalloc(size, FromNew);
32 }
33 CXX_OPERATOR_ATTRIBUTE
operator new[](size_t size)34 void *operator new[](size_t size) {
35 return scudoMalloc(size, FromNewArray);
36 }
37 CXX_OPERATOR_ATTRIBUTE
operator new(size_t size,std::nothrow_t const &)38 void *operator new(size_t size, std::nothrow_t const&) {
39 return scudoMalloc(size, FromNew);
40 }
41 CXX_OPERATOR_ATTRIBUTE
operator new[](size_t size,std::nothrow_t const &)42 void *operator new[](size_t size, std::nothrow_t const&) {
43 return scudoMalloc(size, FromNewArray);
44 }
45
46 CXX_OPERATOR_ATTRIBUTE
operator delete(void * ptr)47 void operator delete(void *ptr) NOEXCEPT {
48 return scudoFree(ptr, FromNew);
49 }
50 CXX_OPERATOR_ATTRIBUTE
operator delete[](void * ptr)51 void operator delete[](void *ptr) NOEXCEPT {
52 return scudoFree(ptr, FromNewArray);
53 }
54 CXX_OPERATOR_ATTRIBUTE
operator delete(void * ptr,std::nothrow_t const &)55 void operator delete(void *ptr, std::nothrow_t const&) NOEXCEPT {
56 return scudoFree(ptr, FromNew);
57 }
58 CXX_OPERATOR_ATTRIBUTE
operator delete[](void * ptr,std::nothrow_t const &)59 void operator delete[](void *ptr, std::nothrow_t const&) NOEXCEPT {
60 return scudoFree(ptr, FromNewArray);
61 }
62 CXX_OPERATOR_ATTRIBUTE
operator delete(void * ptr,size_t size)63 void operator delete(void *ptr, size_t size) NOEXCEPT {
64 scudoSizedFree(ptr, size, FromNew);
65 }
66 CXX_OPERATOR_ATTRIBUTE
operator delete[](void * ptr,size_t size)67 void operator delete[](void *ptr, size_t size) NOEXCEPT {
68 scudoSizedFree(ptr, size, FromNewArray);
69 }
70