// license:BSD-3-Clause // copyright-holders:Aaron Giles /*************************************************************************** corealloc.h Memory allocation helpers for the helper library. ***************************************************************************/ #ifndef MAME_LIB_UTIL_COREALLOC_H #define MAME_LIB_UTIL_COREALLOC_H #pragma once #include #include #include #include #include #include // global allocation helpers namespace util { namespace detail { template struct make_unique_clear_traits { }; template struct make_unique_clear_traits { using unbounded_array_ptr = std::unique_ptr; }; template struct make_unique_clear_traits { }; } // namespace detail /// make_unique_clear for arrays of unknown bound template inline typename detail::make_unique_clear_traits::unbounded_array_ptr make_unique_clear(size_t num) { static_assert(std::is_trivially_constructible_v >, "make_unique_clear is only suitable for trivially constructible types"); auto const size = sizeof(std::remove_extent_t) * 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(new (ptr) std::remove_extent_t [num]); } } // namespace util #endif // MAME_LIB_UTIL_COREALLOC_H