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