annotate src/win32/7zip/7z/C/Alloc.c @ 2:3549bbe597ed

adding makefile.am
author Robert McIntyre <rlm@mit.edu>
date Sat, 03 Mar 2012 10:31:47 -0600
parents f9f4f1b99eed
children
rev   line source
rlm@1 1 /* Alloc.c -- Memory allocation functions
rlm@1 2 2008-09-24
rlm@1 3 Igor Pavlov
rlm@1 4 Public domain */
rlm@1 5
rlm@1 6 #ifdef _WIN32
rlm@1 7 #include <windows.h>
rlm@1 8 #endif
rlm@1 9 #include <stdlib.h>
rlm@1 10
rlm@1 11 #include "Alloc.h"
rlm@1 12
rlm@1 13 /* #define _SZ_ALLOC_DEBUG */
rlm@1 14
rlm@1 15 /* use _SZ_ALLOC_DEBUG to debug alloc/free operations */
rlm@1 16 #ifdef _SZ_ALLOC_DEBUG
rlm@1 17 #include <stdio.h>
rlm@1 18 int g_allocCount = 0;
rlm@1 19 int g_allocCountMid = 0;
rlm@1 20 int g_allocCountBig = 0;
rlm@1 21 #endif
rlm@1 22
rlm@1 23 void *MyAlloc(size_t size)
rlm@1 24 {
rlm@1 25 if (size == 0)
rlm@1 26 return 0;
rlm@1 27 #ifdef _SZ_ALLOC_DEBUG
rlm@1 28 {
rlm@1 29 void *p = malloc(size);
rlm@1 30 fprintf(stderr, "\nAlloc %10d bytes, count = %10d, addr = %8X", size, g_allocCount++, (unsigned)p);
rlm@1 31 return p;
rlm@1 32 }
rlm@1 33 #else
rlm@1 34 return malloc(size);
rlm@1 35 #endif
rlm@1 36 }
rlm@1 37
rlm@1 38 void MyFree(void *address)
rlm@1 39 {
rlm@1 40 #ifdef _SZ_ALLOC_DEBUG
rlm@1 41 if (address != 0)
rlm@1 42 fprintf(stderr, "\nFree; count = %10d, addr = %8X", --g_allocCount, (unsigned)address);
rlm@1 43 #endif
rlm@1 44 free(address);
rlm@1 45 }
rlm@1 46
rlm@1 47 #ifdef _WIN32
rlm@1 48
rlm@1 49 void *MidAlloc(size_t size)
rlm@1 50 {
rlm@1 51 if (size == 0)
rlm@1 52 return 0;
rlm@1 53 #ifdef _SZ_ALLOC_DEBUG
rlm@1 54 fprintf(stderr, "\nAlloc_Mid %10d bytes; count = %10d", size, g_allocCountMid++);
rlm@1 55 #endif
rlm@1 56 return VirtualAlloc(0, size, MEM_COMMIT, PAGE_READWRITE);
rlm@1 57 }
rlm@1 58
rlm@1 59 void MidFree(void *address)
rlm@1 60 {
rlm@1 61 #ifdef _SZ_ALLOC_DEBUG
rlm@1 62 if (address != 0)
rlm@1 63 fprintf(stderr, "\nFree_Mid; count = %10d", --g_allocCountMid);
rlm@1 64 #endif
rlm@1 65 if (address == 0)
rlm@1 66 return;
rlm@1 67 VirtualFree(address, 0, MEM_RELEASE);
rlm@1 68 }
rlm@1 69
rlm@1 70 #ifndef MEM_LARGE_PAGES
rlm@1 71 #undef _7ZIP_LARGE_PAGES
rlm@1 72 #endif
rlm@1 73
rlm@1 74 #ifdef _7ZIP_LARGE_PAGES
rlm@1 75 SIZE_T g_LargePageSize = 0;
rlm@1 76 typedef SIZE_T (WINAPI *GetLargePageMinimumP)();
rlm@1 77 #endif
rlm@1 78
rlm@1 79 void SetLargePageSize()
rlm@1 80 {
rlm@1 81 #ifdef _7ZIP_LARGE_PAGES
rlm@1 82 SIZE_T size = 0;
rlm@1 83 GetLargePageMinimumP largePageMinimum = (GetLargePageMinimumP)
rlm@1 84 GetProcAddress(GetModuleHandle(TEXT("kernel32.dll")), "GetLargePageMinimum");
rlm@1 85 if (largePageMinimum == 0)
rlm@1 86 return;
rlm@1 87 size = largePageMinimum();
rlm@1 88 if (size == 0 || (size & (size - 1)) != 0)
rlm@1 89 return;
rlm@1 90 g_LargePageSize = size;
rlm@1 91 #endif
rlm@1 92 }
rlm@1 93
rlm@1 94
rlm@1 95 void *BigAlloc(size_t size)
rlm@1 96 {
rlm@1 97 if (size == 0)
rlm@1 98 return 0;
rlm@1 99 #ifdef _SZ_ALLOC_DEBUG
rlm@1 100 fprintf(stderr, "\nAlloc_Big %10d bytes; count = %10d", size, g_allocCountBig++);
rlm@1 101 #endif
rlm@1 102
rlm@1 103 #ifdef _7ZIP_LARGE_PAGES
rlm@1 104 if (g_LargePageSize != 0 && g_LargePageSize <= (1 << 30) && size >= (1 << 18))
rlm@1 105 {
rlm@1 106 void *res = VirtualAlloc(0, (size + g_LargePageSize - 1) & (~(g_LargePageSize - 1)),
rlm@1 107 MEM_COMMIT | MEM_LARGE_PAGES, PAGE_READWRITE);
rlm@1 108 if (res != 0)
rlm@1 109 return res;
rlm@1 110 }
rlm@1 111 #endif
rlm@1 112 return VirtualAlloc(0, size, MEM_COMMIT, PAGE_READWRITE);
rlm@1 113 }
rlm@1 114
rlm@1 115 void BigFree(void *address)
rlm@1 116 {
rlm@1 117 #ifdef _SZ_ALLOC_DEBUG
rlm@1 118 if (address != 0)
rlm@1 119 fprintf(stderr, "\nFree_Big; count = %10d", --g_allocCountBig);
rlm@1 120 #endif
rlm@1 121
rlm@1 122 if (address == 0)
rlm@1 123 return;
rlm@1 124 VirtualFree(address, 0, MEM_RELEASE);
rlm@1 125 }
rlm@1 126
rlm@1 127 #endif