diff options
Diffstat (limited to 'src/lib/util/corealloc.h')
-rw-r--r-- | src/lib/util/corealloc.h | 85 |
1 files changed, 17 insertions, 68 deletions
diff --git a/src/lib/util/corealloc.h b/src/lib/util/corealloc.h index 12dea2fce67..c6c954fa2cf 100644 --- a/src/lib/util/corealloc.h +++ b/src/lib/util/corealloc.h @@ -8,93 +8,42 @@ ***************************************************************************/ -#pragma once - #ifndef MAME_LIB_UTIL_COREALLOC_H #define MAME_LIB_UTIL_COREALLOC_H -#include "osdcore.h" - -#include <stdlib.h> +#pragma once +#include <cassert> #include <cstddef> #include <cstring> -#include <new> #include <memory> +#include <new> #include <type_traits> -#include <utility> - - - -//************************************************************************** -// MACROS -//************************************************************************** - -// global allocation helpers -- use these instead of new and delete -#define global_alloc(Type) new Type -#define global_alloc_nothrow(Type) new (std::nothrow) Type -#define global_alloc_array(Type, Num) new Type[Num] -#define global_alloc_array_nothrow(Type, Num) new (std::nothrow) Type[Num] -#define global_free(Ptr) do { delete Ptr; } while (0) -#define global_free_array(Ptr) do { delete[] Ptr; } while (0) - - -template<typename T, typename... Params> -inline T* global_alloc_clear(Params &&... args) -{ - void *const ptr = ::operator new(sizeof(T)); // allocate memory - std::memset(ptr, 0, sizeof(T)); - return new(ptr) T(std::forward<Params>(args)...); -} - -template<typename T> -inline T* global_alloc_array_clear(std::size_t num) -{ - auto const size = sizeof(T) * num; - void *const ptr = new unsigned char[size]; // allocate memory - std::memset(ptr, 0, size); - return new(ptr) T[num](); -} +// global allocation helpers +namespace util { -template<typename Tp> struct MakeUniqClearT { typedef std::unique_ptr<Tp> single_object; }; +namespace detail { -template<typename Tp> struct MakeUniqClearT<Tp[]> { typedef std::unique_ptr<Tp[]> array; }; +template <typename Tp> struct make_unique_clear_traits { }; +template <typename Tp> struct make_unique_clear_traits<Tp []> { using unbounded_array_ptr = std::unique_ptr<Tp []>; }; +template <typename Tp, size_t Bound> struct make_unique_clear_traits<Tp [Bound]> { }; -template<typename Tp, size_t Bound> struct MakeUniqClearT<Tp[Bound]> { struct invalid_type { }; }; - -/// make_unique_clear for single objects -template<typename Tp, typename... Params> -inline typename MakeUniqClearT<Tp>::single_object make_unique_clear(Params&&... args) -{ - void *const ptr = ::operator new(sizeof(Tp)); // allocate memory - std::memset(ptr, 0, sizeof(Tp)); - return std::unique_ptr<Tp>(new(ptr) Tp(std::forward<Params>(args)...)); -} +} // namespace detail /// make_unique_clear for arrays of unknown bound -template<typename Tp> -inline typename MakeUniqClearT<Tp>::array make_unique_clear(size_t num) +template <typename Tp> +inline typename detail::make_unique_clear_traits<Tp>::unbounded_array_ptr make_unique_clear(size_t num) { - auto size = sizeof(std::remove_extent_t<Tp>) * num; - unsigned char* ptr = new unsigned char[size]; // allocate memory + static_assert(std::is_trivially_constructible_v<std::remove_extent_t<Tp> >, "make_unique_clear is only suitable for trivially constructible types"); + auto const size = sizeof(std::remove_extent_t<Tp>) * num; + unsigned char* ptr = new unsigned char [size]; // allocate memory - this assumes new expression overhead is the same for all array types std::memset(ptr, 0, size); - return std::unique_ptr<Tp>(new(ptr) std::remove_extent_t<Tp>[num]()); -} - -template<typename Tp, unsigned char F> -inline typename MakeUniqClearT<Tp>::array make_unique_clear(size_t num) -{ - auto size = sizeof(std::remove_extent_t<Tp>) * num; - unsigned char* ptr = new unsigned char[size]; // allocate memory - std::memset(ptr, F, size); - return std::unique_ptr<Tp>(new(ptr) std::remove_extent_t<Tp>[num]()); + return std::unique_ptr<Tp>(new (ptr) std::remove_extent_t<Tp> [num]); } -/// Disable make_unique_clear for arrays of known bound -template<typename Tp, typename... Params> -inline typename MakeUniqClearT<Tp>::invalid_type make_unique_clear(Params&&...) = delete; +} // namespace util #endif // MAME_LIB_UTIL_COREALLOC_H |