rlm@1: /* Alloc.c -- Memory allocation functions rlm@1: 2008-09-24 rlm@1: Igor Pavlov rlm@1: Public domain */ rlm@1: rlm@1: #ifdef _WIN32 rlm@1: #include rlm@1: #endif rlm@1: #include rlm@1: rlm@1: #include "Alloc.h" rlm@1: rlm@1: /* #define _SZ_ALLOC_DEBUG */ rlm@1: rlm@1: /* use _SZ_ALLOC_DEBUG to debug alloc/free operations */ rlm@1: #ifdef _SZ_ALLOC_DEBUG rlm@1: #include rlm@1: int g_allocCount = 0; rlm@1: int g_allocCountMid = 0; rlm@1: int g_allocCountBig = 0; rlm@1: #endif rlm@1: rlm@1: void *MyAlloc(size_t size) rlm@1: { rlm@1: if (size == 0) rlm@1: return 0; rlm@1: #ifdef _SZ_ALLOC_DEBUG rlm@1: { rlm@1: void *p = malloc(size); rlm@1: fprintf(stderr, "\nAlloc %10d bytes, count = %10d, addr = %8X", size, g_allocCount++, (unsigned)p); rlm@1: return p; rlm@1: } rlm@1: #else rlm@1: return malloc(size); rlm@1: #endif rlm@1: } rlm@1: rlm@1: void MyFree(void *address) rlm@1: { rlm@1: #ifdef _SZ_ALLOC_DEBUG rlm@1: if (address != 0) rlm@1: fprintf(stderr, "\nFree; count = %10d, addr = %8X", --g_allocCount, (unsigned)address); rlm@1: #endif rlm@1: free(address); rlm@1: } rlm@1: rlm@1: #ifdef _WIN32 rlm@1: rlm@1: void *MidAlloc(size_t size) rlm@1: { rlm@1: if (size == 0) rlm@1: return 0; rlm@1: #ifdef _SZ_ALLOC_DEBUG rlm@1: fprintf(stderr, "\nAlloc_Mid %10d bytes; count = %10d", size, g_allocCountMid++); rlm@1: #endif rlm@1: return VirtualAlloc(0, size, MEM_COMMIT, PAGE_READWRITE); rlm@1: } rlm@1: rlm@1: void MidFree(void *address) rlm@1: { rlm@1: #ifdef _SZ_ALLOC_DEBUG rlm@1: if (address != 0) rlm@1: fprintf(stderr, "\nFree_Mid; count = %10d", --g_allocCountMid); rlm@1: #endif rlm@1: if (address == 0) rlm@1: return; rlm@1: VirtualFree(address, 0, MEM_RELEASE); rlm@1: } rlm@1: rlm@1: #ifndef MEM_LARGE_PAGES rlm@1: #undef _7ZIP_LARGE_PAGES rlm@1: #endif rlm@1: rlm@1: #ifdef _7ZIP_LARGE_PAGES rlm@1: SIZE_T g_LargePageSize = 0; rlm@1: typedef SIZE_T (WINAPI *GetLargePageMinimumP)(); rlm@1: #endif rlm@1: rlm@1: void SetLargePageSize() rlm@1: { rlm@1: #ifdef _7ZIP_LARGE_PAGES rlm@1: SIZE_T size = 0; rlm@1: GetLargePageMinimumP largePageMinimum = (GetLargePageMinimumP) rlm@1: GetProcAddress(GetModuleHandle(TEXT("kernel32.dll")), "GetLargePageMinimum"); rlm@1: if (largePageMinimum == 0) rlm@1: return; rlm@1: size = largePageMinimum(); rlm@1: if (size == 0 || (size & (size - 1)) != 0) rlm@1: return; rlm@1: g_LargePageSize = size; rlm@1: #endif rlm@1: } rlm@1: rlm@1: rlm@1: void *BigAlloc(size_t size) rlm@1: { rlm@1: if (size == 0) rlm@1: return 0; rlm@1: #ifdef _SZ_ALLOC_DEBUG rlm@1: fprintf(stderr, "\nAlloc_Big %10d bytes; count = %10d", size, g_allocCountBig++); rlm@1: #endif rlm@1: rlm@1: #ifdef _7ZIP_LARGE_PAGES rlm@1: if (g_LargePageSize != 0 && g_LargePageSize <= (1 << 30) && size >= (1 << 18)) rlm@1: { rlm@1: void *res = VirtualAlloc(0, (size + g_LargePageSize - 1) & (~(g_LargePageSize - 1)), rlm@1: MEM_COMMIT | MEM_LARGE_PAGES, PAGE_READWRITE); rlm@1: if (res != 0) rlm@1: return res; rlm@1: } rlm@1: #endif rlm@1: return VirtualAlloc(0, size, MEM_COMMIT, PAGE_READWRITE); rlm@1: } rlm@1: rlm@1: void BigFree(void *address) rlm@1: { rlm@1: #ifdef _SZ_ALLOC_DEBUG rlm@1: if (address != 0) rlm@1: fprintf(stderr, "\nFree_Big; count = %10d", --g_allocCountBig); rlm@1: #endif rlm@1: rlm@1: if (address == 0) rlm@1: return; rlm@1: VirtualFree(address, 0, MEM_RELEASE); rlm@1: } rlm@1: rlm@1: #endif