other-licenses/7zstub/src/Common/Alloc.cpp

Fri, 16 Jan 2015 18:13:44 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Fri, 16 Jan 2015 18:13:44 +0100
branch
TOR_BUG_9701
changeset 14
925c144e1f1f
permissions
-rw-r--r--

Integrate suggestion from review to improve consistency with existing code.

michael@0 1 // Common/Alloc.cpp
michael@0 2
michael@0 3 #include "StdAfx.h"
michael@0 4
michael@0 5 #ifdef _WIN32
michael@0 6 #include "MyWindows.h"
michael@0 7 #else
michael@0 8 #include <stdlib.h>
michael@0 9 #endif
michael@0 10
michael@0 11 #include "Alloc.h"
michael@0 12
michael@0 13 /* #define _SZ_ALLOC_DEBUG */
michael@0 14 /* use _SZ_ALLOC_DEBUG to debug alloc/free operations */
michael@0 15 #ifdef _SZ_ALLOC_DEBUG
michael@0 16 #include <stdio.h>
michael@0 17 int g_allocCount = 0;
michael@0 18 int g_allocCountMid = 0;
michael@0 19 int g_allocCountBig = 0;
michael@0 20 #endif
michael@0 21
michael@0 22 void *MyAlloc(size_t size) throw()
michael@0 23 {
michael@0 24 if (size == 0)
michael@0 25 return 0;
michael@0 26 #ifdef _SZ_ALLOC_DEBUG
michael@0 27 fprintf(stderr, "\nAlloc %10d bytes; count = %10d", size, g_allocCount++);
michael@0 28 #endif
michael@0 29 return ::malloc(size);
michael@0 30 }
michael@0 31
michael@0 32 void MyFree(void *address) throw()
michael@0 33 {
michael@0 34 #ifdef _SZ_ALLOC_DEBUG
michael@0 35 if (address != 0)
michael@0 36 fprintf(stderr, "\nFree; count = %10d", --g_allocCount);
michael@0 37 #endif
michael@0 38
michael@0 39 ::free(address);
michael@0 40 }
michael@0 41
michael@0 42 #ifdef _WIN32
michael@0 43
michael@0 44 void *MidAlloc(size_t size) throw()
michael@0 45 {
michael@0 46 if (size == 0)
michael@0 47 return 0;
michael@0 48 #ifdef _SZ_ALLOC_DEBUG
michael@0 49 fprintf(stderr, "\nAlloc_Mid %10d bytes; count = %10d", size, g_allocCountMid++);
michael@0 50 #endif
michael@0 51 return ::VirtualAlloc(0, size, MEM_COMMIT, PAGE_READWRITE);
michael@0 52 }
michael@0 53
michael@0 54 void MidFree(void *address) throw()
michael@0 55 {
michael@0 56 #ifdef _SZ_ALLOC_DEBUG
michael@0 57 if (address != 0)
michael@0 58 fprintf(stderr, "\nFree_Mid; count = %10d", --g_allocCountMid);
michael@0 59 #endif
michael@0 60 if (address == 0)
michael@0 61 return;
michael@0 62 ::VirtualFree(address, 0, MEM_RELEASE);
michael@0 63 }
michael@0 64
michael@0 65 static SIZE_T g_LargePageSize =
michael@0 66 #ifdef _WIN64
michael@0 67 (1 << 21);
michael@0 68 #else
michael@0 69 (1 << 22);
michael@0 70 #endif
michael@0 71
michael@0 72 typedef SIZE_T (WINAPI *GetLargePageMinimumP)();
michael@0 73
michael@0 74 bool SetLargePageSize()
michael@0 75 {
michael@0 76 GetLargePageMinimumP largePageMinimum = (GetLargePageMinimumP)
michael@0 77 ::GetProcAddress(::GetModuleHandle(TEXT("kernel32.dll")), "GetLargePageMinimum");
michael@0 78 if (largePageMinimum == 0)
michael@0 79 return false;
michael@0 80 SIZE_T size = largePageMinimum();
michael@0 81 if (size == 0 || (size & (size - 1)) != 0)
michael@0 82 return false;
michael@0 83 g_LargePageSize = size;
michael@0 84 return true;
michael@0 85 }
michael@0 86
michael@0 87
michael@0 88 void *BigAlloc(size_t size) throw()
michael@0 89 {
michael@0 90 if (size == 0)
michael@0 91 return 0;
michael@0 92 #ifdef _SZ_ALLOC_DEBUG
michael@0 93 fprintf(stderr, "\nAlloc_Big %10d bytes; count = %10d", size, g_allocCountBig++);
michael@0 94 #endif
michael@0 95
michael@0 96 if (size >= (1 << 18))
michael@0 97 {
michael@0 98 void *res = ::VirtualAlloc(0, (size + g_LargePageSize - 1) & (~(g_LargePageSize - 1)),
michael@0 99 MEM_COMMIT | MEM_LARGE_PAGES, PAGE_READWRITE);
michael@0 100 if (res != 0)
michael@0 101 return res;
michael@0 102 }
michael@0 103 return ::VirtualAlloc(0, size, MEM_COMMIT, PAGE_READWRITE);
michael@0 104 }
michael@0 105
michael@0 106 void BigFree(void *address) throw()
michael@0 107 {
michael@0 108 #ifdef _SZ_ALLOC_DEBUG
michael@0 109 if (address != 0)
michael@0 110 fprintf(stderr, "\nFree_Big; count = %10d", --g_allocCountBig);
michael@0 111 #endif
michael@0 112
michael@0 113 if (address == 0)
michael@0 114 return;
michael@0 115 ::VirtualFree(address, 0, MEM_RELEASE);
michael@0 116 }
michael@0 117
michael@0 118 #endif

mercurial