michael@0: // Common/Alloc.cpp michael@0: michael@0: #include "StdAfx.h" michael@0: michael@0: #ifdef _WIN32 michael@0: #include "MyWindows.h" michael@0: #else michael@0: #include michael@0: #endif michael@0: michael@0: #include "Alloc.h" michael@0: michael@0: /* #define _SZ_ALLOC_DEBUG */ michael@0: /* use _SZ_ALLOC_DEBUG to debug alloc/free operations */ michael@0: #ifdef _SZ_ALLOC_DEBUG michael@0: #include michael@0: int g_allocCount = 0; michael@0: int g_allocCountMid = 0; michael@0: int g_allocCountBig = 0; michael@0: #endif michael@0: michael@0: void *MyAlloc(size_t size) throw() michael@0: { michael@0: if (size == 0) michael@0: return 0; michael@0: #ifdef _SZ_ALLOC_DEBUG michael@0: fprintf(stderr, "\nAlloc %10d bytes; count = %10d", size, g_allocCount++); michael@0: #endif michael@0: return ::malloc(size); michael@0: } michael@0: michael@0: void MyFree(void *address) throw() michael@0: { michael@0: #ifdef _SZ_ALLOC_DEBUG michael@0: if (address != 0) michael@0: fprintf(stderr, "\nFree; count = %10d", --g_allocCount); michael@0: #endif michael@0: michael@0: ::free(address); michael@0: } michael@0: michael@0: #ifdef _WIN32 michael@0: michael@0: void *MidAlloc(size_t size) throw() michael@0: { michael@0: if (size == 0) michael@0: return 0; michael@0: #ifdef _SZ_ALLOC_DEBUG michael@0: fprintf(stderr, "\nAlloc_Mid %10d bytes; count = %10d", size, g_allocCountMid++); michael@0: #endif michael@0: return ::VirtualAlloc(0, size, MEM_COMMIT, PAGE_READWRITE); michael@0: } michael@0: michael@0: void MidFree(void *address) throw() michael@0: { michael@0: #ifdef _SZ_ALLOC_DEBUG michael@0: if (address != 0) michael@0: fprintf(stderr, "\nFree_Mid; count = %10d", --g_allocCountMid); michael@0: #endif michael@0: if (address == 0) michael@0: return; michael@0: ::VirtualFree(address, 0, MEM_RELEASE); michael@0: } michael@0: michael@0: static SIZE_T g_LargePageSize = michael@0: #ifdef _WIN64 michael@0: (1 << 21); michael@0: #else michael@0: (1 << 22); michael@0: #endif michael@0: michael@0: typedef SIZE_T (WINAPI *GetLargePageMinimumP)(); michael@0: michael@0: bool SetLargePageSize() michael@0: { michael@0: GetLargePageMinimumP largePageMinimum = (GetLargePageMinimumP) michael@0: ::GetProcAddress(::GetModuleHandle(TEXT("kernel32.dll")), "GetLargePageMinimum"); michael@0: if (largePageMinimum == 0) michael@0: return false; michael@0: SIZE_T size = largePageMinimum(); michael@0: if (size == 0 || (size & (size - 1)) != 0) michael@0: return false; michael@0: g_LargePageSize = size; michael@0: return true; michael@0: } michael@0: michael@0: michael@0: void *BigAlloc(size_t size) throw() michael@0: { michael@0: if (size == 0) michael@0: return 0; michael@0: #ifdef _SZ_ALLOC_DEBUG michael@0: fprintf(stderr, "\nAlloc_Big %10d bytes; count = %10d", size, g_allocCountBig++); michael@0: #endif michael@0: michael@0: if (size >= (1 << 18)) michael@0: { michael@0: void *res = ::VirtualAlloc(0, (size + g_LargePageSize - 1) & (~(g_LargePageSize - 1)), michael@0: MEM_COMMIT | MEM_LARGE_PAGES, PAGE_READWRITE); michael@0: if (res != 0) michael@0: return res; michael@0: } michael@0: return ::VirtualAlloc(0, size, MEM_COMMIT, PAGE_READWRITE); michael@0: } michael@0: michael@0: void BigFree(void *address) throw() michael@0: { michael@0: #ifdef _SZ_ALLOC_DEBUG michael@0: if (address != 0) michael@0: fprintf(stderr, "\nFree_Big; count = %10d", --g_allocCountBig); michael@0: #endif michael@0: michael@0: if (address == 0) michael@0: return; michael@0: ::VirtualFree(address, 0, MEM_RELEASE); michael@0: } michael@0: michael@0: #endif