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