diff options
author | 2020-10-03 02:50:49 +1000 | |
---|---|---|
committer | 2020-10-03 03:00:24 +1000 | |
commit | 4f05917494b22943ab0e7c466e4eb9f7a5896daa (patch) | |
tree | b05a6b7820548f21ab8df21c4e4ca034eff0659f /src/lib/util/corealloc.h | |
parent | 3340fc0d2171011e3f6d7f41f0414e9d3952e364 (diff) |
Got rid of global_alloc/global_free.
The global_alloc/global_free functions have outlived their usefulness.
They don't allow consistently overriding the default memory allocation
behaviour because they aren't used consistently, and we don't have
standard library allocator wrappers for them that we'd need to use them
consistently with all the standard library containers we're using. If
you need to change the default allocator behaviour, you can override the
new/delete operators, and there are ways to get more fine-grained
control that way. We're already doing that to pre-fill memory in debug
builds.
Code was already starting to depend on global_alloc/global_free wrapping
new/delete. For example some parts of the code (including the UI and
Windows debugger) was putting the result of global_alloc in a
std::unique_ptr wrappers without custom deleters, and the SPU sound
device was assuming it could use global_free to release memory allocated
with operator new. There was also code misunderstanding the behaviour
of global_alloc, for example the GROM port cartridge code was checking
for nullptr when a failure will actually throw std::bad_alloc.
As well as substituting new/delete, I've made several things use smart
pointers to reduce the chance of leaks, and fixed a couple of leaks,
too.
Diffstat (limited to 'src/lib/util/corealloc.h')
-rw-r--r-- | src/lib/util/corealloc.h | 40 |
1 files changed, 5 insertions, 35 deletions
diff --git a/src/lib/util/corealloc.h b/src/lib/util/corealloc.h index 5d1f055e19b..db8aacf6cb2 100644 --- a/src/lib/util/corealloc.h +++ b/src/lib/util/corealloc.h @@ -8,54 +8,24 @@ ***************************************************************************/ -#pragma once - #ifndef MAME_LIB_UTIL_COREALLOC_H #define MAME_LIB_UTIL_COREALLOC_H -#include "osdcore.h" +#pragma once -#include <cstdlib> +#include "osdcore.h" #include <cstddef> +#include <cstdlib> #include <cstring> -#include <new> #include <memory> +#include <new> #include <type_traits> #include <utility> -//************************************************************************** -// MACROS -//************************************************************************** - -// global allocation helpers -- use these instead of new and delete -#define global_alloc(Type) new Type -#define global_alloc_array(Type, Num) new Type[Num] -#define global_free(Ptr) do { delete Ptr; } while (0) -#define global_free_array(Ptr) do { delete[] Ptr; } while (0) - - - -template<typename T, typename... Params> -inline T* global_alloc_clear(Params &&... 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 T> -inline T* global_alloc_array_clear(std::size_t 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](); -} - - +// global allocation helpers template<typename Tp> struct MakeUniqClearT { typedef std::unique_ptr<Tp> single_object; }; |