1 /* 7zAlloc.c -- Allocation functions
2 2010-10-29 : Igor Pavlov : Public domain */
3 
4 #include "7zAlloc.h"
5 
6 #if defined(_WIN32)
7 #include <WinSock2.h>
8 #include <Windows.h>
9 #endif
10 
11 /* #define _SZ_ALLOC_DEBUG */
12 /* use _SZ_ALLOC_DEBUG to debug alloc/free operations */
13 
14 #ifdef _SZ_ALLOC_DEBUG
15 
16 #ifdef _WIN32
17 #include <windows.h>
18 #endif
19 
20 #include <stdio.h>
21 int g_allocCount = 0;
22 int g_allocCountTemp = 0;
23 
24 #endif
25 #include "clamav.h"
26 
SzAlloc(void * p,size_t size)27 void *SzAlloc(void *p, size_t size)
28 {
29     UNUSEDPARAM(p);
30   if (size == 0)
31     return 0;
32   #ifdef _SZ_ALLOC_DEBUG
33   fprintf(stderr, "\nAlloc %10d bytes; count = %10d", size, g_allocCount);
34   g_allocCount++;
35   #endif
36   return malloc(size);
37 }
38 
SzFree(void * p,void * address)39 void SzFree(void *p, void *address)
40 {
41     UNUSEDPARAM(p);
42   #ifdef _SZ_ALLOC_DEBUG
43   if (address != 0)
44   {
45     g_allocCount--;
46     fprintf(stderr, "\nFree; count = %10d", g_allocCount);
47   }
48   #endif
49   free(address);
50 }
51 
SzAllocTemp(void * p,size_t size)52 void *SzAllocTemp(void *p, size_t size)
53 {
54     UNUSEDPARAM(p);
55   if (size == 0)
56     return 0;
57   #ifdef _SZ_ALLOC_DEBUG
58   fprintf(stderr, "\nAlloc_temp %10d bytes;  count = %10d", size, g_allocCountTemp);
59   g_allocCountTemp++;
60   #ifdef _WIN32
61   return HeapAlloc(GetProcessHeap(), 0, size);
62   #endif
63   #endif
64   return malloc(size);
65 }
66 
SzFreeTemp(void * p,void * address)67 void SzFreeTemp(void *p, void *address)
68 {
69     UNUSEDPARAM(p);
70   #ifdef _SZ_ALLOC_DEBUG
71   if (address != 0)
72   {
73     g_allocCountTemp--;
74     fprintf(stderr, "\nFree_temp; count = %10d", g_allocCountTemp);
75   }
76   #ifdef _WIN32
77   HeapFree(GetProcessHeap(), 0, address);
78   return;
79   #endif
80   #endif
81   free(address);
82 }
83