| 1 |
// Common/Alloc.cpp |
| 2 |
|
| 3 |
#include "StdAfx.h" |
| 4 |
|
| 5 |
#ifdef _WIN32 |
| 6 |
#include "MyWindows.h" |
| 7 |
#else |
| 8 |
#include <stdlib.h> |
| 9 |
#endif |
| 10 |
|
| 11 |
#include "Alloc.h" |
| 12 |
|
| 13 |
/* #define _SZ_ALLOC_DEBUG */ |
| 14 |
/* use _SZ_ALLOC_DEBUG to debug alloc/free operations */ |
| 15 |
#ifdef _SZ_ALLOC_DEBUG |
| 16 |
#include <stdio.h> |
| 17 |
int g_allocCount = 0; |
| 18 |
int g_allocCountBig = 0; |
| 19 |
#endif |
| 20 |
|
| 21 |
void *MyAlloc(size_t size) throw() |
| 22 |
{ |
| 23 |
if (size == 0) |
| 24 |
return 0; |
| 25 |
#ifdef _SZ_ALLOC_DEBUG |
| 26 |
fprintf(stderr, "\nAlloc %10d bytes; count = %10d", size, g_allocCount++); |
| 27 |
#endif |
| 28 |
return ::malloc(size); |
| 29 |
} |
| 30 |
|
| 31 |
void MyFree(void *address) throw() |
| 32 |
{ |
| 33 |
#ifdef _SZ_ALLOC_DEBUG |
| 34 |
if (address != 0) |
| 35 |
fprintf(stderr, "\nFree; count = %10d", --g_allocCount); |
| 36 |
#endif |
| 37 |
|
| 38 |
::free(address); |
| 39 |
} |
| 40 |
|
| 41 |
void *BigAlloc(size_t size) throw() |
| 42 |
{ |
| 43 |
if (size == 0) |
| 44 |
return 0; |
| 45 |
#ifdef _SZ_ALLOC_DEBUG |
| 46 |
fprintf(stderr, "\nAlloc_Big %10d bytes; count = %10d", size, g_allocCountBig++); |
| 47 |
#endif |
| 48 |
|
| 49 |
#ifdef _WIN32 |
| 50 |
return ::VirtualAlloc(0, size, MEM_COMMIT, PAGE_READWRITE); |
| 51 |
#else |
| 52 |
return ::malloc(size); |
| 53 |
#endif |
| 54 |
} |
| 55 |
|
| 56 |
void BigFree(void *address) throw() |
| 57 |
{ |
| 58 |
#ifdef _SZ_ALLOC_DEBUG |
| 59 |
if (address != 0) |
| 60 |
fprintf(stderr, "\nFree_Big; count = %10d", --g_allocCountBig); |
| 61 |
#endif |
| 62 |
|
| 63 |
if (address == 0) |
| 64 |
return; |
| 65 |
#ifdef _WIN32 |
| 66 |
::VirtualFree(address, 0, MEM_RELEASE); |
| 67 |
#else |
| 68 |
::free(address); |
| 69 |
#endif |
| 70 |
} |
| 71 |
|
| 72 |
/* |
| 73 |
void *BigAllocE(size_t size) |
| 74 |
{ |
| 75 |
void *res = BigAlloc(size); |
| 76 |
#ifndef _NO_EXCEPTIONS |
| 77 |
if (res == 0) |
| 78 |
throw CNewException(); |
| 79 |
#endif |
| 80 |
return res; |
| 81 |
} |
| 82 |
*/ |