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.

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

mercurial