1 //===-------------------------- rpnew.h -----------------*- 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 // This library provides a cross-platform lock free thread caching malloc
10 // implementation in C11.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifdef __cplusplus
15
16 #include <new>
17 #include <rpmalloc.h>
18
19 #ifndef __CRTDECL
20 #define __CRTDECL
21 #endif
22
delete(void * p)23 extern void __CRTDECL operator delete(void *p) noexcept { rpfree(p); }
24
25 extern void __CRTDECL operator delete[](void *p) noexcept { rpfree(p); }
26
new(std::size_t size)27 extern void *__CRTDECL operator new(std::size_t size) noexcept(false) {
28 return rpmalloc(size);
29 }
30
noexcept(false)31 extern void *__CRTDECL operator new[](std::size_t size) noexcept(false) {
32 return rpmalloc(size);
33 }
34
new(std::size_t size,const std::nothrow_t & tag)35 extern void *__CRTDECL operator new(std::size_t size,
36 const std::nothrow_t &tag) noexcept {
37 (void)sizeof(tag);
38 return rpmalloc(size);
39 }
40
41 extern void *__CRTDECL operator new[](std::size_t size,
42 const std::nothrow_t &tag) noexcept {
43 (void)sizeof(tag);
44 return rpmalloc(size);
45 }
46
47 #if (__cplusplus >= 201402L || _MSC_VER >= 1916)
48
delete(void * p,std::size_t size)49 extern void __CRTDECL operator delete(void *p, std::size_t size) noexcept {
50 (void)sizeof(size);
51 rpfree(p);
52 }
53
54 extern void __CRTDECL operator delete[](void *p, std::size_t size) noexcept {
55 (void)sizeof(size);
56 rpfree(p);
57 }
58
59 #endif
60
61 #if (__cplusplus > 201402L || defined(__cpp_aligned_new))
62
delete(void * p,std::align_val_t align)63 extern void __CRTDECL operator delete(void *p,
64 std::align_val_t align) noexcept {
65 (void)sizeof(align);
66 rpfree(p);
67 }
68
69 extern void __CRTDECL operator delete[](void *p,
70 std::align_val_t align) noexcept {
71 (void)sizeof(align);
72 rpfree(p);
73 }
74
delete(void * p,std::size_t size,std::align_val_t align)75 extern void __CRTDECL operator delete(void *p, std::size_t size,
76 std::align_val_t align) noexcept {
77 (void)sizeof(size);
78 (void)sizeof(align);
79 rpfree(p);
80 }
81
82 extern void __CRTDECL operator delete[](void *p, std::size_t size,
83 std::align_val_t align) noexcept {
84 (void)sizeof(size);
85 (void)sizeof(align);
86 rpfree(p);
87 }
88
new(std::size_t size,std::align_val_t align)89 extern void *__CRTDECL operator new(std::size_t size,
90 std::align_val_t align) noexcept(false) {
91 return rpaligned_alloc(static_cast<size_t>(align), size);
92 }
93
94 extern void *__CRTDECL operator new[](std::size_t size,
noexcept(false)95 std::align_val_t align) noexcept(false) {
96 return rpaligned_alloc(static_cast<size_t>(align), size);
97 }
98
new(std::size_t size,std::align_val_t align,const std::nothrow_t & tag)99 extern void *__CRTDECL operator new(std::size_t size, std::align_val_t align,
100 const std::nothrow_t &tag) noexcept {
101 (void)sizeof(tag);
102 return rpaligned_alloc(static_cast<size_t>(align), size);
103 }
104
105 extern void *__CRTDECL operator new[](std::size_t size, std::align_val_t align,
106 const std::nothrow_t &tag) noexcept {
107 (void)sizeof(tag);
108 return rpaligned_alloc(static_cast<size_t>(align), size);
109 }
110
111 #endif
112
113 #endif
114