13 /* #define _SZ_ALLOC_DEBUG */
14 /* use _SZ_ALLOC_DEBUG to debug alloc/free operations */
15 #ifdef _SZ_ALLOC_DEBUG
18 int g_allocCountMid
= 0;
19 int g_allocCountBig
= 0;
22 void *MyAlloc(size_t size
) throw()
26 #ifdef _SZ_ALLOC_DEBUG
27 fprintf(stderr
, "\nAlloc %10d bytes; count = %10d", size
, g_allocCount
++);
29 return ::malloc(size
);
32 void MyFree(void *address
) throw()
34 #ifdef _SZ_ALLOC_DEBUG
36 fprintf(stderr
, "\nFree; count = %10d", --g_allocCount
);
44 void *MidAlloc(size_t size
) throw()
48 #ifdef _SZ_ALLOC_DEBUG
49 fprintf(stderr
, "\nAlloc_Mid %10d bytes; count = %10d", size
, g_allocCountMid
++);
51 return ::VirtualAlloc(0, size
, MEM_COMMIT
, PAGE_READWRITE
);
54 void MidFree(void *address
) throw()
56 #ifdef _SZ_ALLOC_DEBUG
58 fprintf(stderr
, "\nFree_Mid; count = %10d", --g_allocCountMid
);
62 ::VirtualFree(address
, 0, MEM_RELEASE
);
65 static SIZE_T g_LargePageSize
=
72 typedef SIZE_T (WINAPI
*GetLargePageMinimumP
)();
74 bool SetLargePageSize()
76 GetLargePageMinimumP largePageMinimum
= (GetLargePageMinimumP
)
77 ::GetProcAddress(::GetModuleHandle(TEXT("kernel32.dll")), "GetLargePageMinimum");
78 if (largePageMinimum
== 0)
80 SIZE_T size
= largePageMinimum();
81 if (size
== 0 || (size
& (size
- 1)) != 0)
83 g_LargePageSize
= size
;
88 void *BigAlloc(size_t size
) throw()
92 #ifdef _SZ_ALLOC_DEBUG
93 fprintf(stderr
, "\nAlloc_Big %10d bytes; count = %10d", size
, g_allocCountBig
++);
96 if (size
>= (1 << 18))
98 void *res
= ::VirtualAlloc(0, (size
+ g_LargePageSize
- 1) & (~(g_LargePageSize
- 1)),
99 MEM_COMMIT
| MEM_LARGE_PAGES
, PAGE_READWRITE
);
103 return ::VirtualAlloc(0, size
, MEM_COMMIT
, PAGE_READWRITE
);
106 void BigFree(void *address
) throw()
108 #ifdef _SZ_ALLOC_DEBUG
110 fprintf(stderr
, "\nFree_Big; count = %10d", --g_allocCountBig
);
115 ::VirtualFree(address
, 0, MEM_RELEASE
);