Skip to content

Commit 8744390

Browse files
committed
Add proxy_lib_new_delete.h
This header provides convenient overrides for the new and delete operations in C++. It should be included in only one source file. Signed-off-by: Lukasz Dorau <[email protected]>
1 parent d019239 commit 8744390

File tree

1 file changed

+153
-0
lines changed

1 file changed

+153
-0
lines changed

include/umf/proxy_lib_new_delete.h

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
/* ----------------------------------------------------------------------------
2+
Copyright (c) 2018-2020 Microsoft Research, Daan Leijen
3+
Copyright (C) 2024 Intel Corporation
4+
5+
This is free software; you can redistribute it and/or modify it under the
6+
terms of the MIT license:
7+
8+
MIT License
9+
10+
Permission is hereby granted, free of charge, to any person obtaining a copy
11+
of this software and associated documentation files (the "Software"), to deal
12+
in the Software without restriction, including without limitation the rights
13+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14+
copies of the Software, and to permit persons to whom the Software is
15+
furnished to do so, subject to the following conditions:
16+
17+
The above copyright notice and this permission notice shall be included in all
18+
copies or substantial portions of the Software.
19+
20+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26+
SOFTWARE.
27+
28+
-----------------------------------------------------------------------------*/
29+
30+
#ifndef UMF_PROXY_LIB_NEW_DELETE_H
31+
#define UMF_PROXY_LIB_NEW_DELETE_H
32+
33+
// ----------------------------------------------------------------------------
34+
// This header provides convenient overrides for the new and
35+
// delete operations in C++.
36+
//
37+
// This header should be included in only one source file!
38+
//
39+
// On Windows, or when linking dynamically with UMF, these
40+
// can be more performant than the standard new-delete operations.
41+
// See <https://en.cppreference.com/w/cpp/memory/new/operator_new>
42+
// ---------------------------------------------------------------------------
43+
44+
#if defined(__cplusplus)
45+
#include <new>
46+
47+
#ifndef _WIN32
48+
#include <stdlib.h>
49+
#endif // _WIN32
50+
51+
static inline void *internal_aligned_alloc(size_t alignment, size_t size) {
52+
#ifdef _WIN32
53+
return _aligned_malloc(size, alignment);
54+
#else
55+
return aligned_alloc(alignment, size);
56+
#endif // _WIN32
57+
}
58+
59+
#if defined(_MSC_VER) && defined(_Ret_notnull_) && \
60+
defined(_Post_writable_byte_size_)
61+
// stay consistent with VCRT definitions
62+
#define decl_new(n) [[nodiscard]] _Ret_notnull_ _Post_writable_byte_size_(n)
63+
#define decl_new_nothrow(n) \
64+
[[nodiscard]] _Ret_maybenull_ _Success_(return != NULL) \
65+
_Post_writable_byte_size_(n)
66+
#else
67+
#define decl_new(n) [[nodiscard]]
68+
#define decl_new_nothrow(n) [[nodiscard]]
69+
#endif // defined(_MSC_VER) && defined(_Ret_notnull_) && defined(_Post_writable_byte_size_)
70+
71+
void operator delete(void *p) noexcept { free(p); };
72+
void operator delete[](void *p) noexcept { free(p); };
73+
74+
void operator delete(void *p, const std::nothrow_t &) noexcept { free(p); }
75+
void operator delete[](void *p, const std::nothrow_t &) noexcept { free(p); }
76+
77+
decl_new(n) void *operator new(std::size_t n) noexcept(false) {
78+
return malloc(n);
79+
}
80+
decl_new(n) void *operator new[](std::size_t n) noexcept(false) {
81+
return malloc(n);
82+
}
83+
84+
decl_new_nothrow(n) void *operator new(std::size_t n,
85+
const std::nothrow_t &tag) noexcept {
86+
(void)(tag);
87+
return malloc(n);
88+
}
89+
decl_new_nothrow(n) void *operator new[](std::size_t n,
90+
const std::nothrow_t &tag) noexcept {
91+
(void)(tag);
92+
return malloc(n);
93+
}
94+
95+
#if (__cplusplus >= 201402L || _MSC_VER >= 1916)
96+
void operator delete(void *p, std::size_t n) noexcept {
97+
(void)(n);
98+
free(p);
99+
};
100+
void operator delete[](void *p, std::size_t n) noexcept {
101+
(void)(n);
102+
free(p);
103+
};
104+
#endif // (__cplusplus >= 201402L || _MSC_VER >= 1916)
105+
106+
#if (__cplusplus > 201402L || defined(__cpp_aligned_new))
107+
void operator delete(void *p, std::align_val_t al) noexcept {
108+
(void)(al);
109+
free(p);
110+
}
111+
void operator delete[](void *p, std::align_val_t al) noexcept {
112+
(void)(al);
113+
free(p);
114+
}
115+
void operator delete(void *p, std::size_t n, std::align_val_t al) noexcept {
116+
(void)(n);
117+
(void)(al);
118+
free(p);
119+
};
120+
void operator delete[](void *p, std::size_t n, std::align_val_t al) noexcept {
121+
(void)(n);
122+
(void)(al);
123+
free(p);
124+
};
125+
void operator delete(void *p, std::align_val_t al,
126+
const std::nothrow_t &) noexcept {
127+
(void)(al);
128+
free(p);
129+
}
130+
void operator delete[](void *p, std::align_val_t al,
131+
const std::nothrow_t &) noexcept {
132+
(void)(al);
133+
free(p);
134+
}
135+
136+
void *operator new(std::size_t n, std::align_val_t al) noexcept(false) {
137+
return internal_aligned_alloc(static_cast<size_t>(al), n);
138+
}
139+
void *operator new[](std::size_t n, std::align_val_t al) noexcept(false) {
140+
return internal_aligned_alloc(static_cast<size_t>(al), n);
141+
}
142+
void *operator new(std::size_t n, std::align_val_t al,
143+
const std::nothrow_t &) noexcept {
144+
return internal_aligned_alloc(static_cast<size_t>(al), n);
145+
}
146+
void *operator new[](std::size_t n, std::align_val_t al,
147+
const std::nothrow_t &) noexcept {
148+
return internal_aligned_alloc(static_cast<size_t>(al), n);
149+
}
150+
#endif // (__cplusplus > 201402L || defined(__cpp_aligned_new))
151+
#endif // defined(__cplusplus)
152+
153+
#endif // UMF_PROXY_LIB_NEW_DELETE_H

0 commit comments

Comments
 (0)