File: mptMemory.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 (370 lines) | stat: -rw-r--r-- 11,589 bytes parent folder | download | duplicates (2)
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
/*
 * mptMemory.h
 * -----------
 * Purpose: Raw memory manipulation
 * 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 "mptAssert.h"
#include "mptBaseTypes.h"
#include "mptSpan.h"

#if MPT_CXX_AT_LEAST(20)
#include <bit>
#endif
#include <utility>

#include <cstring>

#include <string.h>



OPENMPT_NAMESPACE_BEGIN



namespace mpt {



typedef mpt::span<mpt::byte> byte_span;
typedef mpt::span<const mpt::byte> const_byte_span;



// Tell which types are safe for mpt::byte_cast.
// signed char is actually not allowed to alias into an object representation,
// which means that, if the actual type is not itself signed char but char or
// unsigned char instead, dereferencing the signed char pointer is undefined
// behaviour.
template <typename T> struct is_byte_castable : public std::false_type { };
template <> struct is_byte_castable<char>                : public std::true_type { };
template <> struct is_byte_castable<unsigned char>       : public std::true_type { };
#if MPT_BYTE_IS_STD_BYTE
template <> struct is_byte_castable<mpt::byte>           : public std::true_type { };
#endif
template <> struct is_byte_castable<const char>          : public std::true_type { };
template <> struct is_byte_castable<const unsigned char> : public std::true_type { };
#if MPT_BYTE_IS_STD_BYTE
template <> struct is_byte_castable<const mpt::byte>     : public std::true_type { };
#endif


template <typename T> struct is_byte        : public std::false_type { };
template <> struct is_byte<mpt::byte>       : public std::true_type  { };
template <> struct is_byte<const mpt::byte> : public std::true_type  { };


// Tell which types are safe to binary write into files.
// By default, no types are safe.
// When a safe type gets defined,
// also specialize this template so that IO functions will work.
template <typename T> struct is_binary_safe : public std::false_type { }; 

// Specialization for byte types.
template <> struct is_binary_safe<char>      : public std::true_type { };
template <> struct is_binary_safe<uint8>     : public std::true_type { };
template <> struct is_binary_safe<int8>      : public std::true_type { };
#if MPT_BYTE_IS_STD_BYTE
template <> struct is_binary_safe<mpt::byte> : public std::true_type { };
#endif

// Generic Specialization for arrays.
template <typename T, std::size_t N> struct is_binary_safe<T[N]> : public is_binary_safe<T> { };
template <typename T, std::size_t N> struct is_binary_safe<const T[N]> : public is_binary_safe<T> { };
template <typename T, std::size_t N> struct is_binary_safe<std::array<T, N>> : public is_binary_safe<T> { };
template <typename T, std::size_t N> struct is_binary_safe<const std::array<T, N>> : public is_binary_safe<T> { };


} // namespace mpt

#define MPT_BINARY_STRUCT(type, size) \
	MPT_STATIC_ASSERT(sizeof( type ) == (size) ); \
	MPT_STATIC_ASSERT(alignof( type ) == 1); \
	MPT_STATIC_ASSERT(std::is_standard_layout< type >::value); \
	namespace mpt { \
		template <> struct is_binary_safe< type > : public std::true_type { }; \
	} \
/**/



template <typename T>
struct value_initializer
{
	inline void operator () (T & x)
	{
		x = T();
	}
};

template <typename T, std::size_t N>
struct value_initializer<T[N]>
{
	inline void operator () (T (& a)[N])
	{
		for(auto & e : a)
		{
			value_initializer<T>()(e);
		}
	}
};

template <typename T>
inline void Clear(T & x)
{
	MPT_STATIC_ASSERT(!std::is_pointer<T>::value);
	value_initializer<T>()(x);
}


// Memset given object to zero.
template <class T>
inline void MemsetZero(T &a)
{
	static_assert(std::is_pointer<T>::value == false, "Won't memset pointers.");
#if MPT_GCC_BEFORE(5,1,0) || (MPT_COMPILER_CLANG && defined(__GLIBCXX__))
	MPT_STATIC_ASSERT(std::is_standard_layout<T>::value);
	MPT_STATIC_ASSERT(std::is_trivial<T>::value || mpt::is_binary_safe<T>::value); // approximation
#else // default
	MPT_STATIC_ASSERT(std::is_standard_layout<T>::value);
	MPT_STATIC_ASSERT((std::is_trivially_default_constructible<T>::value && std::is_trivially_copyable<T>::value) || mpt::is_binary_safe<T>::value); // C++11, but not supported on most compilers we care about
#endif
	std::memset(&a, 0, sizeof(T));
}



