File: mptAlloc.h

package info (click to toggle)
libopenmpt 0.4.3-1+deb10u1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 7,724 kB
  • sloc: cpp: 99,820; sh: 4,503; ansic: 3,449; makefile: 480
file content (262 lines) | stat: -rw-r--r-- 6,550 bytes parent folder | download
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/*
 * mptAlloc.h
 * ----------
 * Purpose: Dynamic memory allocation.
 * Notes  : (currently none)
 * Authors: OpenMPT Devs
 * The OpenMPT source code is released under the BSD license. Read LICENSE for more details.
 */


#pragma once

#include "BuildSettings.h"



#include "mptBaseMacros.h"
#include "mptMemory.h"
#include "mptSpan.h"

#include <memory>
#include <utility>
#include <vector>



OPENMPT_NAMESPACE_BEGIN



namespace mpt {



template <typename T> inline span<T> as_span(std::vector<T> & cont) { return span<T>(cont); }

template <typename T> inline span<const T> as_span(const std::vector<T> & cont) { return span<const T>(cont); }



template <typename T> inline std::vector<typename std::remove_const<T>::type> make_vector(T * beg, T * end) { return std::vector<typename std::remove_const<T>::type>(beg, end); }

template <typename T> inline std::vector<typename std::remove_const<T>::type> make_vector(T * data, std::size_t size) { return std::vector<typename std::remove_const<T>::type>(data, data + size); }

template <typename T> inline std::vector<typename std::remove_const<T>::type> make_vector(mpt::span<T> data) { return std::vector<typename std::remove_const<T>::type>(data.data(), data.data() + data.size()); }

template <typename T, std::size_t N> inline std::vector<typename std::remove_const<T>::type> make_vector(T (&arr)[N]) { return std::vector<typename std::remove_const<T>::type>(std::begin(arr), std::end(arr)); }



template <typename T>
struct GetRawBytesFunctor<std::vector<T>>
{
	inline mpt::const_byte_span operator () (const std::vector<T> & v) const
	{
		STATIC_ASSERT(mpt::is_binary_safe<typename std::remove_const<T>::type>::value);
		return mpt::as_span(reinterpret_cast<const mpt::byte *>(v.data()), v.size() * sizeof(T));
	}
	inline mpt::byte_span operator () (std::vector<T> & v) const
	{
		STATIC_ASSERT(mpt::is_binary_safe<typename std::remove_const<T>::type>::value);
		return mpt::as_span(reinterpret_cast<mpt::byte *>(v.data()), v.size() * sizeof(T));
	}
};

template <typename T>
struct GetRawBytesFunctor<const std::vector<T>>
{
	inline mpt::const_byte_span operator () (const std::vector<T> & v) const
	{
		STATIC_ASSERT(mpt::is_binary_safe<typename std::remove_const<T>::type>::value);
		return mpt::as_span(reinterpret_cast<const mpt::byte *>(v.data()), v.size() * sizeof(T));
	}
};



} // namespace mpt



#if MPT_CXX_AT_LEAST(14)
namespace mpt {
using std::make_unique;
} // namespace mpt
#else
namespace mpt {
template<typename T, typename... Args>
std::unique_ptr<T> make_unique(Args&&... args)
{
	return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
}
} // namespace mpt
#endif



