OLD | NEW |
1 /* Alloc.c */ | 1 /* Alloc.c -- Memory allocation functions |
| 2 2008-09-24 |
| 3 Igor Pavlov |
| 4 Public domain */ |
2 | 5 |
3 #ifdef _WIN32 | 6 #ifdef _WIN32 |
4 #include <windows.h> | 7 #include <windows.h> |
5 #endif | 8 #endif |
6 #include <stdlib.h> | 9 #include <stdlib.h> |
7 | 10 |
8 #include "Alloc.h" | 11 #include "Alloc.h" |
9 | 12 |
10 /* #define _SZ_ALLOC_DEBUG */ | 13 /* #define _SZ_ALLOC_DEBUG */ |
11 | 14 |
12 /* use _SZ_ALLOC_DEBUG to debug alloc/free operations */ | 15 /* use _SZ_ALLOC_DEBUG to debug alloc/free operations */ |
13 #ifdef _SZ_ALLOC_DEBUG | 16 #ifdef _SZ_ALLOC_DEBUG |
14 #include <stdio.h> | 17 #include <stdio.h> |
15 int g_allocCount = 0; | 18 int g_allocCount = 0; |
16 int g_allocCountMid = 0; | 19 int g_allocCountMid = 0; |
17 int g_allocCountBig = 0; | 20 int g_allocCountBig = 0; |
18 #endif | 21 #endif |
19 | 22 |
20 void *MyAlloc(size_t size) | 23 void *MyAlloc(size_t size) |
21 { | 24 { |
22 if (size == 0) | 25 if (size == 0) |
23 return 0; | 26 return 0; |
24 #ifdef _SZ_ALLOC_DEBUG | 27 #ifdef _SZ_ALLOC_DEBUG |
25 fprintf(stderr, "\nAlloc %10d bytes; count = %10d", size, g_allocCount++); | 28 { |
| 29 void *p = malloc(size); |
| 30 fprintf(stderr, "\nAlloc %10d bytes, count = %10d, addr = %8X", size, g_all
ocCount++, (unsigned)p); |
| 31 return p; |
| 32 } |
| 33 #else |
| 34 return malloc(size); |
26 #endif | 35 #endif |
27 return malloc(size); | |
28 } | 36 } |
29 | 37 |
30 void MyFree(void *address) | 38 void MyFree(void *address) |
31 { | 39 { |
32 #ifdef _SZ_ALLOC_DEBUG | 40 #ifdef _SZ_ALLOC_DEBUG |
33 if (address != 0) | 41 if (address != 0) |
34 fprintf(stderr, "\nFree; count = %10d", --g_allocCount); | 42 fprintf(stderr, "\nFree; count = %10d, addr = %8X", --g_allocCount, (unsign
ed)address); |
35 #endif | 43 #endif |
36 free(address); | 44 free(address); |
37 } | 45 } |
38 | 46 |
39 #ifdef _WIN32 | 47 #ifdef _WIN32 |
40 | 48 |
41 void *MidAlloc(size_t size) | 49 void *MidAlloc(size_t size) |
42 { | 50 { |
43 if (size == 0) | 51 if (size == 0) |
44 return 0; | 52 return 0; |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
88 { | 96 { |
89 if (size == 0) | 97 if (size == 0) |
90 return 0; | 98 return 0; |
91 #ifdef _SZ_ALLOC_DEBUG | 99 #ifdef _SZ_ALLOC_DEBUG |
92 fprintf(stderr, "\nAlloc_Big %10d bytes; count = %10d", size, g_allocCountBig
++); | 100 fprintf(stderr, "\nAlloc_Big %10d bytes; count = %10d", size, g_allocCountBig
++); |
93 #endif | 101 #endif |
94 | 102 |
95 #ifdef _7ZIP_LARGE_PAGES | 103 #ifdef _7ZIP_LARGE_PAGES |
96 if (g_LargePageSize != 0 && g_LargePageSize <= (1 << 30) && size >= (1 << 18)) | 104 if (g_LargePageSize != 0 && g_LargePageSize <= (1 << 30) && size >= (1 << 18)) |
97 { | 105 { |
98 void *res = VirtualAlloc(0, (size + g_LargePageSize - 1) & (~(g_LargePageSiz
e - 1)), | 106 void *res = VirtualAlloc(0, (size + g_LargePageSize - 1) & (~(g_LargePageSiz
e - 1)), |
99 MEM_COMMIT | MEM_LARGE_PAGES, PAGE_READWRITE); | 107 MEM_COMMIT | MEM_LARGE_PAGES, PAGE_READWRITE); |
100 if (res != 0) | 108 if (res != 0) |
101 return res; | 109 return res; |
102 } | 110 } |
103 #endif | 111 #endif |
104 return VirtualAlloc(0, size, MEM_COMMIT, PAGE_READWRITE); | 112 return VirtualAlloc(0, size, MEM_COMMIT, PAGE_READWRITE); |
105 } | 113 } |
106 | 114 |
107 void BigFree(void *address) | 115 void BigFree(void *address) |
108 { | 116 { |
109 #ifdef _SZ_ALLOC_DEBUG | 117 #ifdef _SZ_ALLOC_DEBUG |
110 if (address != 0) | 118 if (address != 0) |
111 fprintf(stderr, "\nFree_Big; count = %10d", --g_allocCountBig); | 119 fprintf(stderr, "\nFree_Big; count = %10d", --g_allocCountBig); |
112 #endif | 120 #endif |
113 | 121 |
114 if (address == 0) | 122 if (address == 0) |
115 return; | 123 return; |
116 VirtualFree(address, 0, MEM_RELEASE); | 124 VirtualFree(address, 0, MEM_RELEASE); |
117 } | 125 } |
118 | 126 |
119 #endif | 127 #endif |
OLD | NEW |