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

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/other-licenses/7zstub/src/Common/Alloc.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,118 @@
     1.4 +// Common/Alloc.cpp
     1.5 +
     1.6 +#include "StdAfx.h"
     1.7 +
     1.8 +#ifdef _WIN32
     1.9 +#include "MyWindows.h"
    1.10 +#else
    1.11 +#include <stdlib.h>
    1.12 +#endif
    1.13 +
    1.14 +#include "Alloc.h"
    1.15 +
    1.16 +/* #define _SZ_ALLOC_DEBUG */
    1.17 +/* use _SZ_ALLOC_DEBUG to debug alloc/free operations */
    1.18 +#ifdef _SZ_ALLOC_DEBUG
    1.19 +#include <stdio.h>
    1.20 +int g_allocCount = 0;
    1.21 +int g_allocCountMid = 0;
    1.22 +int g_allocCountBig = 0;
    1.23 +#endif
    1.24 +
    1.25 +void *MyAlloc(size_t size) throw()
    1.26 +{
    1.27 +  if (size == 0)
    1.28 +    return 0;
    1.29 +  #ifdef _SZ_ALLOC_DEBUG
    1.30 +  fprintf(stderr, "\nAlloc %10d bytes; count = %10d", size, g_allocCount++);
    1.31 +  #endif
    1.32 +  return ::malloc(size);
    1.33 +}
    1.34 +
    1.35 +void MyFree(void *address) throw()
    1.36 +{
    1.37 +  #ifdef _SZ_ALLOC_DEBUG
    1.38 +  if (address != 0)
    1.39 +    fprintf(stderr, "\nFree; count = %10d", --g_allocCount);
    1.40 +  #endif
    1.41 +  
    1.42 +  ::free(address);
    1.43 +}
    1.44 +
    1.45 +#ifdef _WIN32
    1.46 +
    1.47 +void *MidAlloc(size_t size) throw()
    1.48 +{
    1.49 +  if (size == 0)
    1.50 +    return 0;
    1.51 +  #ifdef _SZ_ALLOC_DEBUG
    1.52 +  fprintf(stderr, "\nAlloc_Mid %10d bytes;  count = %10d", size, g_allocCountMid++);
    1.53 +  #endif
    1.54 +  return ::VirtualAlloc(0, size, MEM_COMMIT, PAGE_READWRITE);
    1.55 +}
    1.56 +
    1.57 +void MidFree(void *address) throw()
    1.58 +{
    1.59 +  #ifdef _SZ_ALLOC_DEBUG
    1.60 +  if (address != 0)
    1.61 +    fprintf(stderr, "\nFree_Mid; count = %10d", --g_allocCountMid);
    1.62 +  #endif
    1.63 +  if (address == 0)
    1.64 +    return;
    1.65 +  ::VirtualFree(address, 0, MEM_RELEASE);
    1.66 +}
    1.67 +
    1.68 +static SIZE_T g_LargePageSize = 
    1.69 +    #ifdef _WIN64
    1.70 +    (1 << 21);
    1.71 +    #else
    1.72 +    (1 << 22);
    1.73 +    #endif
    1.74 +
    1.75 +typedef SIZE_T (WINAPI *GetLargePageMinimumP)();
    1.76 +
    1.77 +bool SetLargePageSize()
    1.78 +{
    1.79 +  GetLargePageMinimumP largePageMinimum = (GetLargePageMinimumP)
    1.80 +        ::GetProcAddress(::GetModuleHandle(TEXT("kernel32.dll")), "GetLargePageMinimum");
    1.81 +  if (largePageMinimum == 0)
    1.82 +    return false;
    1.83 +  SIZE_T size = largePageMinimum();
    1.84 +  if (size == 0 || (size & (size - 1)) != 0)
    1.85 +    return false;
    1.86 +  g_LargePageSize = size;
    1.87 +  return true;
    1.88 +}
    1.89 +
    1.90 +
    1.91 +void *BigAlloc(size_t size) throw()
    1.92 +{
    1.93 +  if (size == 0)
    1.94 +    return 0;
    1.95 +  #ifdef _SZ_ALLOC_DEBUG
    1.96 +  fprintf(stderr, "\nAlloc_Big %10d bytes;  count = %10d", size, g_allocCountBig++);
    1.97 +  #endif
    1.98 +  
    1.99 +  if (size >= (1 << 18))
   1.100 +  {
   1.101 +    void *res = ::VirtualAlloc(0, (size + g_LargePageSize - 1) & (~(g_LargePageSize - 1)), 
   1.102 +        MEM_COMMIT | MEM_LARGE_PAGES, PAGE_READWRITE);
   1.103 +    if (res != 0)
   1.104 +      return res;
   1.105 +  }
   1.106 +  return ::VirtualAlloc(0, size, MEM_COMMIT, PAGE_READWRITE);
   1.107 +}
   1.108 +
   1.109 +void BigFree(void *address) throw()
   1.110 +{
   1.111 +  #ifdef _SZ_ALLOC_DEBUG
   1.112 +  if (address != 0)
   1.113 +    fprintf(stderr, "\nFree_Big; count = %10d", --g_allocCountBig);
   1.114 +  #endif
   1.115 +  
   1.116 +  if (address == 0)
   1.117 +    return;
   1.118 +  ::VirtualFree(address, 0, MEM_RELEASE);
   1.119 +}
   1.120 +
   1.121 +#endif

mercurial