1/* 7zAlloc.c -- Allocation functions for 7z processing 22023-03-04 : Igor Pavlov : Public domain */ 3 4#include "Precomp.h" 5 6#include <stdlib.h> 7 8#include "7zAlloc.h" 9 10/* #define SZ_ALLOC_DEBUG */ 11/* use SZ_ALLOC_DEBUG to debug alloc/free operations */ 12 13#ifdef SZ_ALLOC_DEBUG 14 15/* 16#ifdef _WIN32 17#include "7zWindows.h" 18#endif 19*/ 20 21#include <stdio.h> 22static int g_allocCount = 0; 23static int g_allocCountTemp = 0; 24 25static void Print_Alloc(const char *s, size_t size, int *counter) 26{ 27 const unsigned size2 = (unsigned)size; 28 fprintf(stderr, "\n%s count = %10d : %10u bytes; ", s, *counter, size2); 29 (*counter)++; 30} 31static void Print_Free(const char *s, int *counter) 32{ 33 (*counter)--; 34 fprintf(stderr, "\n%s count = %10d", s, *counter); 35} 36#endif 37 38void *SzAlloc(ISzAllocPtr p, size_t size) 39{ 40 UNUSED_VAR(p) 41 if (size == 0) 42 return 0; 43 #ifdef SZ_ALLOC_DEBUG 44 Print_Alloc("Alloc", size, &g_allocCount); 45 #endif 46 return malloc(size); 47} 48 49void SzFree(ISzAllocPtr p, void *address) 50{ 51 UNUSED_VAR(p) 52 #ifdef SZ_ALLOC_DEBUG 53 if (address) 54 Print_Free("Free ", &g_allocCount); 55 #endif 56 free(address); 57} 58 59void *SzAllocTemp(ISzAllocPtr p, size_t size) 60{ 61 UNUSED_VAR(p) 62 if (size == 0) 63 return 0; 64 #ifdef SZ_ALLOC_DEBUG 65 Print_Alloc("Alloc_temp", size, &g_allocCountTemp); 66 /* 67 #ifdef _WIN32 68 return HeapAlloc(GetProcessHeap(), 0, size); 69 #endif 70 */ 71 #endif 72 return malloc(size); 73} 74 75void SzFreeTemp(ISzAllocPtr p, void *address) 76{ 77 UNUSED_VAR(p) 78 #ifdef SZ_ALLOC_DEBUG 79 if (address) 80 Print_Free("Free_temp ", &g_allocCountTemp); 81 /* 82 #ifdef _WIN32 83 HeapFree(GetProcessHeap(), 0, address); 84 return; 85 #endif 86 */ 87 #endif 88 free(address); 89} 90