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