File: mptAlloc.cpp

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 (121 lines) | stat: -rw-r--r-- 2,688 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
/*
 * mptAlloc.cpp
 * ------------
 * 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.
 */



#include "stdafx.h"
#include "mptAlloc.h"

#include "mptBaseTypes.h"

#include <memory>
#include <new>

#include <cstddef>
#include <cstdlib>

#if MPT_COMPILER_MSVC
#include <malloc.h>
#endif



OPENMPT_NAMESPACE_BEGIN



namespace mpt
{



#if MPT_CXX_AT_LEAST(17)
#else
void* align(std::size_t alignment, std::size_t size, void* &ptr, std::size_t &space) noexcept
{
	std::size_t offset = static_cast<std::size_t>(reinterpret_cast<std::uintptr_t>(ptr) & (alignment - 1));
	if(offset != 0)
	{
		offset = alignment - offset;
	}
	if((space < offset) || ((space - offset) < size))
	{
		return nullptr;
	}
	ptr = static_cast<mpt::byte*>(ptr) + offset;
	space -= offset;
	return ptr;
}
#endif

aligned_raw_memory aligned_alloc_impl(std::size_t size, std::size_t count, std::size_t alignment)
{
	#if MPT_CXX_AT_LEAST(17) && (!MPT_COMPILER_MSVC && !MPT_GCC_BEFORE(8,1,0) && !MPT_CLANG_BEFORE(5,0,0)) && !(MPT_COMPILER_CLANG && defined(__GLIBCXX__)) && !(MPT_COMPILER_CLANG && MPT_OS_MACOSX_OR_IOS) && !MPT_OS_EMSCRIPTEN
		std::size_t space = count * size;
		void* mem = std::aligned_alloc(alignment, space);
		if(!mem)
		{
			MPT_EXCEPTION_THROW_OUT_OF_MEMORY();
		}
		return aligned_raw_memory{mem, mem};
	#elif MPT_COMPILER_MSVC
		std::size_t space = count * size;
		void* mem = _aligned_malloc(space, alignment);
		if(!mem)
		{
			MPT_EXCEPTION_THROW_OUT_OF_MEMORY();
		}
		return aligned_raw_memory{mem, mem};
	#else
		if(alignment > alignof(mpt::max_align_t))
		{
			std::size_t space = count * size + (alignment - 1);
			void* mem = std::malloc(space);
			if(!mem)
			{
				MPT_EXCEPTION_THROW_OUT_OF_MEMORY();
			}
			void* aligned_mem = mem;
			void* aligned = mpt::align(alignment, size * count, aligned_mem, space);
			if(!aligned)
			{
				MPT_EXCEPTION_THROW_OUT_OF_MEMORY();
			}
			return aligned_raw_memory{aligned, mem};
		} else
		{
			std::size_t space = count * size;
			void* mem = std::malloc(space);
			if(!mem)
			{
				MPT_EXCEPTION_THROW_OUT_OF_MEMORY();
			}
			return aligned_raw_memory{mem, mem};
		}
	#endif
}

void aligned_free(aligned_raw_memory raw)
{
	#if MPT_CXX_AT_LEAST(17) && (!MPT_COMPILER_MSVC && !MPT_GCC_BEFORE(8,1,0) && !MPT_CLANG_BEFORE(5,0,0)) && !(MPT_COMPILER_CLANG && defined(__GLIBCXX__)) && !(MPT_COMPILER_CLANG && MPT_OS_MACOSX_OR_IOS) && !MPT_OS_EMSCRIPTEN
		std::free(raw.mem);
	#elif MPT_COMPILER_MSVC
		_aligned_free(raw.mem);
	#else
		std::free(raw.mem);
	#endif
}



} // namespace mpt



OPENMPT_NAMESPACE_END