summaryrefslogtreecommitdiffstatshomepage
path: root/src/lib/util/corealloc.h
blob: c6c954fa2cf966d150216ba31754110d36b1aca6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// license:BSD-3-Clause
// copyright-holders:Aaron Giles
/***************************************************************************

    corealloc.h

    Memory allocation helpers for the helper library.

***************************************************************************/

#ifndef MAME_LIB_UTIL_COREALLOC_H
#define MAME_LIB_UTIL_COREALLOC_H

#pragma once

#include <cassert>
#include <cstddef>
#include <cstring>
#include <memory>
#include <new>
#include <type_traits>


// global allocation helpers

namespace util {

namespace detail {

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]> { };

} // namespace detail

/// make_unique_clear for arrays of unknown bound
template <typename Tp>
inline typename detail::make_unique_clear_traits<Tp>::unbounded_array_ptr make_unique_clear(size_t num)
{
	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]);
}

} // namespace util

#endif  // MAME_LIB_UTIL_COREALLOC_H