Mercurial > vba-linux
view src/win32/7zip/7z/C/Alloc.c @ 1:f9f4f1b99eed
importing src directory
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Sat, 03 Mar 2012 10:31:27 -0600 |
parents | |
children |
line wrap: on
line source
1 /* Alloc.c -- Memory allocation functions2 2008-09-243 Igor Pavlov4 Public domain */6 #ifdef _WIN327 #include <windows.h>8 #endif9 #include <stdlib.h>11 #include "Alloc.h"13 /* #define _SZ_ALLOC_DEBUG */15 /* use _SZ_ALLOC_DEBUG to debug alloc/free operations */16 #ifdef _SZ_ALLOC_DEBUG17 #include <stdio.h>18 int g_allocCount = 0;19 int g_allocCountMid = 0;20 int g_allocCountBig = 0;21 #endif23 void *MyAlloc(size_t size)24 {25 if (size == 0)26 return 0;27 #ifdef _SZ_ALLOC_DEBUG28 {29 void *p = malloc(size);30 fprintf(stderr, "\nAlloc %10d bytes, count = %10d, addr = %8X", size, g_allocCount++, (unsigned)p);31 return p;32 }33 #else34 return malloc(size);35 #endif36 }38 void MyFree(void *address)39 {40 #ifdef _SZ_ALLOC_DEBUG41 if (address != 0)42 fprintf(stderr, "\nFree; count = %10d, addr = %8X", --g_allocCount, (unsigned)address);43 #endif44 free(address);45 }47 #ifdef _WIN3249 void *MidAlloc(size_t size)50 {51 if (size == 0)52 return 0;53 #ifdef _SZ_ALLOC_DEBUG54 fprintf(stderr, "\nAlloc_Mid %10d bytes; count = %10d", size, g_allocCountMid++);55 #endif56 return VirtualAlloc(0, size, MEM_COMMIT, PAGE_READWRITE);57 }59 void MidFree(void *address)60 {61 #ifdef _SZ_ALLOC_DEBUG62 if (address != 0)63 fprintf(stderr, "\nFree_Mid; count = %10d", --g_allocCountMid);64 #endif65 if (address == 0)66 return;67 VirtualFree(address, 0, MEM_RELEASE);68 }70 #ifndef MEM_LARGE_PAGES71 #undef _7ZIP_LARGE_PAGES72 #endif74 #ifdef _7ZIP_LARGE_PAGES75 SIZE_T g_LargePageSize = 0;76 typedef SIZE_T (WINAPI *GetLargePageMinimumP)();77 #endif79 void SetLargePageSize()80 {81 #ifdef _7ZIP_LARGE_PAGES82 SIZE_T size = 0;83 GetLargePageMinimumP largePageMinimum = (GetLargePageMinimumP)84 GetProcAddress(GetModuleHandle(TEXT("kernel32.dll")), "GetLargePageMinimum");85 if (largePageMinimum == 0)86 return;87 size = largePageMinimum();88 if (size == 0 || (size & (size - 1)) != 0)89 return;90 g_LargePageSize = size;91 #endif92 }95 void *BigAlloc(size_t size)96 {97 if (size == 0)98 return 0;99 #ifdef _SZ_ALLOC_DEBUG100 fprintf(stderr, "\nAlloc_Big %10d bytes; count = %10d", size, g_allocCountBig++);101 #endif103 #ifdef _7ZIP_LARGE_PAGES104 if (g_LargePageSize != 0 && g_LargePageSize <= (1 << 30) && size >= (1 << 18))105 {106 void *res = VirtualAlloc(0, (size + g_LargePageSize - 1) & (~(g_LargePageSize - 1)),107 MEM_COMMIT | MEM_LARGE_PAGES, PAGE_READWRITE);108 if (res != 0)109 return res;110 }111 #endif112 return VirtualAlloc(0, size, MEM_COMMIT, PAGE_READWRITE);113 }115 void BigFree(void *address)116 {117 #ifdef _SZ_ALLOC_DEBUG118 if (address != 0)119 fprintf(stderr, "\nFree_Big; count = %10d", --g_allocCountBig);120 #endif122 if (address == 0)123 return;124 VirtualFree(address, 0, MEM_RELEASE);125 }127 #endif