Changes to update Tomato RAF.
[tomato.git] / release / src / lzma / C / Alloc.c
blob4ce8f108f0b28854ea5baed5f2052d7004b1f09d
1 /* Alloc.c */
3 #ifdef _WIN32
4 #include <windows.h>
5 #endif
6 #include <stdlib.h>
8 #include "Alloc.h"
10 /* #define _SZ_ALLOC_DEBUG */
12 /* use _SZ_ALLOC_DEBUG to debug alloc/free operations */
13 #ifdef _SZ_ALLOC_DEBUG
14 #include <stdio.h>
15 int g_allocCount = 0;
16 int g_allocCountMid = 0;
17 int g_allocCountBig = 0;
18 #endif
20 void *MyAlloc(size_t size)
22 if (size == 0)
23 return 0;
24 #ifdef _SZ_ALLOC_DEBUG
25 fprintf(stderr, "\nAlloc %10d bytes; count = %10d", size, g_allocCount++);
26 #endif
27 return malloc(size);
30 void MyFree(void *address)
32 #ifdef _SZ_ALLOC_DEBUG
33 if (address != 0)
34 fprintf(stderr, "\nFree; count = %10d", --g_allocCount);
35 #endif
36 free(address);
39 #ifdef _WIN32
41 void *MidAlloc(size_t size)
43 if (size == 0)
44 return 0;
45 #ifdef _SZ_ALLOC_DEBUG
46 fprintf(stderr, "\nAlloc_Mid %10d bytes; count = %10d", size, g_allocCountMid++);
47 #endif
48 return VirtualAlloc(0, size, MEM_COMMIT, PAGE_READWRITE);
51 void MidFree(void *address)
53 #ifdef _SZ_ALLOC_DEBUG
54 if (address != 0)
55 fprintf(stderr, "\nFree_Mid; count = %10d", --g_allocCountMid);
56 #endif
57 if (address == 0)
58 return;
59 VirtualFree(address, 0, MEM_RELEASE);
62 #ifndef MEM_LARGE_PAGES
63 #undef _7ZIP_LARGE_PAGES
64 #endif
66 #ifdef _7ZIP_LARGE_PAGES
67 SIZE_T g_LargePageSize = 0;
68 typedef SIZE_T (WINAPI *GetLargePageMinimumP)();
69 #endif
71 void SetLargePageSize()
73 #ifdef _7ZIP_LARGE_PAGES
74 SIZE_T size = 0;
75 GetLargePageMinimumP largePageMinimum = (GetLargePageMinimumP)
76 GetProcAddress(GetModuleHandle(TEXT("kernel32.dll")), "GetLargePageMinimum");
77 if (largePageMinimum == 0)
78 return;
79 size = largePageMinimum();
80 if (size == 0 || (size & (size - 1)) != 0)
81 return;
82 g_LargePageSize = size;
83 #endif
87 void *BigAlloc(size_t size)
89 if (size == 0)
90 return 0;
91 #ifdef _SZ_ALLOC_DEBUG
92 fprintf(stderr, "\nAlloc_Big %10d bytes; count = %10d", size, g_allocCountBig++);
93 #endif
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);
100 if (res != 0)
101 return res;
103 #endif
104 return VirtualAlloc(0, size, MEM_COMMIT, PAGE_READWRITE);
107 void BigFree(void *address)
109 #ifdef _SZ_ALLOC_DEBUG
110 if (address != 0)
111 fprintf(stderr, "\nFree_Big; count = %10d", --g_allocCountBig);
112 #endif
114 if (address == 0)
115 return;
116 VirtualFree(address, 0, MEM_RELEASE);
119 #endif