1 /*
2 * Copyright (c) 2018
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include <stdlib.h>
8 #include <new>
9
10 #if __cplusplus < 201103L
11 #define NOEXCEPT
12 #else /* >= C++11 */
13 #define NOEXCEPT noexcept
14 #endif /* __cplusplus */
15
16 #if __cplusplus < 202002L
17 #define NODISCARD
18 #else
19 #define NODISCARD [[nodiscard]]
20 #endif /* __cplusplus */
21
operator new(size_t size)22 NODISCARD void* operator new(size_t size)
23 {
24 return malloc(size);
25 }
26
operator new[](size_t size)27 NODISCARD void* operator new[](size_t size)
28 {
29 return malloc(size);
30 }
31
operator new(std::size_t size,const std::nothrow_t & tag)32 NODISCARD void* operator new(std::size_t size, const std::nothrow_t& tag) NOEXCEPT
33 {
34 return malloc(size);
35 }
36
operator new[](std::size_t size,const std::nothrow_t & tag)37 NODISCARD void* operator new[](std::size_t size, const std::nothrow_t& tag) NOEXCEPT
38 {
39 return malloc(size);
40 }
41
42 #if __cplusplus >= 201703L
operator new(size_t size,std::align_val_t al)43 NODISCARD void* operator new(size_t size, std::align_val_t al)
44 {
45 return aligned_alloc(static_cast<size_t>(al), size);
46 }
47
operator new[](std::size_t size,std::align_val_t al)48 NODISCARD void* operator new[](std::size_t size, std::align_val_t al)
49 {
50 return aligned_alloc(static_cast<size_t>(al), size);
51 }
52
operator new(std::size_t size,std::align_val_t al,const std::nothrow_t &)53 NODISCARD void* operator new(std::size_t size, std::align_val_t al,
54 const std::nothrow_t&) NOEXCEPT
55 {
56 return aligned_alloc(static_cast<size_t>(al), size);
57 }
58
operator new[](std::size_t size,std::align_val_t al,const std::nothrow_t &)59 NODISCARD void* operator new[](std::size_t size, std::align_val_t al,
60 const std::nothrow_t&) NOEXCEPT
61 {
62 return aligned_alloc(static_cast<size_t>(al), size);
63 }
64 #endif /* __cplusplus >= 201703L */
65
operator delete(void * ptr)66 void operator delete(void* ptr) NOEXCEPT
67 {
68 free(ptr);
69 }
70
operator delete[](void * ptr)71 void operator delete[](void* ptr) NOEXCEPT
72 {
73 free(ptr);
74 }
75
76 #if (__cplusplus > 201103L)
operator delete(void * ptr,size_t)77 void operator delete(void* ptr, size_t) NOEXCEPT
78 {
79 free(ptr);
80 }
81
operator delete[](void * ptr,size_t)82 void operator delete[](void* ptr, size_t) NOEXCEPT
83 {
84 free(ptr);
85 }
86 #endif // __cplusplus > 201103L
87