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