summaryrefslogtreecommitdiff
path: root/tests/allocator.cpp
blob: a43e14a515db9d6d74a3e4a83db02e35022941e5 (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
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
#include "allocator.hpp"

#include <string.h>
#include <assert.h>

// Low-level allocation functions
#if defined(_WIN32) || defined(_WIN64)
#	ifdef __MWERKS__
#		pragma ANSI_strict off // disable ANSI strictness to include windows.h
#		pragma cpp_extensions on // enable some extensions to include windows.h
#	endif

#   if defined(_MSC_VER)
#       pragma warning(disable: 4201) // nonstandard extension used: nameless struct/union
#   endif

#	ifdef _XBOX_VER
#		define NOD3D
#		include <xtl.h>
#	else
#		include <windows.h>
#	endif

namespace
{
	const size_t page_size = 4096;

	size_t align_to_page(size_t value)
	{
		return (value + page_size - 1) & ~(page_size - 1);
	}

	void* allocate_page_aligned(size_t size)
	{
		// We can't use VirtualAlloc because it has 64Kb granularity so we run out of address space quickly
		// We can't use malloc because of occasional problems with CW on CRT termination
		static HANDLE heap = HeapCreate(0, 0, 0);

		void* result = HeapAlloc(heap, 0, size + page_size);

		return reinterpret_cast<void*>(align_to_page(reinterpret_cast<size_t>(result)));
	}

	void* allocate(size_t size)
	{
		size_t aligned_size = align_to_page(size);

		void* ptr = allocate_page_aligned(aligned_size + page_size);
		if (!ptr) return 0;

		char* end = static_cast<char*>(ptr) + aligned_size;

		DWORD old_flags;
		VirtualProtect(end, page_size, PAGE_NOACCESS, &old_flags);

		return end - size;
	}

	void deallocate(void* ptr, size_t size)
	{
		size_t aligned_size = align_to_page(size);

		void* rptr = static_cast<char*>(ptr) + size - aligned_size;

		DWORD old_flags;
		VirtualProtect(rptr, aligned_size + page_size, PAGE_NOACCESS, &old_flags);
	}
}
#elif defined(__APPLE__) || defined(__linux__)
#	include <sys/mman.h>

namespace
{
	const size_t page_size = 4096;

	size_t align_to_page(size_t value)
	{
		return (value + page_size - 1) & ~(page_size - 1);
	}

	void* allocate_page_aligned(size_t size)
	{
		void* result = malloc(size + page_size);

		return reinterpret_cast<void*>(align_to_page(reinterpret_cast<size_t>(result)));
	}

	void* allocate(size_t size)
	{
		size_t aligned_size = align_to_page(size);

		void* ptr = allocate_page_aligned(aligned_size + page_size);
		if (!ptr) return 0;

		char* end = static_cast<char*>(ptr) + aligned_size;

		int res = mprotect(end, page_size, PROT_NONE);
		assert(res == 0);
		(void)!res;

		return end - size;
	}

	void deallocate(void* ptr, size_t size)
	{
		size_t aligned_size = align_to_page(size);

		void* rptr = static_cast<char*>(ptr) + size - aligned_size;

		int res = mprotect(rptr, aligned_size + page_size, PROT_NONE);
		assert(res == 0);
		(void)!res;
	}
}
#else
#	include <stdlib.h>

namespace
{
	void* allocate(size_t size)
	{
		return malloc(size);
	}

	void deallocate(void* ptr, size_t size)
	{
		(void)size;

		free(ptr);
	}
}
#endif

// High-level allocation functions
void* memory_allocate(size_t size)
{
	void* result = allocate(size + sizeof(size_t));
	if (!result) return 0;

	memcpy(result, &size, sizeof(size_t));

	return static_cast<size_t*>(result) + 1;
}

size_t memory_size(void* ptr)
{
	assert(ptr);

	size_t result;
	memcpy(&result, static_cast<size_t*>(ptr) - 1, sizeof(size_t));

	return result;
}

void memory_deallocate(void* ptr)
{
	if (!ptr) return;

	size_t size = memory_size(ptr);

	deallocate(static_cast<size_t*>(ptr) - 1, size + sizeof(size_t));
}