Debian Codegraph @

src/lzma/Alloc.c

raw
1 /* Alloc.c -- Memory allocation functions
2 2008-09-24
3 Igor Pavlov
4 Public domain */
5 // ----------------------------------------------------------------------------
6 // Copyright (C) 2014
7 // David Freese, W1HKJ
8 //
9 // This file is part of flmsg
10 //
11 // flrig is free software; you can redistribute it and/or modify
12 // it under the terms of the GNU General Public License as published by
13 // the Free Software Foundation; either version 3 of the License, or
14 // (at your option) any later version.
15 //
16 // flrig is distributed in the hope that it will be useful,
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 // GNU General Public License for more details.
20 //
21 // You should have received a copy of the GNU General Public License
22 // along with this program. If not, see <http://www.gnu.org/licenses/>.
23 // ----------------------------------------------------------------------------
24
25 #ifdef _WIN32
26 #include <windows.h>
27 #endif
28 #include <stdlib.h>
29
30 #include "Alloc.h"
31
32 /* #define _SZ_ALLOC_DEBUG */
33
34 /* use _SZ_ALLOC_DEBUG to debug alloc/free operations */
35 #ifdef _SZ_ALLOC_DEBUG
36 #include <stdio.h>
37 int g_allocCount = 0;
38 int g_allocCountMid = 0;
39 int g_allocCountBig = 0;
40 #endif
41
42 void *MyAlloc(size_t size)
43 {
44 if (size == 0)
45 return 0;
46 #ifdef _SZ_ALLOC_DEBUG
47 {
48 void *p = malloc(size);
49 fprintf(stderr, "\nAlloc %10d bytes, count = %10d, addr = %8X", size, g_allocCount++, (unsigned)p);
50 return p;
51 }
52 #else
53 return malloc(size);
54 #endif
55 }
56
57 void MyFree(void *address)
58 {
59 #ifdef _SZ_ALLOC_DEBUG
60 if (address != 0)
61 fprintf(stderr, "\nFree; count = %10d, addr = %8X", --g_allocCount, (unsigned)address);
62 #endif
63 free(address);
64 }
65
66 #ifdef _WIN32
67
68 void *MidAlloc(size_t size)
69 {
70 if (size == 0)
71 return 0;
72 #ifdef _SZ_ALLOC_DEBUG
73 fprintf(stderr, "\nAlloc_Mid %10d bytes; count = %10d", size, g_allocCountMid++);
74 #endif
75 return VirtualAlloc(0, size, MEM_COMMIT, PAGE_READWRITE);
76 }
77
78 void MidFree(void *address)
79 {
80 #ifdef _SZ_ALLOC_DEBUG
81 if (address != 0)
82 fprintf(stderr, "\nFree_Mid; count = %10d", --g_allocCountMid);
83 #endif
84 if (address == 0)
85 return;
86 VirtualFree(address, 0, MEM_RELEASE);
87 }
88
89 #ifndef MEM_LARGE_PAGES
90 #undef _7ZIP_LARGE_PAGES
91 #endif
92
93 #ifdef _7ZIP_LARGE_PAGES
94 SIZE_T g_LargePageSize = 0;
95 typedef SIZE_T (WINAPI *GetLargePageMinimumP)();
96 #endif
97
98 void SetLargePageSize()
99 {
100 #ifdef _7ZIP_LARGE_PAGES
101 SIZE_T size = 0;
102 GetLargePageMinimumP largePageMinimum = (GetLargePageMinimumP)
103 GetProcAddress(GetModuleHandle(TEXT("kernel32.dll")), "GetLargePageMinimum");
104 if (largePageMinimum == 0)
105 return;
106 size = largePageMinimum();
107 if (size == 0 || (size & (size - 1)) != 0)
108 return;
109 g_LargePageSize = size;
110 #endif
111 }
112
113
114 void *BigAlloc(size_t size)
115 {
116 if (size == 0)
117 return 0;
118 #ifdef _SZ_ALLOC_DEBUG
119 fprintf(stderr, "\nAlloc_Big %10d bytes; count = %10d", size, g_allocCountBig++);
120 #endif
121
122 #ifdef _7ZIP_LARGE_PAGES
123 if (g_LargePageSize != 0 && g_LargePageSize <= (1 << 30) && size >= (1 << 18))
124 {
125 void *res = VirtualAlloc(0, (size + g_LargePageSize - 1) & (~(g_LargePageSize - 1)),
126 MEM_COMMIT | MEM_LARGE_PAGES, PAGE_READWRITE);
127 if (res != 0)
128 return res;
129 }
130 #endif
131 return VirtualAlloc(0, size, MEM_COMMIT, PAGE_READWRITE);
132 }
133
134 void BigFree(void *address)
135 {
136 #ifdef _SZ_ALLOC_DEBUG
137 if (address != 0)
138 fprintf(stderr, "\nFree_Big; count = %10d", --g_allocCountBig);
139 #endif
140
141 if (address == 0)
142 return;
143 VirtualFree(address, 0, MEM_RELEASE);
144 }
145
146 #endif