diff options
author | 2016-05-28 15:10:28 +1000 | |
---|---|---|
committer | 2016-05-28 15:10:28 +1000 | |
commit | 6b9c752850531d3b3a5d039d4b851a539583accb (patch) | |
tree | 3b9fb8a5df1504ce1870c79ac353a62b49a99a68 | |
parent | 71c500d93174212cc309df13060aa2dd69b04768 (diff) |
misc fixes (nw)
* fix a mismatched new[]/delete error in corealloc
* _name massacre in corealloc while at it
* add template/macro for delaring array with equivalent dimensions
-rw-r--r-- | src/lib/util/corealloc.h | 64 | ||||
-rw-r--r-- | src/osd/osdcomm.h | 11 | ||||
-rw-r--r-- | src/tools/chdman.cpp | 2 |
3 files changed, 38 insertions, 39 deletions
diff --git a/src/lib/util/corealloc.h b/src/lib/util/corealloc.h index b5fc126cba5..12dea2fce67 100644 --- a/src/lib/util/corealloc.h +++ b/src/lib/util/corealloc.h @@ -31,12 +31,12 @@ //************************************************************************** // 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) +#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) @@ -59,54 +59,42 @@ inline T* global_alloc_array_clear(std::size_t num) -template<typename _Tp> -struct _MakeUniqClear -{ - typedef std::unique_ptr<_Tp> __single_object; -}; +template<typename Tp> struct MakeUniqClearT { typedef std::unique_ptr<Tp> single_object; }; -template<typename _Tp> -struct _MakeUniqClear<_Tp[]> -{ - typedef std::unique_ptr<_Tp[]> __array; -}; +template<typename Tp> struct MakeUniqClearT<Tp[]> { typedef std::unique_ptr<Tp[]> array; }; -template<typename _Tp, size_t _Bound> -struct _MakeUniqClear<_Tp[_Bound]> -{ - struct __invalid_type { }; -}; +template<typename Tp, size_t Bound> struct MakeUniqClearT<Tp[Bound]> { struct invalid_type { }; }; /// make_unique_clear for single objects -template<typename _Tp, typename... _Args> -inline typename _MakeUniqClear<_Tp>::__single_object make_unique_clear(_Args&&... __args) +template<typename Tp, typename... Params> +inline typename MakeUniqClearT<Tp>::single_object make_unique_clear(Params&&... args) { - unsigned char* ptr = new unsigned char[sizeof(_Tp)]; // allocate memory - memset(ptr, 0, sizeof(_Tp)); - return std::unique_ptr<_Tp>(new(ptr) _Tp(std::forward<_Args>(__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)...)); } /// make_unique_clear for arrays of unknown bound -template<typename _Tp> -inline typename _MakeUniqClear<_Tp>::__array make_unique_clear(size_t __num) +template<typename Tp> +inline typename MakeUniqClearT<Tp>::array make_unique_clear(size_t num) { - auto size = sizeof(std::remove_extent_t<_Tp>) * __num; + auto size = sizeof(std::remove_extent_t<Tp>) * num; unsigned char* ptr = new unsigned char[size]; // allocate memory - memset(ptr, 0, size); - return std::unique_ptr<_Tp>(new(ptr) std::remove_extent_t<_Tp>[__num]()); + 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 _MakeUniqClear<_Tp>::__array make_unique_clear(size_t __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; + auto size = sizeof(std::remove_extent_t<Tp>) * num; unsigned char* ptr = new unsigned char[size]; // allocate memory - memset(ptr, _F, size); - return std::unique_ptr<_Tp>(new(ptr) std::remove_extent_t<_Tp>[__num]()); + std::memset(ptr, F, size); + 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... _Args> -inline typename _MakeUniqClear<_Tp>::__invalid_type make_unique_clear(_Args&&...) = delete; +template<typename Tp, typename... Params> +inline typename MakeUniqClearT<Tp>::invalid_type make_unique_clear(Params&&...) = delete; #endif // MAME_LIB_UTIL_COREALLOC_H diff --git a/src/osd/osdcomm.h b/src/osd/osdcomm.h index a376ee6b9bd..ff8ca667713 100644 --- a/src/osd/osdcomm.h +++ b/src/osd/osdcomm.h @@ -17,6 +17,8 @@ #include <stdio.h> #include <string.h> #include <cstdint> +#include <type_traits> + /*************************************************************************** COMPILER-SPECIFIC NASTINESS @@ -131,6 +133,15 @@ using FPTR = uintptr_t; // Highly useful template for compile-time knowledge of an array size template <typename T, size_t N> constexpr inline size_t ARRAY_LENGTH(T (&)[N]) { return N;} +// For declaring an array of the same dimensions as another array (including multi-dimensional arrays) +template <typename T, typename U> struct equivalent_array_or_type { typedef T type; }; +template <typename T, typename U, std::size_t N> struct equivalent_array_or_type<T, U[N]> { typedef typename equivalent_array_or_type<T, U>::type type[N]; }; +template <typename T, typename U> using equivalent_array_or_type_t = typename equivalent_array_or_type<T, U>::type; +template <typename T, typename U> struct equivalent_array { }; +template <typename T, typename U, std::size_t N> struct equivalent_array<T, U[N]> { typedef equivalent_array_or_type_t<T, U> type[N]; }; +template <typename T, typename U> using equivalent_array_t = typename equivalent_array<T, U>::type; +#define EQUIVALENT_ARRAY(a, T) equivalent_array_t<T, std::remove_reference_t<decltype(a)> > + /* Macros for normalizing data into big or little endian formats */ #define FLIPENDIAN_INT16(x) (((((UINT16) (x)) >> 8) | ((x) << 8)) & 0xffff) diff --git a/src/tools/chdman.cpp b/src/tools/chdman.cpp index addfe14a913..ee2bae66779 100644 --- a/src/tools/chdman.cpp +++ b/src/tools/chdman.cpp @@ -471,7 +471,7 @@ public: // loop over channels and read the samples int channels = MIN(m_info.channels, ARRAY_LENGTH(m_audio)); - INT16 *samplesptr[ARRAY_LENGTH(m_audio)]; + EQUIVALENT_ARRAY(m_audio, INT16 *) samplesptr; for (int chnum = 0; chnum < channels; chnum++) { // read the sound samples |