Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:14:00

0001 /* Alloc.c -- Memory allocation functions

0002 2008-09-24

0003 Igor Pavlov

0004 Public domain */
0005 
0006 #ifdef _WIN32
0007 #include <windows.h>
0008 #endif
0009 #include <cstdlib>
0010 
0011 #include "Alloc.h"
0012 
0013 /* #define _SZ_ALLOC_DEBUG */
0014 
0015 /* use _SZ_ALLOC_DEBUG to debug alloc/free operations */
0016 #ifdef _SZ_ALLOC_DEBUG
0017 #include <stdio.h>
0018 int g_allocCount = 0;
0019 int g_allocCountMid = 0;
0020 int g_allocCountBig = 0;
0021 #endif
0022 
0023 void *MyAlloc(size_t size) {
0024   if (size == 0)
0025     return nullptr;
0026 #ifdef _SZ_ALLOC_DEBUG
0027   {
0028     void *p = malloc(size);
0029     fprintf(stderr, "\nAlloc %10d bytes, count = %10d,  addr = %8X", size, g_allocCount++, (unsigned)p);
0030     return p;
0031   }
0032 #else
0033   return malloc(size);
0034 #endif
0035 }
0036 
0037 void MyFree(void *address) {
0038 #ifdef _SZ_ALLOC_DEBUG
0039   if (address != 0)
0040     fprintf(stderr, "\nFree; count = %10d,  addr = %8X", --g_allocCount, (unsigned)address);
0041 #endif
0042   free(address);
0043 }
0044 
0045 #ifdef _WIN32
0046 
0047 void *MidAlloc(size_t size) {
0048   if (size == 0)
0049     return 0;
0050 #ifdef _SZ_ALLOC_DEBUG
0051   fprintf(stderr, "\nAlloc_Mid %10d bytes;  count = %10d", size, g_allocCountMid++);
0052 #endif
0053   return VirtualAlloc(0, size, MEM_COMMIT, PAGE_READWRITE);
0054 }
0055 
0056 void MidFree(void *address) {
0057 #ifdef _SZ_ALLOC_DEBUG
0058   if (address != 0)
0059     fprintf(stderr, "\nFree_Mid; count = %10d", --g_allocCountMid);
0060 #endif
0061   if (address == 0)
0062     return;
0063   VirtualFree(address, 0, MEM_RELEASE);
0064 }
0065 
0066 #ifndef MEM_LARGE_PAGES
0067 #undef _7ZIP_LARGE_PAGES
0068 #endif
0069 
0070 #ifdef _7ZIP_LARGE_PAGES
0071 SIZE_T g_LargePageSize = 0;
0072 typedef SIZE_T(WINAPI *GetLargePageMinimumP)();
0073 #endif
0074 
0075 void SetLargePageSize() {
0076 #ifdef _7ZIP_LARGE_PAGES
0077   SIZE_T size = 0;
0078   GetLargePageMinimumP largePageMinimum =
0079       (GetLargePageMinimumP)GetProcAddress(GetModuleHandle(TEXT("kernel32.dll")), "GetLargePageMinimum");
0080   if (largePageMinimum == 0)
0081     return;
0082   size = largePageMinimum();
0083   if (size == 0 || (size & (size - 1)) != 0)
0084     return;
0085   g_LargePageSize = size;
0086 #endif
0087 }
0088 
0089 void *BigAlloc(size_t size) {
0090   if (size == 0)
0091     return 0;
0092 #ifdef _SZ_ALLOC_DEBUG
0093   fprintf(stderr, "\nAlloc_Big %10d bytes;  count = %10d", size, g_allocCountBig++);
0094 #endif
0095 
0096 #ifdef _7ZIP_LARGE_PAGES
0097   if (g_LargePageSize != 0 && g_LargePageSize <= (1 << 30) && size >= (1 << 18)) {
0098     void *res = VirtualAlloc(
0099         0, (size + g_LargePageSize - 1) & (~(g_LargePageSize - 1)), MEM_COMMIT | MEM_LARGE_PAGES, PAGE_READWRITE);
0100     if (res != 0)
0101       return res;
0102   }
0103 #endif
0104   return VirtualAlloc(0, size, MEM_COMMIT, PAGE_READWRITE);
0105 }
0106 
0107 void BigFree(void *address) {
0108 #ifdef _SZ_ALLOC_DEBUG
0109   if (address != 0)
0110     fprintf(stderr, "\nFree_Big; count = %10d", --g_allocCountBig);
0111 #endif
0112 
0113   if (address == 0)
0114     return;
0115   VirtualFree(address, 0, MEM_RELEASE);
0116 }
0117 
0118 #endif