1 /* 7zAlloc.c -- Allocation functions
2 2017-04-03 : 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 #ifdef _WIN32
16 #include <windows.h>
17 #endif
18 
19 #include <stdio.h>
20 int g_allocCount = 0;
21 int g_allocCountTemp = 0;
22 
23 #endif
24 
SzAlloc(ISzAllocPtr p,size_t size)25 void *SzAlloc(ISzAllocPtr p, size_t size)
26 {
27   UNUSED_VAR(p);
28   if (size == 0)
29     return 0;
30   #ifdef _SZ_ALLOC_DEBUG
31   fprintf(stderr, "\nAlloc %10u bytes; count = %10d", (unsigned)size, g_allocCount);
32   g_allocCount++;
33   #endif
34   return malloc(size);
35 }
36 
SzFree(ISzAllocPtr p,void * address)37 void SzFree(ISzAllocPtr p, void *address)
38 {
39   UNUSED_VAR(p);
40   #ifdef _SZ_ALLOC_DEBUG
41   if (address != 0)
42   {
43     g_allocCount--;
44     fprintf(stderr, "\nFree; count = %10d", g_allocCount);
45   }
46   #endif
47   free(address);
48 }
49 
SzAllocTemp(ISzAllocPtr p,size_t size)50 void *SzAllocTemp(ISzAllocPtr p, size_t size)
51 {
52   UNUSED_VAR(p);
53   if (size == 0)
54     return 0;
55   #ifdef _SZ_ALLOC_DEBUG
56   fprintf(stderr, "\nAlloc_temp %10u bytes;  count = %10d", (unsigned)size, g_allocCountTemp);
57   g_allocCountTemp++;
58   #ifdef _WIN32
59   return HeapAlloc(GetProcessHeap(), 0, size);
60   #endif
61   #endif
62   return malloc(size);
63 }
64 
SzFreeTemp(ISzAllocPtr p,void * address)65 void SzFreeTemp(ISzAllocPtr p, void *address)
66 {
67   UNUSED_VAR(p);
68   #ifdef _SZ_ALLOC_DEBUG
69   if (address != 0)
70   {
71     g_allocCountTemp--;
72     fprintf(stderr, "\nFree_temp; count = %10d", g_allocCountTemp);
73   }
74   #ifdef _WIN32
75   HeapFree(GetProcessHeap(), 0, address);
76   return;
77   #endif
78   #endif
79   free(address);
80 }
81