namespace mpt {



#if MPT_CXX_AT_LEAST(20)
using std::bit_cast;
#else
// C++2a compatible bit_cast.
// See <http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0476r1.html>.
// Not implementing constexpr because this is not easily possible pre C++2a.
template <typename Tdst, typename Tsrc>
MPT_FORCEINLINE Tdst bit_cast(const Tsrc & src) noexcept
{
	MPT_STATIC_ASSERT(sizeof(Tdst) == sizeof(Tsrc));
#if MPT_GCC_BEFORE(5,1,0) || (MPT_COMPILER_CLANG && defined(__GLIBCXX__))
	MPT_STATIC_ASSERT(std::is_trivial<Tdst>::value); // approximation
	MPT_STATIC_ASSERT(std::is_trivial<Tsrc>::value); // approximation
#else // default
	MPT_STATIC_ASSERT(std::is_trivially_copyable<Tdst>::value);
	MPT_STATIC_ASSERT(std::is_trivially_copyable<Tsrc>::value);
#endif
	#if MPT_COMPILER_GCC || MPT_COMPILER_MSVC
		// Compiler supports type-punning through unions. This is not stricly standard-conforming.
		// For GCC, this is documented, for MSVC this is apparently not documented, but we assume it.
		union {
			Tsrc src;
			Tdst dst;
		} conv;
		conv.src = src;
		return conv.dst;
	#else // MPT_COMPILER
		// Compiler does not support type-punning through unions. std::memcpy is used instead.
		// This is the safe fallback and strictly standard-conforming.
		// Another standard-compliant alternative would be casting pointers to a character type pointer.
		// This results in rather unreadable code and,
		// in most cases, compilers generate better code by just inlining the memcpy anyway.
		// (see <http://blog.regehr.org/archives/959>).
		Tdst dst{};
		std::memcpy(&dst, &src, sizeof(Tdst));
		return dst;
	#endif // MPT_COMPILER
}
#endif



template <typename Tdst, typename Tsrc>
struct byte_cast_impl
{
	inline Tdst operator () (Tsrc src) const
	{
		STATIC_ASSERT(sizeof(Tsrc) == sizeof(mpt::byte));
		STATIC_ASSERT(sizeof(Tdst) == sizeof(mpt::byte));
		// not checking is_byte_castable here because we are actually
		// doing a static_cast and converting the value
		STATIC_ASSERT(std::is_integral<Tsrc>::value || mpt::is_byte<Tsrc>::value);
		STATIC_ASSERT(std::is_integral<Tdst>::value || mpt::is_byte<Tdst>::value);
		return static_cast<Tdst>(src);
	}
};
template <typename Tdst, typename Tsrc>
struct byte_cast_impl<mpt::span<Tdst>, mpt::span<Tsrc> >
{
	inline mpt::span<Tdst> operator () (mpt::span<Tsrc> src) const
	{
		STATIC_ASSERT(sizeof(Tsrc) == sizeof(mpt::byte));
		STATIC_ASSERT(sizeof(Tdst) == sizeof(mpt::byte));
		STATIC_ASSERT(mpt::is_byte_castable<Tsrc>::value);
		STATIC_ASSERT(mpt::is_byte_castable<Tdst>::value);
		STATIC_ASSERT(std::is_integral<Tsrc>::value || mpt::is_byte<Tsrc>::value);
		STATIC_ASSERT(std::is_integral<Tdst>::value || mpt::is_byte<Tdst>::value);
		return mpt::as_span(mpt::byte_cast_impl<Tdst*, Tsrc*>()(src.begin()), mpt::byte_cast_impl<Tdst*, Tsrc*>()(src.end()));
	}
};
template <typename Tdst, typename Tsrc>
struct byte_cast_impl<Tdst*, Tsrc*>
{
	inline Tdst* operator () (Tsrc* src) const
	{
		STATIC_ASSERT(sizeof(Tsrc) == sizeof(mpt::byte));
		STATIC_ASSERT(sizeof(Tdst) == sizeof(mpt::byte));
		STATIC_ASSERT(mpt::is_byte_castable<Tsrc>::value);
		STATIC_ASSERT(mpt::is_byte_castable<Tdst>::value);
		STATIC_ASSERT(std::is_integral<Tsrc>::value || mpt::is_byte<Tsrc>::value);
		STATIC_ASSERT(std::is_integral<Tdst>::value || mpt::is_byte<Tdst>::value);
		return reinterpret_cast<Tdst*>(src);
	}
};

template <typename Tdst, typename Tsrc>
struct void_cast_impl;

template <typename Tdst>
struct void_cast_impl<Tdst*, void*>
{
	inline Tdst* operator () (void* src) const
	{
		STATIC_ASSERT(sizeof(Tdst) == sizeof(mpt::byte));
		STATIC_ASSERT(mpt::is_byte_castable<Tdst>::value);
		STATIC_ASSERT(std::is_integral<Tdst>::value || mpt::is_byte<Tdst>::value);
		return reinterpret_cast<Tdst*>(src);
	}
};
template <typename Tdst>
struct void_cast_impl<Tdst*, const void*>
{
	inline Tdst* operator () (const void* src) const
	{
		STATIC_ASSERT(sizeof(Tdst) == sizeof(mpt::byte));
		STATIC_ASSERT(mpt::is_byte_castable<Tdst>::value);
		STATIC_ASSERT(std::is_integral<Tdst>::value || mpt::is_byte<Tdst>::value);
		return reinterpret_cast<Tdst*>(src);
	}
};
template <typename Tsrc>
struct void_cast_impl<void*, Tsrc*>
{
	inline void* operator () (Tsrc* src) const
	{
		STATIC_ASSERT(sizeof(Tsrc) == sizeof(mpt::byte));
		STATIC_ASSERT(mpt::is_byte_castable<Tsrc>::value);
		STATIC_ASSERT(std::is_integral<Tsrc>::value || mpt::is_byte<Tsrc>::value);
		return reinterpret_cast<void*>(src);
	}
};
template <typename Tsrc>
struct void_cast_impl<const void*, Tsrc*>
{
	inline const void* operator () (Tsrc* src) const
	{
		STATIC_ASSERT(sizeof(Tsrc) == sizeof(mpt::byte));
		STATIC_ASSERT(mpt::is_byte_castable<Tsrc>::value);
		STATIC_ASSERT(std::is_integral<Tsrc>::value || mpt::is_byte<Tsrc>::value);
		return reinterpret_cast<const void*>(src);
	}
};

// casts between different byte (char) types or pointers to these types
template <typename Tdst, typename Tsrc>
inline Tdst byte_cast(Tsrc src)
{
	return byte_cast_impl<Tdst, Tsrc>()(src);
}

// casts between pointers to void and pointers to byte
template <typename Tdst, typename Tsrc>
inline Tdst void_cast(Tsrc src)
{
	return void_cast_impl<Tdst, Tsrc>()(src);
}



template <typename T>
MPT_CONSTEXPR14_FUN mpt::byte as_byte(T src) noexcept
{
	MPT_STATIC_ASSERT(std::is_integral<T>::value);
	return static_cast<mpt::byte>(static_cast<uint8>(src));
}



template <typename T>
struct GetRawBytesFunctor
{
	inline mpt::const_byte_span operator () (const 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), sizeof(T));
	}
	inline mpt::byte_span operator () (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), sizeof(T));
	}
};

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

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

// In order to be able to partially specialize it,
// as_raw_memory is implemented via a class template.
// Do not overload or specialize as_raw_memory directly.
// Using a wrapper (by default just around a cast to const mpt::byte *),
// allows for implementing raw memory access
// via on-demand generating a cached serialized representation.
template <typename T> inline mpt::const_byte_span as_raw_memory(const T & v)
{
	return mpt::GetRawBytesFunctor<T>()(v);
}
template <typename T> inline mpt::byte_span as_raw_memory(T & v)
{
	return mpt::GetRawBytesFunctor<T>()(v);
}



} // namespace mpt



OPENMPT_NAMESPACE_END