namespace mpt
{



#if MPT_CXX_AT_LEAST(17) && !(MPT_COMPILER_CLANG && defined(__GLIBCXX__)) && !(MPT_COMPILER_CLANG && MPT_OS_MACOSX_OR_IOS)
using std::launder;
#else
template <class T>
MPT_NOINLINE T* launder(T* p) noexcept
{
	return p;
}
#endif


#if MPT_CXX_AT_LEAST(17)
using std::align;
#else
// pre-C++17, std::align does not support over-alignement
void* align(std::size_t alignment, std::size_t size, void* &ptr, std::size_t &space) noexcept;
#endif



struct aligned_raw_memory
{
	void* aligned;
	void* mem;
};

aligned_raw_memory aligned_alloc_impl(std::size_t size, std::size_t count, std::size_t alignment);

template <std::size_t alignment>
inline aligned_raw_memory aligned_alloc(std::size_t size, std::size_t count)
{
	MPT_STATIC_ASSERT(alignment > 0);
	MPT_CONSTEXPR14_ASSERT(mpt::weight(alignment) == 1);
	return aligned_alloc_impl(size, count, alignment);
}

void aligned_free(aligned_raw_memory raw);

template <typename T>
struct aligned_raw_buffer
{
	T* elements;
	void* mem;
};

template <typename T, std::size_t alignment>
inline aligned_raw_buffer<T> aligned_alloc(std::size_t count)
{
	MPT_STATIC_ASSERT(alignment >= alignof(T));
	aligned_raw_memory raw = aligned_alloc<alignment>(sizeof(T), count);
	return aligned_raw_buffer<T>{mpt::launder(reinterpret_cast<T*>(raw.aligned)), raw.mem};
}

template <typename T>
inline void aligned_free(aligned_raw_buffer<T> buf)
{
	aligned_free(aligned_raw_memory{buf.elements, buf.mem});
}

template <typename T>
struct aligned_raw_objects
{
	T* elements;
	std::size_t count;
	void* mem;
};

template <typename T, std::size_t alignment>
inline aligned_raw_objects<T> aligned_new(std::size_t count, T init = T())
{
	aligned_raw_buffer<T> buf = aligned_alloc<T, alignment>(count);
	std::size_t constructed = 0;
	try
	{
		for(std::size_t i = 0; i < count; ++i)
		{
			new(&(buf.elements[i])) T(init);
			constructed++;
		}
	} MPT_EXCEPTION_CATCH_OUT_OF_MEMORY(e)
	{
		while(constructed--)
		{
			mpt::launder(&(buf.elements[constructed - 1]))->~T();
		}
		aligned_free(buf);
		MPT_EXCEPTION_RETHROW_OUT_OF_MEMORY(e);
	} catch(...)
	{
		while(constructed--)
		{
			mpt::launder(&(buf.elements[constructed - 1]))->~T();
		}
		aligned_free(buf);
		throw;
	}
	return aligned_raw_objects<T>{mpt::launder(buf.elements), count, buf.mem};
}

template <typename T>
inline void aligned_delete(aligned_raw_objects<T> objs)
{
	if(objs.elements)
	{
		std::size_t constructed = objs.count;
		while(constructed--)
		{
			objs.elements[constructed - 1].~T();
		}
	}
	aligned_free(aligned_raw_buffer<T>{objs.elements, objs.mem});
}

template <typename T, std::size_t alignment>
class aligned_buffer
{
private:
	aligned_raw_objects<T> objs;
public:
	explicit aligned_buffer(std::size_t count = 0)
		: objs(aligned_new<T, alignment>(count))
	{
	}
	aligned_buffer(const aligned_buffer&) = delete;
	aligned_buffer& operator=(const aligned_buffer&) = delete;
	~aligned_buffer()
	{
		aligned_delete(objs);
	}
public:
	void destructive_resize(std::size_t count)
	{
		aligned_raw_objects<T> tmpobjs = aligned_new<T, alignment>(count);
		{
			using namespace std;
			swap(objs, tmpobjs);
		}
		aligned_delete(tmpobjs);
	}
public:
	T* begin() noexcept { return objs.elements; }
	const T* begin() const noexcept { return objs.elements; }
	T* end() noexcept { return objs.elements + objs.count; }
	const T* end() const noexcept { return objs.elements + objs.count; }
	const T* cbegin() const noexcept { return objs.elements; }
	const T* cend() const noexcept { return objs.elements + objs.count; }
	T& operator[](std::size_t i) noexcept { return objs.elements[i]; }
	const T& operator[](std::size_t i) const noexcept { return objs.elements[i]; }
	T* data() noexcept { return objs.elements; }
	const T* data() const noexcept { return objs.elements; }
	std::size_t size() const noexcept { return objs.count; }
};



} // namespace mpt



OPENMPT_NAMESPACE_END