1 /* Alloc.c -- Memory allocation functions
13 /* #define _SZ_ALLOC_DEBUG */
15 /* use _SZ_ALLOC_DEBUG to debug alloc/free operations */
16 #ifdef _SZ_ALLOC_DEBUG
19 int g_allocCountMid = 0;
20 int g_allocCountBig = 0;
23 void *MyAlloc(size_t size)
27 #ifdef _SZ_ALLOC_DEBUG
29 void *p = malloc(size);
30 fprintf(stderr, "\nAlloc %10d bytes, count = %10d, addr = %8X", size, g_allocCount++, (unsigned)p);
38 void MyFree(void *address)
40 #ifdef _SZ_ALLOC_DEBUG
42 fprintf(stderr, "\nFree; count = %10d, addr = %8X", --g_allocCount, (unsigned)address);
49 void *MidAlloc(size_t size)
53 #ifdef _SZ_ALLOC_DEBUG
54 fprintf(stderr, "\nAlloc_Mid %10d bytes; count = %10d", size, g_allocCountMid++);
56 return VirtualAlloc(0, size, MEM_COMMIT, PAGE_READWRITE);
59 void MidFree(void *address)
61 #ifdef _SZ_ALLOC_DEBUG
63 fprintf(stderr, "\nFree_Mid; count = %10d", --g_allocCountMid);
67 VirtualFree(address, 0, MEM_RELEASE);
70 #ifndef MEM_LARGE_PAGES
71 #undef _7ZIP_LARGE_PAGES
74 #ifdef _7ZIP_LARGE_PAGES
75 SIZE_T g_LargePageSize = 0;
76 typedef SIZE_T (WINAPI *GetLargePageMinimumP)();
79 void SetLargePageSize()
81 #ifdef _7ZIP_LARGE_PAGES
83 GetLargePageMinimumP largePageMinimum = (GetLargePageMinimumP)
84 GetProcAddress(GetModuleHandle(TEXT("kernel32.dll")), "GetLargePageMinimum");
85 if (largePageMinimum == 0)
87 size = largePageMinimum();
88 if (size == 0 || (size & (size - 1)) != 0)
90 g_LargePageSize = size;
95 void *BigAlloc(size_t size)
99 #ifdef _SZ_ALLOC_DEBUG
100 fprintf(stderr, "\nAlloc_Big %10d bytes; count = %10d", size, g_allocCountBig++);
103 #ifdef _7ZIP_LARGE_PAGES
104 if (g_LargePageSize != 0 && g_LargePageSize <= (1 << 30) && size >= (1 << 18))
106 void *res = VirtualAlloc(0, (size + g_LargePageSize - 1) & (~(g_LargePageSize - 1)),
107 MEM_COMMIT | MEM_LARGE_PAGES, PAGE_READWRITE);
112 return VirtualAlloc(0, size, MEM_COMMIT, PAGE_READWRITE);
115 void BigFree(void *address)
117 #ifdef _SZ_ALLOC_DEBUG
119 fprintf(stderr, "\nFree_Big; count = %10d", --g_allocCountBig);
124 VirtualFree(address, 0, MEM_RELEASE);