| 1 |
/* |
| 2 |
* Alloc.cpp |
| 3 |
* |
| 4 |
* This file is a part of LZMA compression module for NSIS. |
| 5 |
* |
| 6 |
* Original LZMA SDK Copyright (C) 1999-2006 Igor Pavlov |
| 7 |
* Modifications Copyright (C) 2003-2026 Amir Szekely <[email protected]> |
| 8 |
* |
| 9 |
* Licensed under the Common Public License version 1.0 (the "License"); |
| 10 |
* you may not use this file except in compliance with the License. |
| 11 |
* |
| 12 |
* Licence details can be found in the file COPYING. |
| 13 |
* |
| 14 |
* This software is provided 'as-is', without any express or implied |
| 15 |
* warranty. |
| 16 |
* |
| 17 |
* Unicode support by Jim Park -- 08/28/2007 |
| 18 |
*/ |
| 19 |
|
| 20 |
#include "StdAfx.h" |
| 21 |
|
| 22 |
#ifdef _WIN32 |
| 23 |
#include "MyWindows.h" |
| 24 |
#else |
| 25 |
#include <stdlib.h> |
| 26 |
#include "../../tchar.h" |
| 27 |
#endif |
| 28 |
|
| 29 |
#include "Alloc.h" |
| 30 |
|
| 31 |
/* #define _SZ_ALLOC_DEBUG */ |
| 32 |
/* use _SZ_ALLOC_DEBUG to debug alloc/free operations */ |
| 33 |
#ifdef _SZ_ALLOC_DEBUG |
| 34 |
#include <stdio.h> |
| 35 |
int g_allocCount = 0; |
| 36 |
int g_allocCountMid = 0; |
| 37 |
int g_allocCountBig = 0; |
| 38 |
#endif |
| 39 |
|
| 40 |
void *MyAlloc(size_t size) throw() |
| 41 |
{ |
| 42 |
if (size == 0) |
| 43 |
return 0; |
| 44 |
#ifdef _SZ_ALLOC_DEBUG |
| 45 |
_ftprintf(stderr, _T("\nAlloc %10d bytes; count = %10d"), size, g_allocCount++); |
| 46 |
#endif |
| 47 |
return ::malloc(size); |
| 48 |
} |
| 49 |
|
| 50 |
void MyFree(void *address) throw() |
| 51 |
{ |
| 52 |
#ifdef _SZ_ALLOC_DEBUG |
| 53 |
if (address != 0) |
| 54 |
_ftprintf(stderr, _T("\nFree; count = %10d"), --g_allocCount); |
| 55 |
#endif |
| 56 |
|
| 57 |
::free(address); |
| 58 |
} |
| 59 |
|
| 60 |
#ifdef _WIN32 |
| 61 |
|
| 62 |
void *MidAlloc(size_t size) throw() |
| 63 |
{ |
| 64 |
if (size == 0) |
| 65 |
return 0; |
| 66 |
#ifdef _SZ_ALLOC_DEBUG |
| 67 |
_ftprintf(stderr, _T("\nAlloc_Mid %10d bytes; count = %10d"), size, g_allocCountMid++); |
| 68 |
#endif |
| 69 |
return ::VirtualAlloc(0, size, MEM_COMMIT, PAGE_READWRITE); |
| 70 |
} |
| 71 |
|
| 72 |
void MidFree(void *address) throw() |
| 73 |
{ |
| 74 |
#ifdef _SZ_ALLOC_DEBUG |
| 75 |
if (address != 0) |
| 76 |
_ftprintf(stderr, _T("\nFree_Mid; count = %10d"), --g_allocCountMid); |
| 77 |
#endif |
| 78 |
if (address == 0) |
| 79 |
return; |
| 80 |
::VirtualFree(address, 0, MEM_RELEASE); |
| 81 |
} |
| 82 |
|
| 83 |
static SIZE_T g_LargePageSize = |
| 84 |
#ifdef _WIN64 |
| 85 |
(1 << 21); |
| 86 |
#else |
| 87 |
(1 << 22); |
| 88 |
#endif |
| 89 |
|
| 90 |
typedef SIZE_T (WINAPI *GetLargePageMinimumP)(); |
| 91 |
|
| 92 |
bool SetLargePageSize() |
| 93 |
{ |
| 94 |
GetLargePageMinimumP largePageMinimum = (GetLargePageMinimumP) |
| 95 |
::GetProcAddress(::GetModuleHandle(_T("kernel32.dll")), "GetLargePageMinimum"); |
| 96 |
if (largePageMinimum == 0) |
| 97 |
return false; |
| 98 |
SIZE_T size = largePageMinimum(); |
| 99 |
if (size == 0 || (size & (size - 1)) != 0) |
| 100 |
return false; |
| 101 |
g_LargePageSize = size; |
| 102 |
return true; |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
void *BigAlloc(size_t size) throw() |
| 107 |
{ |
| 108 |
if (size == 0) |
| 109 |
return 0; |
| 110 |
#ifdef _SZ_ALLOC_DEBUG |
| 111 |
_ftprintf(stderr, _T("\nAlloc_Big %10d bytes; count = %10d"), size, g_allocCountBig++); |
| 112 |
#endif |
| 113 |
|
| 114 |
if (size >= (1 << 18)) |
| 115 |
{ |
| 116 |
void *res = ::VirtualAlloc(0, (size + g_LargePageSize - 1) & (~(g_LargePageSize - 1)), |
| 117 |
MEM_COMMIT | MEM_LARGE_PAGES, PAGE_READWRITE); |
| 118 |
if (res != 0) |
| 119 |
return res; |
| 120 |
} |
| 121 |
return ::VirtualAlloc(0, size, MEM_COMMIT, PAGE_READWRITE); |
| 122 |
} |
| 123 |
|
| 124 |
void BigFree(void *address) throw() |
| 125 |
{ |
| 126 |
#ifdef _SZ_ALLOC_DEBUG |
| 127 |
if (address != 0) |
| 128 |
_ftprintf(stderr, _T("\nFree_Big; count = %10d"), --g_allocCountBig); |
| 129 |
#endif |
| 130 |
|
| 131 |
if (address == 0) |
| 132 |
return; |
| 133 |
::VirtualFree(address, 0, MEM_RELEASE); |
| 134 |
} |
| 135 |
|
| 136 |
#endif |