10 /* #define _SZ_ALLOC_DEBUG */
12 /* use _SZ_ALLOC_DEBUG to debug alloc/free operations */
13 #ifdef _SZ_ALLOC_DEBUG
16 int g_allocCountMid
= 0;
17 int g_allocCountBig
= 0;
20 void *MyAlloc(size_t size
)
24 #ifdef _SZ_ALLOC_DEBUG
25 fprintf(stderr
, "\nAlloc %10d bytes; count = %10d", size
, g_allocCount
++);
30 void MyFree(void *address
)
32 #ifdef _SZ_ALLOC_DEBUG
34 fprintf(stderr
, "\nFree; count = %10d", --g_allocCount
);
41 void *MidAlloc(size_t size
)
45 #ifdef _SZ_ALLOC_DEBUG
46 fprintf(stderr
, "\nAlloc_Mid %10d bytes; count = %10d", size
, g_allocCountMid
++);
48 return VirtualAlloc(0, size
, MEM_COMMIT
, PAGE_READWRITE
);
51 void MidFree(void *address
)
53 #ifdef _SZ_ALLOC_DEBUG
55 fprintf(stderr
, "\nFree_Mid; count = %10d", --g_allocCountMid
);
59 VirtualFree(address
, 0, MEM_RELEASE
);
62 #ifndef MEM_LARGE_PAGES
63 #undef _7ZIP_LARGE_PAGES
66 #ifdef _7ZIP_LARGE_PAGES
67 SIZE_T g_LargePageSize
= 0;
68 typedef SIZE_T (WINAPI
*GetLargePageMinimumP
)();
71 void SetLargePageSize()
73 #ifdef _7ZIP_LARGE_PAGES
75 GetLargePageMinimumP largePageMinimum
= (GetLargePageMinimumP
)
76 GetProcAddress(GetModuleHandle(TEXT("kernel32.dll")), "GetLargePageMinimum");
77 if (largePageMinimum
== 0)
79 size
= largePageMinimum();
80 if (size
== 0 || (size
& (size
- 1)) != 0)
82 g_LargePageSize
= size
;
87 void *BigAlloc(size_t size
)
91 #ifdef _SZ_ALLOC_DEBUG
92 fprintf(stderr
, "\nAlloc_Big %10d bytes; count = %10d", size
, g_allocCountBig
++);
95 #ifdef _7ZIP_LARGE_PAGES
96 if (g_LargePageSize
!= 0 && g_LargePageSize
<= (1 << 30) && size
>= (1 << 18))
98 void *res
= VirtualAlloc(0, (size
+ g_LargePageSize
- 1) & (~(g_LargePageSize
- 1)),
99 MEM_COMMIT
| MEM_LARGE_PAGES
, PAGE_READWRITE
);
104 return VirtualAlloc(0, size
, MEM_COMMIT
, PAGE_READWRITE
);
107 void BigFree(void *address
)
109 #ifdef _SZ_ALLOC_DEBUG
111 fprintf(stderr
, "\nFree_Big; count = %10d", --g_allocCountBig
);
116 VirtualFree(address
, 0, MEM_RELEASE
);