]> git.cworth.org Git - vogl/blob - src/voglcore/lzma_Alloc.cpp
Initial vogl checkin
[vogl] / src / voglcore / lzma_Alloc.cpp
1 /**************************************************************************
2  *
3  * Copyright 2013-2014 RAD Game Tools and Valve Software
4  * Copyright 2010-2014 Rich Geldreich and Tenacious Software LLC
5  * All Rights Reserved.
6  *
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:
13  *
14  * The above copyright notice and this permission notice shall be included in
15  * all copies or substantial portions of the Software.
16  *
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
23  * THE SOFTWARE.
24  *
25  **************************************************************************/
26
27 /* Alloc.c -- Memory allocation functions
28 2008-09-24
29 Igor Pavlov
30 Public domain */
31 #include "vogl_core.h"
32 #ifdef _WIN32
33 #include <windows.h>
34 #endif
35 #include <stdlib.h>
36
37 #include "lzma_Alloc.h"
38
39 namespace vogl
40 {
41
42 /* #define _SZ_ALLOC_DEBUG */
43
44 /* use _SZ_ALLOC_DEBUG to debug alloc/free operations */
45 #ifdef _SZ_ALLOC_DEBUG
46 #include <stdio.h>
47     int g_allocCount = 0;
48     int g_allocCountMid = 0;
49     int g_allocCountBig = 0;
50 #endif
51
52     void *MyAlloc(size_t size)
53     {
54         if (size == 0)
55             return 0;
56 #ifdef _SZ_ALLOC_DEBUG
57         {
58             void *p = vogl_malloc(size);
59             fprintf(stderr, "\nAlloc %10d bytes, count = %10d,  addr = %8X", size, g_allocCount++, (unsigned)p);
60             return p;
61         }
62 #else
63         return vogl_malloc(size);
64 #endif
65     }
66
67     void MyFree(void *address)
68     {
69 #ifdef _SZ_ALLOC_DEBUG
70         if (address != 0)
71             fprintf(stderr, "\nFree; count = %10d,  addr = %8X", --g_allocCount, (unsigned)address);
72 #endif
73         vogl_free(address);
74     }
75
76 #ifdef _WIN32
77
78     void *MidAlloc(size_t size)
79     {
80         if (size == 0)
81             return 0;
82 #ifdef _SZ_ALLOC_DEBUG
83         fprintf(stderr, "\nAlloc_Mid %10d bytes;  count = %10d", size, g_allocCountMid++);
84 #endif
85         return VirtualAlloc(0, size, MEM_COMMIT, PAGE_READWRITE);
86     }
87
88     void MidFree(void *address)
89     {
90 #ifdef _SZ_ALLOC_DEBUG
91         if (address != 0)
92             fprintf(stderr, "\nFree_Mid; count = %10d", --g_allocCountMid);
93 #endif
94         if (address == 0)
95             return;
96         VirtualFree(address, 0, MEM_RELEASE);
97     }
98
99 #ifndef MEM_LARGE_PAGES
100 #undef _7ZIP_LARGE_PAGES
101 #endif
102
103 #ifdef _7ZIP_LARGE_PAGES
104     SIZE_T g_LargePageSize = 0;
105     typedef SIZE_T(WINAPI *GetLargePageMinimumP)();
106 #endif
107
108     void SetLargePageSize()
109     {
110 #ifdef _7ZIP_LARGE_PAGES
111         SIZE_T size = 0;
112         GetLargePageMinimumP largePageMinimum = (GetLargePageMinimumP)
113             GetProcAddress(GetModuleHandle(TEXT("kernel32.dll")), "GetLargePageMinimum");
114         if (largePageMinimum == 0)
115             return;
116         size = largePageMinimum();
117         if (size == 0 || (size & (size - 1)) != 0)
118             return;
119         g_LargePageSize = size;
120 #endif
121     }
122
123     void *BigAlloc(size_t size)
124     {
125         if (size == 0)
126             return 0;
127 #ifdef _SZ_ALLOC_DEBUG
128         fprintf(stderr, "\nAlloc_Big %10d bytes;  count = %10d", size, g_allocCountBig++);
129 #endif
130
131 #ifdef _7ZIP_LARGE_PAGES
132         if (g_LargePageSize != 0 && g_LargePageSize <= (1 << 30) && size >= (1 << 18))
133         {
134             void *res = VirtualAlloc(0, (size + g_LargePageSize - 1) & (~(g_LargePageSize - 1)),
135                                      MEM_COMMIT | MEM_LARGE_PAGES, PAGE_READWRITE);
136             if (res != 0)
137                 return res;
138         }
139 #endif
140         return VirtualAlloc(0, size, MEM_COMMIT, PAGE_READWRITE);
141     }
142
143     void BigFree(void *address)
144     {
145 #ifdef _SZ_ALLOC_DEBUG
146         if (address != 0)
147             fprintf(stderr, "\nFree_Big; count = %10d", --g_allocCountBig);
148 #endif
149
150         if (address == 0)
151             return;
152         VirtualFree(address, 0, MEM_RELEASE);
153     }
154
155 #endif
156 }