OLD | NEW |
(Empty) | |
| 1 /* Alloc.c -- Memory allocation functions |
| 2 2008-09-24 |
| 3 Igor Pavlov |
| 4 Public domain */ |
| 5 |
| 6 #ifdef _WIN32 |
| 7 #include <windows.h> |
| 8 #endif |
| 9 #include <stdlib.h> |
| 10 |
| 11 #include "Alloc.h" |
| 12 |
| 13 namespace ots { |
| 14 namespace lzma { |
| 15 |
| 16 /* #define _SZ_ALLOC_DEBUG */ |
| 17 |
| 18 /* use _SZ_ALLOC_DEBUG to debug alloc/free operations */ |
| 19 #ifdef _SZ_ALLOC_DEBUG |
| 20 #include <stdio.h> |
| 21 int g_allocCount = 0; |
| 22 int g_allocCountMid = 0; |
| 23 int g_allocCountBig = 0; |
| 24 #endif |
| 25 |
| 26 void *MyAlloc(size_t size) |
| 27 { |
| 28 if (size == 0) |
| 29 return 0; |
| 30 #ifdef _SZ_ALLOC_DEBUG |
| 31 { |
| 32 void *p = malloc(size); |
| 33 fprintf(stderr, "\nAlloc %10d bytes, count = %10d, addr = %8X", size, g_all
ocCount++, (unsigned)p); |
| 34 return p; |
| 35 } |
| 36 #else |
| 37 return malloc(size); |
| 38 #endif |
| 39 } |
| 40 |
| 41 void MyFree(void *address) |
| 42 { |
| 43 #ifdef _SZ_ALLOC_DEBUG |
| 44 if (address != 0) |
| 45 fprintf(stderr, "\nFree; count = %10d, addr = %8X", --g_allocCount, (unsign
ed)address); |
| 46 #endif |
| 47 free(address); |
| 48 } |
| 49 |
| 50 #ifdef _WIN32 |
| 51 |
| 52 void *MidAlloc(size_t size) |
| 53 { |
| 54 if (size == 0) |
| 55 return 0; |
| 56 #ifdef _SZ_ALLOC_DEBUG |
| 57 fprintf(stderr, "\nAlloc_Mid %10d bytes; count = %10d", size, g_allocCountMid
++); |
| 58 #endif |
| 59 return VirtualAlloc(0, size, MEM_COMMIT, PAGE_READWRITE); |
| 60 } |
| 61 |
| 62 void MidFree(void *address) |
| 63 { |
| 64 #ifdef _SZ_ALLOC_DEBUG |
| 65 if (address != 0) |
| 66 fprintf(stderr, "\nFree_Mid; count = %10d", --g_allocCountMid); |
| 67 #endif |
| 68 if (address == 0) |
| 69 return; |
| 70 VirtualFree(address, 0, MEM_RELEASE); |
| 71 } |
| 72 |
| 73 #ifndef MEM_LARGE_PAGES |
| 74 #undef _7ZIP_LARGE_PAGES |
| 75 #endif |
| 76 |
| 77 #ifdef _7ZIP_LARGE_PAGES |
| 78 SIZE_T g_LargePageSize = 0; |
| 79 typedef SIZE_T (WINAPI *GetLargePageMinimumP)(); |
| 80 #endif |
| 81 |
| 82 void SetLargePageSize() |
| 83 { |
| 84 #ifdef _7ZIP_LARGE_PAGES |
| 85 SIZE_T size = 0; |
| 86 GetLargePageMinimumP largePageMinimum = (GetLargePageMinimumP) |
| 87 GetProcAddress(GetModuleHandle(TEXT("kernel32.dll")), "GetLargePageMinim
um"); |
| 88 if (largePageMinimum == 0) |
| 89 return; |
| 90 size = largePageMinimum(); |
| 91 if (size == 0 || (size & (size - 1)) != 0) |
| 92 return; |
| 93 g_LargePageSize = size; |
| 94 #endif |
| 95 } |
| 96 |
| 97 |
| 98 void *BigAlloc(size_t size) |
| 99 { |
| 100 if (size == 0) |
| 101 return 0; |
| 102 #ifdef _SZ_ALLOC_DEBUG |
| 103 fprintf(stderr, "\nAlloc_Big %10d bytes; count = %10d", size, g_allocCountBig
++); |
| 104 #endif |
| 105 |
| 106 #ifdef _7ZIP_LARGE_PAGES |
| 107 if (g_LargePageSize != 0 && g_LargePageSize <= (1 << 30) && size >= (1 << 18)) |
| 108 { |
| 109 void *res = VirtualAlloc(0, (size + g_LargePageSize - 1) & (~(g_LargePageSiz
e - 1)), |
| 110 MEM_COMMIT | MEM_LARGE_PAGES, PAGE_READWRITE); |
| 111 if (res != 0) |
| 112 return res; |
| 113 } |
| 114 #endif |
| 115 return VirtualAlloc(0, size, MEM_COMMIT, PAGE_READWRITE); |
| 116 } |
| 117 |
| 118 void BigFree(void *address) |
| 119 { |
| 120 #ifdef _SZ_ALLOC_DEBUG |
| 121 if (address != 0) |
| 122 fprintf(stderr, "\nFree_Big; count = %10d", --g_allocCountBig); |
| 123 #endif |
| 124 |
| 125 if (address == 0) |
| 126 return; |
| 127 VirtualFree(address, 0, MEM_RELEASE); |
| 128 } |
| 129 |
| 130 #endif |
| 131 |
| 132 } // namespace lzma |
| 133 } // namespace ots |
OLD | NEW |