1370b324cSopenharmony_ci/* 7zAlloc.c -- Allocation functions for 7z processing
2370b324cSopenharmony_ci2023-03-04 : Igor Pavlov : Public domain */
3370b324cSopenharmony_ci
4370b324cSopenharmony_ci#include "Precomp.h"
5370b324cSopenharmony_ci
6370b324cSopenharmony_ci#include <stdlib.h>
7370b324cSopenharmony_ci
8370b324cSopenharmony_ci#include "7zAlloc.h"
9370b324cSopenharmony_ci
10370b324cSopenharmony_ci/* #define SZ_ALLOC_DEBUG */
11370b324cSopenharmony_ci/* use SZ_ALLOC_DEBUG to debug alloc/free operations */
12370b324cSopenharmony_ci
13370b324cSopenharmony_ci#ifdef SZ_ALLOC_DEBUG
14370b324cSopenharmony_ci
15370b324cSopenharmony_ci/*
16370b324cSopenharmony_ci#ifdef _WIN32
17370b324cSopenharmony_ci#include "7zWindows.h"
18370b324cSopenharmony_ci#endif
19370b324cSopenharmony_ci*/
20370b324cSopenharmony_ci
21370b324cSopenharmony_ci#include <stdio.h>
22370b324cSopenharmony_cistatic int g_allocCount = 0;
23370b324cSopenharmony_cistatic int g_allocCountTemp = 0;
24370b324cSopenharmony_ci
25370b324cSopenharmony_cistatic void Print_Alloc(const char *s, size_t size, int *counter)
26370b324cSopenharmony_ci{
27370b324cSopenharmony_ci  const unsigned size2 = (unsigned)size;
28370b324cSopenharmony_ci  fprintf(stderr, "\n%s count = %10d : %10u bytes; ", s, *counter, size2);
29370b324cSopenharmony_ci  (*counter)++;
30370b324cSopenharmony_ci}
31370b324cSopenharmony_cistatic void Print_Free(const char *s, int *counter)
32370b324cSopenharmony_ci{
33370b324cSopenharmony_ci  (*counter)--;
34370b324cSopenharmony_ci  fprintf(stderr, "\n%s count = %10d", s, *counter);
35370b324cSopenharmony_ci}
36370b324cSopenharmony_ci#endif
37370b324cSopenharmony_ci
38370b324cSopenharmony_civoid *SzAlloc(ISzAllocPtr p, size_t size)
39370b324cSopenharmony_ci{
40370b324cSopenharmony_ci  UNUSED_VAR(p)
41370b324cSopenharmony_ci  if (size == 0)
42370b324cSopenharmony_ci    return 0;
43370b324cSopenharmony_ci  #ifdef SZ_ALLOC_DEBUG
44370b324cSopenharmony_ci  Print_Alloc("Alloc", size, &g_allocCount);
45370b324cSopenharmony_ci  #endif
46370b324cSopenharmony_ci  return malloc(size);
47370b324cSopenharmony_ci}
48370b324cSopenharmony_ci
49370b324cSopenharmony_civoid SzFree(ISzAllocPtr p, void *address)
50370b324cSopenharmony_ci{
51370b324cSopenharmony_ci  UNUSED_VAR(p)
52370b324cSopenharmony_ci  #ifdef SZ_ALLOC_DEBUG
53370b324cSopenharmony_ci  if (address)
54370b324cSopenharmony_ci    Print_Free("Free ", &g_allocCount);
55370b324cSopenharmony_ci  #endif
56370b324cSopenharmony_ci  free(address);
57370b324cSopenharmony_ci}
58370b324cSopenharmony_ci
59370b324cSopenharmony_civoid *SzAllocTemp(ISzAllocPtr p, size_t size)
60370b324cSopenharmony_ci{
61370b324cSopenharmony_ci  UNUSED_VAR(p)
62370b324cSopenharmony_ci  if (size == 0)
63370b324cSopenharmony_ci    return 0;
64370b324cSopenharmony_ci  #ifdef SZ_ALLOC_DEBUG
65370b324cSopenharmony_ci  Print_Alloc("Alloc_temp", size, &g_allocCountTemp);
66370b324cSopenharmony_ci  /*
67370b324cSopenharmony_ci  #ifdef _WIN32
68370b324cSopenharmony_ci  return HeapAlloc(GetProcessHeap(), 0, size);
69370b324cSopenharmony_ci  #endif
70370b324cSopenharmony_ci  */
71370b324cSopenharmony_ci  #endif
72370b324cSopenharmony_ci  return malloc(size);
73370b324cSopenharmony_ci}
74370b324cSopenharmony_ci
75370b324cSopenharmony_civoid SzFreeTemp(ISzAllocPtr p, void *address)
76370b324cSopenharmony_ci{
77370b324cSopenharmony_ci  UNUSED_VAR(p)
78370b324cSopenharmony_ci  #ifdef SZ_ALLOC_DEBUG
79370b324cSopenharmony_ci  if (address)
80370b324cSopenharmony_ci    Print_Free("Free_temp ", &g_allocCountTemp);
81370b324cSopenharmony_ci  /*
82370b324cSopenharmony_ci  #ifdef _WIN32
83370b324cSopenharmony_ci  HeapFree(GetProcessHeap(), 0, address);
84370b324cSopenharmony_ci  return;
85370b324cSopenharmony_ci  #endif
86370b324cSopenharmony_ci  */
87370b324cSopenharmony_ci  #endif
88370b324cSopenharmony_ci  free(address);
89370b324cSopenharmony_ci}
90