CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_5_3_13_patch3/src/GeneratorInterface/ReggeGribovPartonMCInterface/src/Alloc.cc

Go to the documentation of this file.
00001 /* Alloc.c -- Memory allocation functions
00002 2008-09-24
00003 Igor Pavlov
00004 Public domain */
00005 
00006 #ifdef _WIN32
00007 #include <windows.h>
00008 #endif
00009 #include <stdlib.h>
00010 
00011 #include "Alloc.h"
00012 
00013 /* #define _SZ_ALLOC_DEBUG */
00014 
00015 /* use _SZ_ALLOC_DEBUG to debug alloc/free operations */
00016 #ifdef _SZ_ALLOC_DEBUG
00017 #include <stdio.h>
00018 int g_allocCount = 0;
00019 int g_allocCountMid = 0;
00020 int g_allocCountBig = 0;
00021 #endif
00022 
00023 void *MyAlloc(size_t size)
00024 {
00025   if (size == 0)
00026     return 0;
00027   #ifdef _SZ_ALLOC_DEBUG
00028   {
00029     void *p = malloc(size);
00030     fprintf(stderr, "\nAlloc %10d bytes, count = %10d,  addr = %8X", size, g_allocCount++, (unsigned)p);
00031     return p;
00032   }
00033   #else
00034   return malloc(size);
00035   #endif
00036 }
00037 
00038 void MyFree(void *address)
00039 {
00040   #ifdef _SZ_ALLOC_DEBUG
00041   if (address != 0)
00042     fprintf(stderr, "\nFree; count = %10d,  addr = %8X", --g_allocCount, (unsigned)address);
00043   #endif
00044   free(address);
00045 }
00046 
00047 #ifdef _WIN32
00048 
00049 void *MidAlloc(size_t size)
00050 {
00051   if (size == 0)
00052     return 0;
00053   #ifdef _SZ_ALLOC_DEBUG
00054   fprintf(stderr, "\nAlloc_Mid %10d bytes;  count = %10d", size, g_allocCountMid++);
00055   #endif
00056   return VirtualAlloc(0, size, MEM_COMMIT, PAGE_READWRITE);
00057 }
00058 
00059 void MidFree(void *address)
00060 {
00061   #ifdef _SZ_ALLOC_DEBUG
00062   if (address != 0)
00063     fprintf(stderr, "\nFree_Mid; count = %10d", --g_allocCountMid);
00064   #endif
00065   if (address == 0)
00066     return;
00067   VirtualFree(address, 0, MEM_RELEASE);
00068 }
00069 
00070 #ifndef MEM_LARGE_PAGES
00071 #undef _7ZIP_LARGE_PAGES
00072 #endif
00073 
00074 #ifdef _7ZIP_LARGE_PAGES
00075 SIZE_T g_LargePageSize = 0;
00076 typedef SIZE_T (WINAPI *GetLargePageMinimumP)();
00077 #endif
00078 
00079 void SetLargePageSize()
00080 {
00081   #ifdef _7ZIP_LARGE_PAGES
00082   SIZE_T size = 0;
00083   GetLargePageMinimumP largePageMinimum = (GetLargePageMinimumP)
00084         GetProcAddress(GetModuleHandle(TEXT("kernel32.dll")), "GetLargePageMinimum");
00085   if (largePageMinimum == 0)
00086     return;
00087   size = largePageMinimum();
00088   if (size == 0 || (size & (size - 1)) != 0)
00089     return;
00090   g_LargePageSize = size;
00091   #endif
00092 }
00093 
00094 
00095 void *BigAlloc(size_t size)
00096 {
00097   if (size == 0)
00098     return 0;
00099   #ifdef _SZ_ALLOC_DEBUG
00100   fprintf(stderr, "\nAlloc_Big %10d bytes;  count = %10d", size, g_allocCountBig++);
00101   #endif
00102   
00103   #ifdef _7ZIP_LARGE_PAGES
00104   if (g_LargePageSize != 0 && g_LargePageSize <= (1 << 30) && size >= (1 << 18))
00105   {
00106     void *res = VirtualAlloc(0, (size + g_LargePageSize - 1) & (~(g_LargePageSize - 1)),
00107         MEM_COMMIT | MEM_LARGE_PAGES, PAGE_READWRITE);
00108     if (res != 0)
00109       return res;
00110   }
00111   #endif
00112   return VirtualAlloc(0, size, MEM_COMMIT, PAGE_READWRITE);
00113 }
00114 
00115 void BigFree(void *address)
00116 {
00117   #ifdef _SZ_ALLOC_DEBUG
00118   if (address != 0)
00119     fprintf(stderr, "\nFree_Big; count = %10d", --g_allocCountBig);
00120   #endif
00121   
00122   if (address == 0)
00123     return;
00124   VirtualFree(address, 0, MEM_RELEASE);
00125 }
00126 
00127 #endif