/* * mptAlloc.h * ---------- * Purpose: Dynamic memory allocation. * Notes : (currently none) * Authors: OpenMPT Devs * The OpenMPT source code is released under the BSD license. Read LICENSE for more details. */ #pragma once #include "BuildSettings.h" #include "mptBaseMacros.h" #include "mptMemory.h" #include "mptSpan.h" #include #include #include #include OPENMPT_NAMESPACE_BEGIN namespace mpt { template inline mpt::span as_span(std::vector & cont) { return mpt::span(cont); } template inline mpt::span as_span(const std::vector & cont) { return mpt::span(cont); } template inline std::vector::type> make_vector(T * beg, T * end) { return std::vector::type>(beg, end); } template inline std::vector::type> make_vector(T * data, std::size_t size) { return std::vector::type>(data, data + size); } template inline std::vector::type> make_vector(mpt::span data) { return std::vector::type>(data.data(), data.data() + data.size()); } template inline std::vector::type> make_vector(T (&arr)[N]) { return std::vector::type>(std::begin(arr), std::end(arr)); } template struct GetRawBytesFunctor> { inline mpt::const_byte_span operator () (const std::vector & v) const { static_assert(mpt::is_binary_safe::type>::value); return mpt::as_span(reinterpret_cast(v.data()), v.size() * sizeof(T)); } inline mpt::byte_span operator () (std::vector & v) const { static_assert(mpt::is_binary_safe::type>::value); return mpt::as_span(reinterpret_cast(v.data()), v.size() * sizeof(T)); } }; template struct GetRawBytesFunctor> { inline mpt::const_byte_span operator () (const std::vector & v) const { static_assert(mpt::is_binary_safe::type>::value); return mpt::as_span(reinterpret_cast(v.data()), v.size() * sizeof(T)); } }; } // namespace mpt #if defined(MPT_ENABLE_ALIGNED_ALLOC) namespace mpt { #if !(MPT_COMPILER_CLANG && defined(__GLIBCXX__)) && !(MPT_COMPILER_CLANG && MPT_OS_MACOSX_OR_IOS) using std::launder; #else template MPT_NOINLINE T* launder(T* p) noexcept { return p; } #endif template struct alignas(static_cast(alignment)) aligned_array : std::array { static_assert(static_cast(alignment) >= alignof(T)); static_assert(((count * sizeof(T)) % static_cast(alignment)) == 0); static_assert(sizeof(std::array) == (sizeof(T) * count)); }; static_assert(sizeof(mpt::aligned_array) == sizeof(std::array)); } // namespace mpt #endif // MPT_ENABLE_ALIGNED_ALLOC OPENMPT_NAMESPACE_END