Skylicht Engine
Loading...
Searching...
No Matches
NewOperator.h
1/*
2!@
3MIT License
4
5Copyright (c) 2026 Skylicht Technology CO., LTD
6
7Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files
8(the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify,
9merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
10subject to the following conditions:
11
12The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
13
14THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
15INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
16IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
17WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19
20This file is part of the "Skylicht Engine".
21https://github.com/skylicht-lab/skylicht-engine
22!#
23*/
24
25#pragma once
26
27#ifdef USE_SHARED_HEAP_MEMORY
28
29#include <cstddef>
30#include <cstdint>
31#include <limits>
32#include <windows.h>
33#include <new>
34
35namespace Skylicht
36{
37 namespace Heap
38 {
39 inline size_t normalizeSize(size_t size) noexcept
40 {
41 return size == 0 ? 1 : size;
42 }
43
44 inline size_t normalizeAlignment(size_t alignment) noexcept
45 {
46 const size_t minAlignment = alignof(void*);
47 return alignment < minAlignment ? minAlignment : alignment;
48 }
49
50 inline void* allocRawNoThrow(size_t size) noexcept
51 {
52 return ::HeapAlloc(::GetProcessHeap(), 0, normalizeSize(size));
53 }
54
55 inline void* allocRaw(size_t size)
56 {
57 for (;;)
58 {
59 if (void* p = allocRawNoThrow(size))
60 return p;
61
62 std::new_handler handler = std::get_new_handler();
63 if (handler == NULL)
64 throw std::bad_alloc();
65
66 handler();
67 }
68 }
69
70 inline void freeRaw(void* p) noexcept
71 {
72 if (p != NULL)
73 ::HeapFree(::GetProcessHeap(), 0, p);
74 }
75
76 inline void* allocAlignedNoThrow(size_t size, size_t alignment) noexcept
77 {
78 size = normalizeSize(size);
79 alignment = normalizeAlignment(alignment);
80
81 const size_t extra = alignment - 1 + sizeof(void*);
82
83 void* base = ::HeapAlloc(::GetProcessHeap(), 0, size + extra);
84 if (base == NULL)
85 return NULL;
86
87 const uintptr_t raw = reinterpret_cast<uintptr_t>(base) + sizeof(void*);
88 const uintptr_t aligned = (raw + alignment - 1) & ~(static_cast<uintptr_t>(alignment) - 1);
89
90 void* p = reinterpret_cast<void*>(aligned);
91 reinterpret_cast<void**>(p)[-1] = base;
92 return p;
93 }
94
95 inline void* allocAligned(size_t size, size_t alignment)
96 {
97 for (;;)
98 {
99 if (void* p = allocAlignedNoThrow(size, alignment))
100 return p;
101
102 std::new_handler handler = std::get_new_handler();
103 if (handler == NULL)
104 throw std::bad_alloc();
105
106 handler();
107 }
108 }
109
110 inline void freeAligned(void* p) noexcept
111 {
112 if (p == NULL)
113 return;
114
115 void* base = reinterpret_cast<void**>(p)[-1];
116 ::HeapFree(::GetProcessHeap(), 0, base);
117 }
118 }
119}
120
121inline void* operator new(size_t size)
122{
123 return Skylicht::Heap::allocRaw(size);
124}
125
126inline void operator delete(void* p) noexcept
127{
128 Skylicht::Heap::freeRaw(p);
129}
130
131inline void* operator new[](size_t size)
132{
133 return Skylicht::Heap::allocRaw(size);
134}
135
136inline void operator delete[](void* p) noexcept
137{
138 Skylicht::Heap::freeRaw(p);
139}
140
141inline void* operator new(size_t size, const std::nothrow_t&) noexcept
142{
143 return Skylicht::Heap::allocRawNoThrow(size);
144}
145
146inline void operator delete(void* p, const std::nothrow_t&) noexcept
147{
148 Skylicht::Heap::freeRaw(p);
149}
150
151inline void* operator new[](size_t size, const std::nothrow_t&) noexcept
152{
153 return Skylicht::Heap::allocRawNoThrow(size);
154}
155
156inline void operator delete[](void* p, const std::nothrow_t&) noexcept
157{
158 Skylicht::Heap::freeRaw(p);
159}
160
161#if defined(__cpp_sized_deallocation) || defined(_MSC_VER)
162inline void operator delete(void* p, size_t) noexcept
163{
164 Skylicht::Heap::freeRaw(p);
165}
166
167inline void operator delete[](void* p, size_t) noexcept
168{
169 Skylicht::Heap::freeRaw(p);
170}
171#endif
172
173#if defined(__cpp_aligned_new)
174inline void* operator new(size_t size, std::align_val_t alignment)
175{
176 return Skylicht::Heap::allocAligned(size, static_cast<size_t>(alignment));
177}
178
179inline void operator delete(void* p, std::align_val_t) noexcept
180{
181 Skylicht::Heap::freeAligned(p);
182}
183
184inline void* operator new[](size_t size, std::align_val_t alignment)
185{
186 return Skylicht::Heap::allocAligned(size, static_cast<size_t>(alignment));
187}
188
189inline void operator delete[](void* p, std::align_val_t) noexcept
190{
191 Skylicht::Heap::freeAligned(p);
192}
193
194inline void* operator new(size_t size, std::align_val_t alignment, const std::nothrow_t&) noexcept
195{
196 return Skylicht::Heap::allocAlignedNoThrow(size, static_cast<size_t>(alignment));
197}
198
199inline void operator delete(void* p, std::align_val_t, const std::nothrow_t&) noexcept
200{
201 Skylicht::Heap::freeAligned(p);
202}
203
204inline void* operator new[](size_t size, std::align_val_t alignment, const std::nothrow_t&) noexcept
205{
206 return Skylicht::Heap::allocAlignedNoThrow(size, static_cast<size_t>(alignment));
207}
208
209inline void operator delete[](void* p, std::align_val_t, const std::nothrow_t&) noexcept
210{
211 Skylicht::Heap::freeAligned(p);
212}
213
214#if defined(__cpp_sized_deallocation) || defined(_MSC_VER)
215inline void operator delete(void* p, size_t, std::align_val_t) noexcept
216{
217 Skylicht::Heap::freeAligned(p);
218}
219
220inline void operator delete[](void* p, size_t, std::align_val_t) noexcept
221{
222 Skylicht::Heap::freeAligned(p);
223}
224#endif
225#endif
226
227#endif
Everything in the Skylicht Engine. You can start by looking at the topics.
Definition AudioDebugLog.h:29