2010-07-19 09:57:32 +00:00
|
|
|
#include "allocator.hpp"
|
|
|
|
|
|
|
|
#include <string.h>
|
2014-10-21 03:33:47 +00:00
|
|
|
#include <assert.h>
|
2015-04-15 22:11:13 -07:00
|
|
|
#include <stdlib.h>
|
2010-07-19 09:57:32 +00:00
|
|
|
|
2015-05-12 21:49:44 -07:00
|
|
|
// Address sanitizer
|
|
|
|
#if defined(__has_feature)
|
|
|
|
# define ADDRESS_SANITIZER __has_feature(address_sanitizer)
|
|
|
|
#else
|
2017-11-10 21:35:59 -08:00
|
|
|
# if defined(__SANITIZE_ADDRESS__)
|
|
|
|
# define ADDRESS_SANITIZER 1
|
|
|
|
# else
|
|
|
|
# define ADDRESS_SANITIZER 0
|
|
|
|
# endif
|
2015-05-12 21:49:44 -07:00
|
|
|
#endif
|
|
|
|
|
2010-07-19 09:57:32 +00:00
|
|
|
// 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
|
|
|
|
|
2012-02-19 09:05:23 +00:00
|
|
|
# if defined(_MSC_VER)
|
|
|
|
# pragma warning(disable: 4201) // nonstandard extension used: nameless struct/union
|
|
|
|
# endif
|
|
|
|
|
2010-07-19 09:57:32 +00:00
|
|
|
# ifdef _XBOX_VER
|
|
|
|
# define NOD3D
|
|
|
|
# include <xtl.h>
|
|
|
|
# else
|
|
|
|
# include <windows.h>
|
|
|
|
# endif
|
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
2015-03-21 00:14:53 -07:00
|
|
|
const size_t page_size = 4096;
|
2010-07-19 09:57:32 +00:00
|
|
|
|
2014-10-01 07:02:39 +00:00
|
|
|
size_t align_to_page(size_t value)
|
|
|
|
{
|
2015-03-21 00:14:53 -07:00
|
|
|
return (value + page_size - 1) & ~(page_size - 1);
|
2014-10-01 07:02:39 +00:00
|
|
|
}
|
2010-08-03 12:18:27 +00:00
|
|
|
|
|
|
|
void* allocate_page_aligned(size_t size)
|
2014-10-01 07:02:39 +00:00
|
|
|
{
|
|
|
|
// We can't use VirtualAlloc because it has 64Kb granularity so we run out of address space quickly
|
2010-08-30 18:30:39 +00:00
|
|
|
// We can't use malloc because of occasional problems with CW on CRT termination
|
|
|
|
static HANDLE heap = HeapCreate(0, 0, 0);
|
|
|
|
|
2015-03-21 00:14:53 -07:00
|
|
|
void* result = HeapAlloc(heap, 0, size + page_size);
|
2010-08-03 12:18:27 +00:00
|
|
|
|
2014-10-01 07:02:39 +00:00
|
|
|
return reinterpret_cast<void*>(align_to_page(reinterpret_cast<size_t>(result)));
|
|
|
|
}
|
2010-08-03 12:18:27 +00:00
|
|
|
|
2010-07-19 09:57:32 +00:00
|
|
|
void* allocate(size_t size)
|
|
|
|
{
|
2010-08-03 12:18:27 +00:00
|
|
|
size_t aligned_size = align_to_page(size);
|
2010-07-19 09:57:32 +00:00
|
|
|
|
2015-03-21 00:14:53 -07:00
|
|
|
void* ptr = allocate_page_aligned(aligned_size + page_size);
|
2010-07-19 09:57:32 +00:00
|
|
|
if (!ptr) return 0;
|
|
|
|
|
2010-12-19 10:16:37 +00:00
|
|
|
char* end = static_cast<char*>(ptr) + aligned_size;
|
2010-07-19 09:57:32 +00:00
|
|
|
|
|
|
|
DWORD old_flags;
|
2015-03-21 00:14:53 -07:00
|
|
|
VirtualProtect(end, page_size, PAGE_NOACCESS, &old_flags);
|
2010-07-19 09:57:32 +00:00
|
|
|
|
2010-12-19 10:16:37 +00:00
|
|
|
return end - size;
|
2010-07-19 09:57:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void deallocate(void* ptr, size_t size)
|
|
|
|
{
|
2010-08-03 12:18:27 +00:00
|
|
|
size_t aligned_size = align_to_page(size);
|
2010-07-19 09:57:32 +00:00
|
|
|
|
2010-12-19 10:16:37 +00:00
|
|
|
void* rptr = static_cast<char*>(ptr) + size - aligned_size;
|
2010-07-19 09:57:32 +00:00
|
|
|
|
2014-10-01 07:02:39 +00:00
|
|
|
DWORD old_flags;
|
2015-03-21 00:14:53 -07:00
|
|
|
VirtualProtect(rptr, aligned_size + page_size, PAGE_NOACCESS, &old_flags);
|
2010-07-19 09:57:32 +00:00
|
|
|
}
|
|
|
|
}
|
2015-07-25 14:05:06 -04:00
|
|
|
#elif (defined(__APPLE__) || defined(__linux__)) && (defined(__i386) || defined(__x86_64)) && !ADDRESS_SANITIZER
|
2015-03-01 20:57:22 -08:00
|
|
|
# include <sys/mman.h>
|
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
2015-03-21 00:14:53 -07:00
|
|
|
const size_t page_size = 4096;
|
2015-03-01 20:57:22 -08:00
|
|
|
|
|
|
|
size_t align_to_page(size_t value)
|
|
|
|
{
|
2015-03-21 00:14:53 -07:00
|
|
|
return (value + page_size - 1) & ~(page_size - 1);
|
2015-03-01 20:57:22 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void* allocate_page_aligned(size_t size)
|
|
|
|
{
|
2015-04-15 21:44:52 -07:00
|
|
|
void* result = malloc(size + page_size);
|
|
|
|
|
|
|
|
return reinterpret_cast<void*>(align_to_page(reinterpret_cast<size_t>(result)));
|
2015-03-01 20:57:22 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void* allocate(size_t size)
|
|
|
|
{
|
|
|
|
size_t aligned_size = align_to_page(size);
|
|
|
|
|
2015-03-21 00:14:53 -07:00
|
|
|
void* ptr = allocate_page_aligned(aligned_size + page_size);
|
2015-03-01 20:57:22 -08:00
|
|
|
if (!ptr) return 0;
|
|
|
|
|
|
|
|
char* end = static_cast<char*>(ptr) + aligned_size;
|
|
|
|
|
2015-03-21 00:14:53 -07:00
|
|
|
int res = mprotect(end, page_size, PROT_NONE);
|
2015-03-01 20:57:22 -08:00
|
|
|
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;
|
|
|
|
|
2015-03-21 00:14:53 -07:00
|
|
|
int res = mprotect(rptr, aligned_size + page_size, PROT_NONE);
|
2015-03-01 20:57:22 -08:00
|
|
|
assert(res == 0);
|
|
|
|
(void)!res;
|
|
|
|
}
|
|
|
|
}
|
2010-07-19 09:57:32 +00:00
|
|
|
#else
|
|
|
|
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
|
2015-07-25 17:04:17 -04:00
|
|
|
const size_t memory_alignment = sizeof(double) > sizeof(void*) ? sizeof(double) : sizeof(void*);
|
|
|
|
|
2010-07-19 09:57:32 +00:00
|
|
|
void* memory_allocate(size_t size)
|
|
|
|
{
|
2015-07-25 17:04:17 -04:00
|
|
|
void* result = allocate(size + memory_alignment);
|
2010-07-19 09:57:32 +00:00
|
|
|
if (!result) return 0;
|
|
|
|
|
|
|
|
memcpy(result, &size, sizeof(size_t));
|
|
|
|
|
2015-07-25 17:04:17 -04:00
|
|
|
return static_cast<char*>(result) + memory_alignment;
|
2010-07-19 09:57:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t memory_size(void* ptr)
|
|
|
|
{
|
2014-10-21 03:33:47 +00:00
|
|
|
assert(ptr);
|
|
|
|
|
2010-07-19 09:57:32 +00:00
|
|
|
size_t result;
|
2015-07-25 17:04:17 -04:00
|
|
|
memcpy(&result, static_cast<char*>(ptr) - memory_alignment, sizeof(size_t));
|
2010-07-19 09:57:32 +00:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void memory_deallocate(void* ptr)
|
|
|
|
{
|
|
|
|
if (!ptr) return;
|
|
|
|
|
|
|
|
size_t size = memory_size(ptr);
|
|
|
|
|
2015-07-25 17:04:17 -04:00
|
|
|
deallocate(static_cast<char*>(ptr) - memory_alignment, size + memory_alignment);
|
2010-07-19 09:57:32 +00:00
|
|
|
}
|
|
|
|
|