v2/documentation: romfs -> cbfs rename
[coreboot.git] / util / romtool / tools / lzma / C / Common / Alloc.cpp
1 // Common/Alloc.cpp\r
2 \r
3 #include "StdAfx.h"\r
4 \r
5 #ifdef _WIN32\r
6 #include "MyWindows.h"\r
7 #else\r
8 #include <stdlib.h>\r
9 #endif\r
10 \r
11 #include "Alloc.h"\r
12 \r
13 /* #define _SZ_ALLOC_DEBUG */\r
14 /* use _SZ_ALLOC_DEBUG to debug alloc/free operations */\r
15 #ifdef _SZ_ALLOC_DEBUG\r
16 #include <stdio.h>\r
17 int g_allocCount = 0;\r
18 int g_allocCountMid = 0;\r
19 int g_allocCountBig = 0;\r
20 #endif\r
21 \r
22 void *MyAlloc(size_t size) throw()\r
23 {\r
24   if (size == 0)\r
25     return 0;\r
26   #ifdef _SZ_ALLOC_DEBUG\r
27   fprintf(stderr, "\nAlloc %10d bytes; count = %10d", size, g_allocCount++);\r
28   #endif\r
29   return ::malloc(size);\r
30 }\r
31 \r
32 void MyFree(void *address) throw()\r
33 {\r
34   #ifdef _SZ_ALLOC_DEBUG\r
35   if (address != 0)\r
36     fprintf(stderr, "\nFree; count = %10d", --g_allocCount);\r
37   #endif\r
38   \r
39   ::free(address);\r
40 }\r
41 \r
42 #ifdef _WIN32\r
43 \r
44 void *MidAlloc(size_t size) throw()\r
45 {\r
46   if (size == 0)\r
47     return 0;\r
48   #ifdef _SZ_ALLOC_DEBUG\r
49   fprintf(stderr, "\nAlloc_Mid %10d bytes;  count = %10d", size, g_allocCountMid++);\r
50   #endif\r
51   return ::VirtualAlloc(0, size, MEM_COMMIT, PAGE_READWRITE);\r
52 }\r
53 \r
54 void MidFree(void *address) throw()\r
55 {\r
56   #ifdef _SZ_ALLOC_DEBUG\r
57   if (address != 0)\r
58     fprintf(stderr, "\nFree_Mid; count = %10d", --g_allocCountMid);\r
59   #endif\r
60   if (address == 0)\r
61     return;\r
62   ::VirtualFree(address, 0, MEM_RELEASE);\r
63 }\r
64 \r
65 static SIZE_T g_LargePageSize = \r
66     #ifdef _WIN64\r
67     (1 << 21);\r
68     #else\r
69     (1 << 22);\r
70     #endif\r
71 \r
72 typedef SIZE_T (WINAPI *GetLargePageMinimumP)();\r
73 \r
74 bool SetLargePageSize()\r
75 {\r
76   GetLargePageMinimumP largePageMinimum = (GetLargePageMinimumP)\r
77         ::GetProcAddress(::GetModuleHandle(TEXT("kernel32.dll")), "GetLargePageMinimum");\r
78   if (largePageMinimum == 0)\r
79     return false;\r
80   SIZE_T size = largePageMinimum();\r
81   if (size == 0 || (size & (size - 1)) != 0)\r
82     return false;\r
83   g_LargePageSize = size;\r
84   return true;\r
85 }\r
86 \r
87 \r
88 void *BigAlloc(size_t size) throw()\r
89 {\r
90   if (size == 0)\r
91     return 0;\r
92   #ifdef _SZ_ALLOC_DEBUG\r
93   fprintf(stderr, "\nAlloc_Big %10d bytes;  count = %10d", size, g_allocCountBig++);\r
94   #endif\r
95   \r
96   if (size >= (1 << 18))\r
97   {\r
98     void *res = ::VirtualAlloc(0, (size + g_LargePageSize - 1) & (~(g_LargePageSize - 1)), \r
99         MEM_COMMIT | MEM_LARGE_PAGES, PAGE_READWRITE);\r
100     if (res != 0)\r
101       return res;\r
102   }\r
103   return ::VirtualAlloc(0, size, MEM_COMMIT, PAGE_READWRITE);\r
104 }\r
105 \r
106 void BigFree(void *address) throw()\r
107 {\r
108   #ifdef _SZ_ALLOC_DEBUG\r
109   if (address != 0)\r
110     fprintf(stderr, "\nFree_Big; count = %10d", --g_allocCountBig);\r
111   #endif\r
112   \r
113   if (address == 0)\r
114     return;\r
115   ::VirtualFree(address, 0, MEM_RELEASE);\r
116 }\r
117 \r
118 #endif\r