MALLOC_DEBUG part is not able to compile on UWP (nw)

This commit is contained in:
Miodrag Milanovic 2016-11-11 10:11:16 +01:00
parent dac02ddf8e
commit d8bf7a88e8

View File

@ -1,5 +1,5 @@
// license:BSD-3-Clause
// copyright-holders:Olivier Galibert, R. Belmont
// copyright-holders:Olivier Galibert, R. Belmont, Brad Hughes
//============================================================
//
// sdlos_*.c - OS specific low level code
@ -13,9 +13,6 @@
#include <mmsystem.h>
#include <stdlib.h>
#ifndef _MSC_VER
#include <unistd.h>
#endif
#include <cstdio>
#include <memory>
@ -120,24 +117,7 @@ void osd_process_kill()
void *osd_malloc(size_t size)
{
#ifndef MALLOC_DEBUG
return malloc(size);
#else
// add in space for the size and offset
size += MAX_ALIGNMENT + sizeof(size_t) + 2;
size &= ~size_t(1);
// basic objects just come from the heap
uint8_t *const block = reinterpret_cast<uint8_t *>(HeapAlloc(GetProcessHeap(), 0, size));
if (block == nullptr)
return nullptr;
uint8_t *const result = reinterpret_cast<uint8_t *>(reinterpret_cast<uintptr_t>(block + sizeof(size_t) + MAX_ALIGNMENT) & ~(uintptr_t(MAX_ALIGNMENT) - 1));
// store the size and return and pointer to the data afterward
*reinterpret_cast<size_t *>(block) = size;
*(result - 1) = result - block;
return result;
#endif
}
@ -147,35 +127,7 @@ void *osd_malloc(size_t size)
void *osd_malloc_array(size_t size)
{
#ifndef MALLOC_DEBUG
return malloc(size);
#else
// add in space for the size and offset
size += MAX_ALIGNMENT + sizeof(size_t) + 2;
size &= ~size_t(1);
// round the size up to a page boundary
size_t const rounded_size = ((size + sizeof(void *) + PAGE_SIZE - 1) / PAGE_SIZE) * PAGE_SIZE;
// reserve that much memory, plus two guard pages
void *page_base = VirtualAlloc(nullptr, rounded_size + 2 * PAGE_SIZE, MEM_RESERVE, PAGE_NOACCESS);
if (page_base == nullptr)
return nullptr;
// now allow access to everything but the first and last pages
page_base = VirtualAlloc(reinterpret_cast<uint8_t *>(page_base) + PAGE_SIZE, rounded_size, MEM_COMMIT, PAGE_READWRITE);
if (page_base == nullptr)
return nullptr;
// work backwards from the page base to get to the block base
uint8_t *const block = GUARD_ALIGN_START ? reinterpret_cast<uint8_t *>(page_base) : (reinterpret_cast<uint8_t *>(page_base) + rounded_size - size);
uint8_t *const result = reinterpret_cast<uint8_t *>(reinterpret_cast<uintptr_t>(block + sizeof(size_t) + MAX_ALIGNMENT) & ~(uintptr_t(MAX_ALIGNMENT) - 1));
// store the size at the start with a flag indicating it has a guard page
*reinterpret_cast<size_t *>(block) = size | 1;
*(result - 1) = result - block;
return result;
#endif
}
@ -185,25 +137,7 @@ void *osd_malloc_array(size_t size)
void osd_free(void *ptr)
{
#ifndef MALLOC_DEBUG
free(ptr);
#else
uint8_t const offset = *(reinterpret_cast<uint8_t *>(ptr) - 1);
uint8_t *const block = reinterpret_cast<uint8_t *>(ptr) - offset;
size_t const size = *reinterpret_cast<size_t *>(block);
if ((size & 0x1) == 0)
{
// if no guard page, just free the pointer
HeapFree(GetProcessHeap(), 0, block);
}
else
{
// large items need more care
ULONG_PTR const page_base = reinterpret_cast<ULONG_PTR>(block) & ~(PAGE_SIZE - 1);
VirtualFree(reinterpret_cast<void *>(page_base - PAGE_SIZE), 0, MEM_RELEASE);
}
#endif
}
@ -237,21 +171,11 @@ void osd_free_executable(void *ptr, size_t size)
void osd_break_into_debugger(const char *message)
{
#ifdef OSD_WINDOWS
if (IsDebuggerPresent())
{
win_output_debug_string_utf8(message);
DebugBreak();
}
else if (s_debugger_stack_crawler != nullptr)
(*s_debugger_stack_crawler)();
#else
if (IsDebuggerPresent())
{
OutputDebugStringA(message);
__debugbreak();
}
#endif
}
//============================================================
@ -309,13 +233,7 @@ char *osd_get_clipboard_text(void)
//============================================================
// osd_dynamic_bind
//============================================================
#if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
// for classic desktop applications
#define load_library(filename) LoadLibrary(filename)
#else
// for Windows Store universal applications
// This needs to change ASAP as it won't be allowed in the store
typedef HMODULE __stdcall t_LLA(const char *);
typedef FARPROC __stdcall t_GPA(HMODULE H, const char *);
@ -457,7 +375,7 @@ void find_load_exports()
#define load_library(filename) g_LoadLibraryA(osd::text::from_wstring(filename).c_str())
#define get_proc_address(mod, proc) g_GetProcAddressA(mod, proc)
#endif
namespace osd {
class dynamic_module_win32_impl : public dynamic_module
@ -467,9 +385,7 @@ public:
: m_module(nullptr)
{
m_libraries = libraries;
#if defined(OSD_UWP)
find_load_exports();
#endif
}
virtual ~dynamic_module_win32_impl() override