diff options
| author | 2022-04-03 02:53:19 +1000 | |
|---|---|---|
| committer | 2022-04-03 02:53:19 +1000 | |
| commit | c4f9ff9790382c1c5b99a2ffbb2a14e04d4c7cea (patch) | |
| tree | 80a1185e2dbed843500eda4e740dfac9f03eba1b /src/lib | |
| parent | 5255f96203e4dc58ba660f74a64e6b640016a604 (diff) | |
-util/corealloc.h: Reduced make_unique_clear to a single variant for POD arrays.
* Enabled GCC lifetime dead store elimination optimisation.
* emu/device.h: Don't pre-clear memory for drivers. Ivan Vangelista
fixed at least the majority of things that crashed outright, and
Robbbert initialised variables that coverity complained about. It's
unlikely anything will break due to this.
* sound/discrete.h: Explicitly initialise members of discrete "devices"
to zero. I don't see a way around doing this in headers due to the
macro soup used to build the constructors.
* sound/mos6581.cpp: Moved creation of the SID core to device_start and
explictly initialised members of the SID core structures. These
structures are in internal headers, so they won't cause downstream
recompiles.
-Lua engine: Made I/O port manager type_seq a bit more tolerant of
omitted arguments.
Diffstat (limited to 'src/lib')
| -rw-r--r-- | src/lib/formats/cassimg.cpp | 4 | ||||
| -rw-r--r-- | src/lib/util/corealloc.h | 45 |
2 files changed, 16 insertions, 33 deletions
diff --git a/src/lib/formats/cassimg.cpp b/src/lib/formats/cassimg.cpp index 2fab4a1ec8b..38be38db9b1 100644 --- a/src/lib/formats/cassimg.cpp +++ b/src/lib/formats/cassimg.cpp @@ -11,7 +11,7 @@ #include "cassimg.h" #include "imageutl.h" -#include "corealloc.h" // make_unique_clear +#include "corealloc.h" // util::make_unique_clear #include <algorithm> #include <cassert> @@ -191,7 +191,7 @@ cassette_image::error cassette_image::lookup_sample(int channel, size_t sample, m_blocks.resize(sample_blocknum + 1); if (!m_blocks[sample_blocknum]) - m_blocks[sample_blocknum] = make_unique_clear<int32_t []>(SAMPLES_PER_BLOCK); + m_blocks[sample_blocknum] = util::make_unique_clear<int32_t []>(SAMPLES_PER_BLOCK); ptr = &m_blocks[sample_blocknum][sample_index]; return error::SUCCESS; diff --git a/src/lib/util/corealloc.h b/src/lib/util/corealloc.h index d70a0909f2b..c6c954fa2cf 100644 --- a/src/lib/util/corealloc.h +++ b/src/lib/util/corealloc.h @@ -13,54 +13,37 @@ #pragma once +#include <cassert> #include <cstddef> -#include <cstdlib> #include <cstring> #include <memory> #include <new> #include <type_traits> -#include <utility> - // global allocation helpers -template<typename Tp> struct MakeUniqClearT { typedef std::unique_ptr<Tp> single_object; }; +namespace util { -template<typename Tp> struct MakeUniqClearT<Tp[]> { typedef std::unique_ptr<Tp[]> array; }; +namespace detail { -template<typename Tp, size_t Bound> struct MakeUniqClearT<Tp[Bound]> { struct invalid_type { }; }; +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]> { }; -/// 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 |
