From cb231cea0ada42a372cd50a7513c082cb230e5aa Mon Sep 17 00:00:00 2001 From: Vas Crabb Date: Mon, 21 Mar 2016 03:57:54 +1100 Subject: [PATCH] Fix many valgrind Mismatched free() / delete / delete [] errors --- src/lib/util/corealloc.h | 37 +++++++++++++++++++++---------------- 1 file changed, 21 insertions(+), 16 deletions(-) diff --git a/src/lib/util/corealloc.h b/src/lib/util/corealloc.h index 2c1df6dd46c..b5fc126cba5 100644 --- a/src/lib/util/corealloc.h +++ b/src/lib/util/corealloc.h @@ -10,15 +10,20 @@ #pragma once -#ifndef __COREALLOC_H__ -#define __COREALLOC_H__ +#ifndef MAME_LIB_UTIL_COREALLOC_H +#define MAME_LIB_UTIL_COREALLOC_H + +#include "osdcore.h" #include + +#include +#include #include +#include #include #include -#include -#include "osdcore.h" + //************************************************************************** @@ -35,21 +40,21 @@ -template -inline _Tp* global_alloc_clear(_Args&&... __args) +template +inline T* global_alloc_clear(Params &&... args) { - unsigned char * ptr = new unsigned char[sizeof(_Tp)]; // allocate memory - memset(ptr, 0, sizeof(_Tp)); - return new(ptr) _Tp(std::forward<_Args>(__args)...); + void *const ptr = ::operator new(sizeof(T)); // allocate memory + std::memset(ptr, 0, sizeof(T)); + return new(ptr) T(std::forward(args)...); } -template -inline _Tp* global_alloc_array_clear(size_t __num) +template +inline T* global_alloc_array_clear(std::size_t num) { - auto size = sizeof(_Tp) * __num; - unsigned char* ptr = new unsigned char[size]; // allocate memory - memset(ptr, 0, size); - return new(ptr) _Tp[__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](); } @@ -104,4 +109,4 @@ inline typename _MakeUniqClear<_Tp>::__array make_unique_clear(size_t __num) template inline typename _MakeUniqClear<_Tp>::__invalid_type make_unique_clear(_Args&&...) = delete; -#endif /* __COREALLOC_H__ */ +#endif // MAME_LIB_UTIL_COREALLOC_H