1//===-- wrappers_cpp.cpp ----------------------------------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "platform.h"
10
11// Skip this compilation unit if compiled as part of Bionic.
12#if !SCUDO_ANDROID || !_BIONIC
13
14#include "allocator_config.h"
15#include "internal_defs.h"
16#include "platform.h"
17#include "scudo/interface.h"
18#include "wrappers_c.h"
19
20#include <stdint.h>
21
22namespace std {
23struct nothrow_t {};
24enum class align_val_t : size_t {};
25} // namespace std
26
27static void reportAllocation(void *ptr, size_t size) {
28  if (SCUDO_ENABLE_HOOKS)
29    if (__scudo_allocate_hook && ptr)
30      __scudo_allocate_hook(ptr, size);
31}
32static void reportDeallocation(void *ptr) {
33  if (SCUDO_ENABLE_HOOKS)
34    if (__scudo_deallocate_hook)
35      __scudo_deallocate_hook(ptr);
36}
37
38INTERFACE WEAK void *operator new(size_t size) {
39  void *Ptr = Allocator.allocate(size, scudo::Chunk::Origin::New);
40  reportAllocation(Ptr, size);
41  return Ptr;
42}
43INTERFACE WEAK void *operator new[](size_t size) {
44  void *Ptr = Allocator.allocate(size, scudo::Chunk::Origin::NewArray);
45  reportAllocation(Ptr, size);
46  return Ptr;
47}
48INTERFACE WEAK void *operator new(size_t size,
49                                  std::nothrow_t const &) NOEXCEPT {
50  void *Ptr = Allocator.allocate(size, scudo::Chunk::Origin::New);
51  reportAllocation(Ptr, size);
52  return Ptr;
53}
54INTERFACE WEAK void *operator new[](size_t size,
55                                    std::nothrow_t const &) NOEXCEPT {
56  void *Ptr = Allocator.allocate(size, scudo::Chunk::Origin::NewArray);
57  reportAllocation(Ptr, size);
58  return Ptr;
59}
60INTERFACE WEAK void *operator new(size_t size, std::align_val_t align) {
61  void *Ptr = Allocator.allocate(size, scudo::Chunk::Origin::New,
62                                 static_cast<scudo::uptr>(align));
63  reportAllocation(Ptr, size);
64  return Ptr;
65}
66INTERFACE WEAK void *operator new[](size_t size, std::align_val_t align) {
67  void *Ptr = Allocator.allocate(size, scudo::Chunk::Origin::NewArray,
68                                 static_cast<scudo::uptr>(align));
69  reportAllocation(Ptr, size);
70  return Ptr;
71}
72INTERFACE WEAK void *operator new(size_t size, std::align_val_t align,
73                                  std::nothrow_t const &) NOEXCEPT {
74  void *Ptr = Allocator.allocate(size, scudo::Chunk::Origin::New,
75                                 static_cast<scudo::uptr>(align));
76  reportAllocation(Ptr, size);
77  return Ptr;
78}
79INTERFACE WEAK void *operator new[](size_t size, std::align_val_t align,
80                                    std::nothrow_t const &) NOEXCEPT {
81  void *Ptr = Allocator.allocate(size, scudo::Chunk::Origin::NewArray,
82                                 static_cast<scudo::uptr>(align));
83  reportAllocation(Ptr, size);
84  return Ptr;
85}
86
87INTERFACE WEAK void operator delete(void *ptr) NOEXCEPT {
88  reportDeallocation(ptr);
89  Allocator.deallocate(ptr, scudo::Chunk::Origin::New);
90}
91INTERFACE WEAK void operator delete[](void *ptr) NOEXCEPT {
92  reportDeallocation(ptr);
93  Allocator.deallocate(ptr, scudo::Chunk::Origin::NewArray);
94}
95INTERFACE WEAK void operator delete(void *ptr,
96                                    std::nothrow_t const &) NOEXCEPT {
97  reportDeallocation(ptr);
98  Allocator.deallocate(ptr, scudo::Chunk::Origin::New);
99}
100INTERFACE WEAK void operator delete[](void *ptr,
101                                      std::nothrow_t const &) NOEXCEPT {
102  reportDeallocation(ptr);
103  Allocator.deallocate(ptr, scudo::Chunk::Origin::NewArray);
104}
105INTERFACE WEAK void operator delete(void *ptr, size_t size) NOEXCEPT {
106  reportDeallocation(ptr);
107  Allocator.deallocate(ptr, scudo::Chunk::Origin::New, size);
108}
109INTERFACE WEAK void operator delete[](void *ptr, size_t size) NOEXCEPT {
110  reportDeallocation(ptr);
111  Allocator.deallocate(ptr, scudo::Chunk::Origin::NewArray, size);
112}
113INTERFACE WEAK void operator delete(void *ptr,
114                                    std::align_val_t align) NOEXCEPT {
115  reportDeallocation(ptr);
116  Allocator.deallocate(ptr, scudo::Chunk::Origin::New, 0,
117                       static_cast<scudo::uptr>(align));
118}
119INTERFACE WEAK void operator delete[](void *ptr,
120                                      std::align_val_t align) NOEXCEPT {
121  reportDeallocation(ptr);
122  Allocator.deallocate(ptr, scudo::Chunk::Origin::NewArray, 0,
123                       static_cast<scudo::uptr>(align));
124}
125INTERFACE WEAK void operator delete(void *ptr, std::align_val_t align,
126                                    std::nothrow_t const &) NOEXCEPT {
127  reportDeallocation(ptr);
128  Allocator.deallocate(ptr, scudo::Chunk::Origin::New, 0,
129                       static_cast<scudo::uptr>(align));
130}
131INTERFACE WEAK void operator delete[](void *ptr, std::align_val_t align,
132                                      std::nothrow_t const &) NOEXCEPT {
133  reportDeallocation(ptr);
134  Allocator.deallocate(ptr, scudo::Chunk::Origin::NewArray, 0,
135                       static_cast<scudo::uptr>(align));
136}
137INTERFACE WEAK void operator delete(void *ptr, size_t size,
138                                    std::align_val_t align) NOEXCEPT {
139  reportDeallocation(ptr);
140  Allocator.deallocate(ptr, scudo::Chunk::Origin::New, size,
141                       static_cast<scudo::uptr>(align));
142}
143INTERFACE WEAK void operator delete[](void *ptr, size_t size,
144                                      std::align_val_t align) NOEXCEPT {
145  reportDeallocation(ptr);
146  Allocator.deallocate(ptr, scudo::Chunk::Origin::NewArray, size,
147                       static_cast<scudo::uptr>(align));
148}
149
150#endif // !SCUDO_ANDROID || !_BIONIC
151