cog/Frameworks/OpenMPT.old/OpenMPT/common/mptAlloc.h

126 lines
3.2 KiB
C
Raw Normal View History

2019-01-24 02:16:37 +00:00
/*
* 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"
2020-09-22 04:54:24 +00:00
#include <array>
2019-01-24 02:16:37 +00:00
#include <memory>
2020-09-22 04:54:24 +00:00
#include <new>
2019-01-24 02:16:37 +00:00
#include <vector>
OPENMPT_NAMESPACE_BEGIN
namespace mpt {
2020-09-22 04:54:24 +00:00
template <typename T> inline mpt::span<T> as_span(std::vector<T> & cont) { return mpt::span<T>(cont); }
2019-01-24 02:16:37 +00:00
2020-09-22 04:54:24 +00:00
template <typename T> inline mpt::span<const T> as_span(const std::vector<T> & cont) { return mpt::span<const T>(cont); }
2019-01-24 02:16:37 +00:00
template <typename T> inline std::vector<typename std::remove_const<T>::type> make_vector(T * beg, T * end) { return std::vector<typename std::remove_const<T>::type>(beg, end); }
template <typename T> inline std::vector<typename std::remove_const<T>::type> make_vector(T * data, std::size_t size) { return std::vector<typename std::remove_const<T>::type>(data, data + size); }
template <typename T> inline std::vector<typename std::remove_const<T>::type> make_vector(mpt::span<T> data) { return std::vector<typename std::remove_const<T>::type>(data.data(), data.data() + data.size()); }
template <typename T, std::size_t N> inline std::vector<typename std::remove_const<T>::type> make_vector(T (&arr)[N]) { return std::vector<typename std::remove_const<T>::type>(std::begin(arr), std::end(arr)); }
template <typename T>
struct GetRawBytesFunctor<std::vector<T>>
{
inline mpt::const_byte_span operator () (const std::vector<T> & v) const
{
2020-09-22 04:54:24 +00:00
static_assert(mpt::is_binary_safe<typename std::remove_const<T>::type>::value);
return mpt::as_span(reinterpret_cast<const std::byte *>(v.data()), v.size() * sizeof(T));
2019-01-24 02:16:37 +00:00
}
inline mpt::byte_span operator () (std::vector<T> & v) const
{
2020-09-22 04:54:24 +00:00
static_assert(mpt::is_binary_safe<typename std::remove_const<T>::type>::value);
return mpt::as_span(reinterpret_cast<std::byte *>(v.data()), v.size() * sizeof(T));
2019-01-24 02:16:37 +00:00
}
};
template <typename T>
struct GetRawBytesFunctor<const std::vector<T>>
{
inline mpt::const_byte_span operator () (const std::vector<T> & v) const
{
2020-09-22 04:54:24 +00:00
static_assert(mpt::is_binary_safe<typename std::remove_const<T>::type>::value);
return mpt::as_span(reinterpret_cast<const std::byte *>(v.data()), v.size() * sizeof(T));
2019-01-24 02:16:37 +00:00
}
};
} // namespace mpt
2019-10-25 00:33:28 +00:00
#if defined(MPT_ENABLE_ALIGNED_ALLOC)
2019-01-24 02:16:37 +00:00
namespace mpt
{
2020-09-22 04:54:24 +00:00
#if !(MPT_COMPILER_CLANG && defined(__GLIBCXX__)) && !(MPT_COMPILER_CLANG && MPT_OS_MACOSX_OR_IOS)
2019-01-24 02:16:37 +00:00
using std::launder;
#else
template <class T>
MPT_NOINLINE T* launder(T* p) noexcept
{
return p;
}
#endif
2020-09-22 04:54:24 +00:00
template <typename T, std::size_t count, std::align_val_t alignment>
struct alignas(static_cast<std::size_t>(alignment)) aligned_array
: std::array<T, count>
2019-01-24 02:16:37 +00:00
{
2020-09-22 04:54:24 +00:00
static_assert(static_cast<std::size_t>(alignment) >= alignof(T));
static_assert(((count * sizeof(T)) % static_cast<std::size_t>(alignment)) == 0);
static_assert(sizeof(std::array<T, count>) == (sizeof(T) * count));
2019-01-24 02:16:37 +00:00
};
2020-09-22 04:54:24 +00:00
static_assert(sizeof(mpt::aligned_array<float, 4, std::align_val_t{sizeof(float) * 4}>) == sizeof(std::array<float, 4>));
2019-01-24 02:16:37 +00:00
} // namespace mpt
2019-10-25 00:33:28 +00:00
#endif // MPT_ENABLE_ALIGNED_ALLOC
2019-01-24 02:16:37 +00:00
OPENMPT_NAMESPACE_END