aboutsummaryrefslogtreecommitdiff
path: root/primedev/core/memalloc.cpp
blob: 511677179138a34493008735a1cd9084ffc5cade (plain)
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
#include "core/memalloc.h"
#include "core/tier0.h"

// TODO: rename to malloc and free after removing statically compiled .libs

void* _malloc_base(size_t n)
{
	// allocate into static buffer if g_pMemAllocSingleton isn't initialised
	if (!g_pMemAllocSingleton)
		TryCreateGlobalMemAlloc();

	return g_pMemAllocSingleton->m_vtable->Alloc(g_pMemAllocSingleton, n);
}

/*extern "C" void* malloc(size_t n)
{
	return _malloc_base(n);
}*/

void _free_base(void* p)
{
	if (!g_pMemAllocSingleton)
		TryCreateGlobalMemAlloc();

	g_pMemAllocSingleton->m_vtable->Free(g_pMemAllocSingleton, p);
}

void* _realloc_base(void* oldPtr, size_t size)
{
	if (!g_pMemAllocSingleton)
		TryCreateGlobalMemAlloc();

	return g_pMemAllocSingleton->m_vtable->Realloc(g_pMemAllocSingleton, oldPtr, size);
}

void* _calloc_base(size_t n, size_t size)
{
	size_t bytes = n * size;
	void* memory = _malloc_base(bytes);
	if (memory)
	{
		memset(memory, 0, bytes);
	}
	return memory;
}

void* _recalloc_base(void* const block, size_t const count, size_t const size)
{
	if (!block)
		return _calloc_base(count, size);

	const size_t new_size = count * size;
	const size_t old_size = _msize(block);

	void* const memory = _realloc_base(block, new_size);

	if (memory && old_size < new_size)
	{
		memset(static_cast<char*>(memory) + old_size, 0, new_size - old_size);
	}

	return memory;
}

size_t _msize(void* const block)
{
	if (!g_pMemAllocSingleton)
		TryCreateGlobalMemAlloc();

	return g_pMemAllocSingleton->m_vtable->GetSize(g_pMemAllocSingleton, block);
}

char* _strdup_base(const char* src)
{
	char* str;
	char* p;
	int len = 0;

	while (src[len])
		len++;
	str = reinterpret_cast<char*>(_malloc_base(len + 1));
	p = str;
	while (*src)
		*p++ = *src++;
	*p = '\0';
	return str;
}

void* operator new(size_t n)
{
	return _malloc_base(n);
}

void operator delete(void* p) noexcept
{
	_free_base(p);
} // /FORCE:MULTIPLE