1 /**************************************************************************
3 * Copyright 2013-2014 RAD Game Tools and Valve Software
4 * Copyright 2010-2014 Rich Geldreich and Tenacious Software LLC
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 **************************************************************************/
27 /* Alloc.c -- Memory allocation functions
31 #include "vogl_core.h"
37 #include "lzma_Alloc.h"
42 /* #define _SZ_ALLOC_DEBUG */
44 /* use _SZ_ALLOC_DEBUG to debug alloc/free operations */
45 #ifdef _SZ_ALLOC_DEBUG
48 int g_allocCountMid = 0;
49 int g_allocCountBig = 0;
52 void *MyAlloc(size_t size)
56 #ifdef _SZ_ALLOC_DEBUG
58 void *p = vogl_malloc(size);
59 fprintf(stderr, "\nAlloc %10d bytes, count = %10d, addr = %8X", size, g_allocCount++, (unsigned)p);
63 return vogl_malloc(size);
67 void MyFree(void *address)
69 #ifdef _SZ_ALLOC_DEBUG
71 fprintf(stderr, "\nFree; count = %10d, addr = %8X", --g_allocCount, (unsigned)address);
78 void *MidAlloc(size_t size)
82 #ifdef _SZ_ALLOC_DEBUG
83 fprintf(stderr, "\nAlloc_Mid %10d bytes; count = %10d", size, g_allocCountMid++);
85 return VirtualAlloc(0, size, MEM_COMMIT, PAGE_READWRITE);
88 void MidFree(void *address)
90 #ifdef _SZ_ALLOC_DEBUG
92 fprintf(stderr, "\nFree_Mid; count = %10d", --g_allocCountMid);
96 VirtualFree(address, 0, MEM_RELEASE);
99 #ifndef MEM_LARGE_PAGES
100 #undef _7ZIP_LARGE_PAGES
103 #ifdef _7ZIP_LARGE_PAGES
104 SIZE_T g_LargePageSize = 0;
105 typedef SIZE_T(WINAPI *GetLargePageMinimumP)();
108 void SetLargePageSize()
110 #ifdef _7ZIP_LARGE_PAGES
112 GetLargePageMinimumP largePageMinimum = (GetLargePageMinimumP)
113 GetProcAddress(GetModuleHandle(TEXT("kernel32.dll")), "GetLargePageMinimum");
114 if (largePageMinimum == 0)
116 size = largePageMinimum();
117 if (size == 0 || (size & (size - 1)) != 0)
119 g_LargePageSize = size;
123 void *BigAlloc(size_t size)
127 #ifdef _SZ_ALLOC_DEBUG
128 fprintf(stderr, "\nAlloc_Big %10d bytes; count = %10d", size, g_allocCountBig++);
131 #ifdef _7ZIP_LARGE_PAGES
132 if (g_LargePageSize != 0 && g_LargePageSize <= (1 << 30) && size >= (1 << 18))
134 void *res = VirtualAlloc(0, (size + g_LargePageSize - 1) & (~(g_LargePageSize - 1)),
135 MEM_COMMIT | MEM_LARGE_PAGES, PAGE_READWRITE);
140 return VirtualAlloc(0, size, MEM_COMMIT, PAGE_READWRITE);
143 void BigFree(void *address)
145 #ifdef _SZ_ALLOC_DEBUG
147 fprintf(stderr, "\nFree_Big; count = %10d", --g_allocCountBig);
152 VirtualFree(address, 0, MEM_RELEASE);