merged with dev-win

This commit is contained in:
daan 2019-07-14 12:57:47 -07:00
commit 672506a3b3
2 changed files with 16 additions and 16 deletions

View file

@ -82,8 +82,11 @@ static size_t mi_os_good_alloc_size(size_t size, size_t alignment) {
#if defined(_WIN32) #if defined(_WIN32)
// We use VirtualAlloc2 for aligned allocation, but it is only supported on Windows 10 and Windows Server 2016. // We use VirtualAlloc2 for aligned allocation, but it is only supported on Windows 10 and Windows Server 2016.
// So, we need to look it up dynamically to run on older systems. (use __stdcall for 32-bit compatibility) // So, we need to look it up dynamically to run on older systems. (use __stdcall for 32-bit compatibility)
typedef PVOID(__stdcall *VirtualAlloc2Ptr)(HANDLE, PVOID, SIZE_T, ULONG, ULONG, MEM_EXTENDED_PARAMETER*, ULONG); // Same for DiscardVirtualMemory
static VirtualAlloc2Ptr pVirtualAlloc2 = NULL; typedef PVOID(__stdcall *PVirtualAlloc2)(HANDLE, PVOID, SIZE_T, ULONG, ULONG, MEM_EXTENDED_PARAMETER*, ULONG);
typedef DWORD(__stdcall *PDiscardVirtualMemory)(PVOID,SIZE_T);
static PVirtualAlloc2 pVirtualAlloc2 = NULL;
static PDiscardVirtualMemory pDiscardVirtualMemory = NULL;
void _mi_os_init(void) { void _mi_os_init(void) {
// get the page size // get the page size
@ -95,8 +98,10 @@ void _mi_os_init(void) {
HINSTANCE hDll; HINSTANCE hDll;
hDll = LoadLibrary(TEXT("kernelbase.dll")); hDll = LoadLibrary(TEXT("kernelbase.dll"));
if (hDll != NULL) { if (hDll != NULL) {
// use VirtualAlloc2FromApp as it is available to Windows store apps // use VirtualAlloc2FromApp if possible as it is available to Windows store apps
pVirtualAlloc2 = (VirtualAlloc2Ptr)GetProcAddress(hDll, "VirtualAlloc2FromApp"); pVirtualAlloc2 = (PVirtualAlloc2)GetProcAddress(hDll, "VirtualAlloc2FromApp");
if (pVirtualAlloc2==NULL) pVirtualAlloc2 = (PVirtualAlloc2)GetProcAddress(hDll, "VirtualAlloc2");
pDiscardVirtualMemory = (PDiscardVirtualMemory)GetProcAddress(hDll, "DiscardVirtualMemory");
FreeLibrary(hDll); FreeLibrary(hDll);
} }
// Try to see if large OS pages are supported // Try to see if large OS pages are supported
@ -460,22 +465,16 @@ static bool mi_os_resetx(void* addr, size_t size, bool reset, mi_stats_t* stats)
#if defined(_WIN32) #if defined(_WIN32)
// Testing shows that for us (on `malloc-large`) MEM_RESET is 2x faster than DiscardVirtualMemory // Testing shows that for us (on `malloc-large`) MEM_RESET is 2x faster than DiscardVirtualMemory
// (but this is for an access pattern that immediately reuses the memory) // (but this is for an access pattern that immediately reuses the memory)
if (mi_option_is_enabled(mi_option_reset_discards)) { if (mi_option_is_enabled(mi_option_reset_discards) && pDiscardVirtualMemory != NULL) {
DWORD ok = DiscardVirtualMemory(start, csize); DWORD ok = (*pDiscardVirtualMemory)(start, csize);
mi_assert_internal(ok == 0); mi_assert_internal(ok == ERROR_SUCCESS);
if (ok != 0) return false; if (ok != ERROR_SUCCESS) return false;
} }
else { else {
void* p = VirtualAlloc(start, csize, MEM_RESET, PAGE_READWRITE); void* p = VirtualAlloc(start, csize, MEM_RESET, PAGE_READWRITE);
mi_assert_internal(p == start); mi_assert_internal(p == start);
if (p != start) return false; if (p != start) return false;
} }
/*
// VirtualUnlock removes the memory eagerly from the current working set (which MEM_RESET does lazily on demand)
// TODO: put this behind an option?
DWORD ok = VirtualUnlock(start, csize);
if (ok != 0) return false;
*/
#else #else
#if defined(MADV_FREE) #if defined(MADV_FREE)
static int advice = MADV_FREE; static int advice = MADV_FREE;

View file

@ -116,7 +116,8 @@ int main() {
for (int i = 0; i < TRANSFERS; i++) { for (int i = 0; i < TRANSFERS; i++) {
free_items((void*)transfer[i]); free_items((void*)transfer[i]);
} }
mi_collect(false); mi_collect(false); // ensures abandoned segments are reclaimed
mi_collect(true); // frees everything
mi_stats_print(NULL); mi_stats_print(NULL);
return 0; return 0;
} }