summaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
author Vas Crabb <vas@vastheman.com>2016-03-21 03:57:54 +1100
committer Vas Crabb <vas@vastheman.com>2016-03-21 03:58:09 +1100
commitcb231cea0ada42a372cd50a7513c082cb230e5aa (patch)
tree10cbfd98d21565a80e24e8f9cb08e990fcdb83a7
parentfff33e90f8c310995379d6dd3482bd93cb27901d (diff)
Fix many valgrind Mismatched free() / delete / delete [] errors
-rw-r--r--src/lib/util/corealloc.h37
1 files 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 <stdlib.h>
+
+#include <cstddef>
+#include <cstring>
#include <new>
+#include <memory>
#include <type_traits>
#include <utility>
-#include <memory>
-#include "osdcore.h"
+
//**************************************************************************
@@ -35,21 +40,21 @@
-template<typename _Tp, typename... _Args>
-inline _Tp* global_alloc_clear(_Args&&... __args)
+template<typename T, typename... Params>
+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<Params>(args)...);
}
-template<typename _Tp>
-inline _Tp* global_alloc_array_clear(size_t __num)
+template<typename T>
+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<typename _Tp, typename... _Args>
inline typename _MakeUniqClear<_Tp>::__invalid_type make_unique_clear(_Args&&...) = delete;
-#endif /* __COREALLOC_H__ */
+#endif // MAME_LIB_UTIL_COREALLOC_H