Skip to content

Commit ddc5a62

Browse files
<xmemory>: Unify overloads of _Allocate and _Deallocate with if constexpr (#4432)
Co-authored-by: Stephan T. Lavavej <[email protected]>
1 parent 46f6bc5 commit ddc5a62

File tree

1 file changed

+28
-51
lines changed

1 file changed

+28
-51
lines changed

stl/inc/xmemory

Lines changed: 28 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -190,21 +190,21 @@ inline void _Adjust_manually_vector_aligned(void*& _Ptr, size_t& _Bytes) {
190190
}
191191
#endif // defined(_M_IX86) || defined(_M_X64)
192192

193-
#ifdef __cpp_aligned_new
194-
template <size_t _Align, class _Traits = _Default_allocate_traits,
195-
enable_if_t<(_Align > __STDCPP_DEFAULT_NEW_ALIGNMENT__), int> = 0>
193+
template <size_t _Align, class _Traits = _Default_allocate_traits>
196194
__declspec(allocator) _CONSTEXPR20 void* _Allocate(const size_t _Bytes) {
197-
// allocate _Bytes when __cpp_aligned_new && _Align > __STDCPP_DEFAULT_NEW_ALIGNMENT__
195+
// allocate _Bytes
198196
if (_Bytes == 0) {
199197
return nullptr;
200198
}
201199

202200
#if _HAS_CXX20 // TRANSITION, GH-1532
203201
if (_STD is_constant_evaluated()) {
204202
return _Traits::_Allocate(_Bytes);
205-
} else
203+
}
206204
#endif // _HAS_CXX20
207-
{
205+
206+
#ifdef __cpp_aligned_new
207+
if constexpr (_Align > __STDCPP_DEFAULT_NEW_ALIGNMENT__) {
208208
size_t _Passed_align = _Align;
209209
#if defined(_M_IX86) || defined(_M_X64)
210210
if (_Bytes >= _Big_allocation_threshold) {
@@ -213,75 +213,52 @@ __declspec(allocator) _CONSTEXPR20 void* _Allocate(const size_t _Bytes) {
213213
}
214214
#endif // defined(_M_IX86) || defined(_M_X64)
215215
return _Traits::_Allocate_aligned(_Bytes, _Passed_align);
216-
}
217-
}
218-
219-
template <size_t _Align, enable_if_t<(_Align > __STDCPP_DEFAULT_NEW_ALIGNMENT__), int> = 0>
220-
_CONSTEXPR20 void _Deallocate(void* _Ptr, const size_t _Bytes) noexcept {
221-
// deallocate storage allocated by _Allocate when __cpp_aligned_new && _Align > __STDCPP_DEFAULT_NEW_ALIGNMENT__
222-
#if _HAS_CXX20 // TRANSITION, GH-1532
223-
if (_STD is_constant_evaluated()) {
224-
::operator delete(_Ptr);
225216
} else
226-
#endif // _HAS_CXX20
217+
#endif // defined(__cpp_aligned_new)
227218
{
228-
size_t _Passed_align = _Align;
229219
#if defined(_M_IX86) || defined(_M_X64)
230-
if (_Bytes >= _Big_allocation_threshold) { // boost the alignment of big allocations to help autovectorization
231-
_Passed_align = (_STD max)(_Align, _Big_allocation_alignment);
232-
}
233-
#endif // defined(_M_IX86) || defined(_M_X64)
234-
::operator delete(_Ptr, _Bytes, align_val_t{_Passed_align});
235-
}
236-
}
237-
238-
#define _HAS_ALIGNED_NEW 1
239-
#else // ^^^ defined(__cpp_aligned_new) / !defined(__cpp_aligned_new) vvv
240-
#define _HAS_ALIGNED_NEW 0
241-
#endif // ^^^ !defined(__cpp_aligned_new) ^^^
242-
243-
template <size_t _Align, class _Traits = _Default_allocate_traits,
244-
enable_if_t<(!_HAS_ALIGNED_NEW || _Align <= __STDCPP_DEFAULT_NEW_ALIGNMENT__), int> = 0>
245-
__declspec(allocator) _CONSTEXPR20 void* _Allocate(const size_t _Bytes) {
246-
// allocate _Bytes when !_HAS_ALIGNED_NEW || _Align <= __STDCPP_DEFAULT_NEW_ALIGNMENT__
247-
#if defined(_M_IX86) || defined(_M_X64)
248-
#if _HAS_CXX20 // TRANSITION, GH-1532
249-
if (!_STD is_constant_evaluated())
250-
#endif // _HAS_CXX20
251-
{
252-
if (_Bytes >= _Big_allocation_threshold) { // boost the alignment of big allocations to help autovectorization
220+
if (_Bytes >= _Big_allocation_threshold) {
221+
// boost the alignment of big allocations to help autovectorization
253222
return _Allocate_manually_vector_aligned<_Traits>(_Bytes);
254223
}
255-
}
256224
#endif // defined(_M_IX86) || defined(_M_X64)
257-
258-
if (_Bytes != 0) {
259225
return _Traits::_Allocate(_Bytes);
260226
}
261-
262-
return nullptr;
263227
}
264228

265-
template <size_t _Align, enable_if_t<(!_HAS_ALIGNED_NEW || _Align <= __STDCPP_DEFAULT_NEW_ALIGNMENT__), int> = 0>
229+
template <size_t _Align>
266230
_CONSTEXPR20 void _Deallocate(void* _Ptr, size_t _Bytes) noexcept {
267-
// deallocate storage allocated by _Allocate when !_HAS_ALIGNED_NEW || _Align <= __STDCPP_DEFAULT_NEW_ALIGNMENT__
231+
// deallocate storage allocated by _Allocate
268232
#if _HAS_CXX20 // TRANSITION, GH-1532
269233
if (_STD is_constant_evaluated()) {
270234
::operator delete(_Ptr);
271-
} else
235+
return;
236+
}
272237
#endif // _HAS_CXX20
238+
239+
#ifdef __cpp_aligned_new
240+
if constexpr (_Align > __STDCPP_DEFAULT_NEW_ALIGNMENT__) {
241+
size_t _Passed_align = _Align;
242+
#if defined(_M_IX86) || defined(_M_X64)
243+
if (_Bytes >= _Big_allocation_threshold) {
244+
// boost the alignment of big allocations to help autovectorization
245+
_Passed_align = (_STD max)(_Align, _Big_allocation_alignment);
246+
}
247+
#endif // defined(_M_IX86) || defined(_M_X64)
248+
::operator delete(_Ptr, _Bytes, align_val_t{_Passed_align});
249+
} else
250+
#endif // defined(__cpp_aligned_new)
273251
{
274252
#if defined(_M_IX86) || defined(_M_X64)
275-
if (_Bytes >= _Big_allocation_threshold) { // boost the alignment of big allocations to help autovectorization
253+
if (_Bytes >= _Big_allocation_threshold) {
254+
// boost the alignment of big allocations to help autovectorization
276255
_Adjust_manually_vector_aligned(_Ptr, _Bytes);
277256
}
278257
#endif // defined(_M_IX86) || defined(_M_X64)
279258
::operator delete(_Ptr, _Bytes);
280259
}
281260
}
282261

283-
#undef _HAS_ALIGNED_NEW
284-
285262
template <class _Ptr, class _Ty>
286263
using _Rebind_pointer_t = typename pointer_traits<_Ptr>::template rebind<_Ty>;
287264

0 commit comments

Comments
 (0)