Common: Add aligned_unique_ptr

This commit is contained in:
Stenzek 2024-09-01 13:02:58 +10:00
parent 07d1c6ab14
commit a5bf1ba5ce
No known key found for this signature in database

View file

@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: 2019-2022 Connor McLaughlin <stenzek@gmail.com> // SPDX-FileCopyrightText: 2019-2024 Connor McLaughlin <stenzek@gmail.com>
// SPDX-License-Identifier: (GPL-3.0 OR CC-BY-NC-ND-4.0) // SPDX-License-Identifier: (GPL-3.0 OR CC-BY-NC-ND-4.0)
#pragma once #pragma once
@ -6,6 +6,8 @@
#include "types.h" #include "types.h"
#include <cstdlib> #include <cstdlib>
#include <memory>
#include <type_traits>
#ifdef _MSC_VER #ifdef _MSC_VER
#include <malloc.h> #include <malloc.h>
@ -109,4 +111,46 @@ ALWAYS_INLINE static void AlignedFree(void* ptr)
#endif #endif
} }
namespace detail {
template<class T>
struct unique_aligned_ptr_deleter
{
ALWAYS_INLINE void operator()(T* ptr) { Common::AlignedFree(ptr); }
};
template<class>
constexpr bool is_unbounded_array_v = false;
template<class T>
constexpr bool is_unbounded_array_v<T[]> = true;
template<class>
constexpr bool is_bounded_array_v = false;
template<class T, std::size_t N>
constexpr bool is_bounded_array_v<T[N]> = true;
} // namespace detail
template<class T>
using unique_aligned_ptr = std::unique_ptr<T, detail::unique_aligned_ptr_deleter<std::remove_extent_t<T>>>;
template<class T, class... Args>
requires(std::is_unbounded_array_v<T>, std::is_trivially_default_constructible_v<std::remove_extent_t<T>>,
std::is_trivially_destructible_v<std::remove_extent_t<T>>)
unique_aligned_ptr<T> make_unique_aligned(size_t alignment, size_t n)
{
unique_aligned_ptr<T> ptr(
static_cast<std::remove_extent_t<T>*>(AlignedMalloc(sizeof(std::remove_extent_t<T>) * n, alignment)));
if (ptr)
new (ptr.get()) std::remove_extent_t<T>[ n ]();
return ptr;
}
template<class T, class... Args>
requires(std::is_unbounded_array_v<T>, std::is_trivially_default_constructible_v<std::remove_extent_t<T>>,
std::is_trivially_destructible_v<std::remove_extent_t<T>>)
unique_aligned_ptr<T> make_unique_aligned_for_overwrite(size_t alignment, size_t n)
{
return unique_aligned_ptr<T>(
static_cast<std::remove_extent_t<T>*>(AlignedMalloc(sizeof(std::remove_extent_t<T>) * n, alignment)));
}
} // namespace Common } // namespace Common