xref: /aosp_15_r20/external/lzma/C/7zAlloc.c (revision f6dc9357d832569d4d1f5d24eacdb3935a1ae8e6)
1 /* 7zAlloc.c -- Allocation functions for 7z processing
2 2023-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>
22 static int g_allocCount = 0;
23 static int g_allocCountTemp = 0;
24 
Print_Alloc(const char * s,size_t size,int * counter)25 static 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 }
Print_Free(const char * s,int * counter)31 static void Print_Free(const char *s, int *counter)
32 {
33   (*counter)--;
34   fprintf(stderr, "\n%s count = %10d", s, *counter);
35 }
36 #endif
37 
SzAlloc(ISzAllocPtr p,size_t size)38 void *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 
SzFree(ISzAllocPtr p,void * address)49 void 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 
SzAllocTemp(ISzAllocPtr p,size_t size)59 void *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 
SzFreeTemp(ISzAllocPtr p,void * address)75 void 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