7zip/C/7zAlloc.c

90 lines
1.6 KiB
C
Raw Permalink Normal View History

2023-06-21 02:00:00 +02:00
/* 7zAlloc.c -- Allocation functions for 7z processing
2023-03-04 : Igor Pavlov : Public domain */
2021-12-27 01:00:00 +01:00
#include "Precomp.h"
#include <stdlib.h>
#include "7zAlloc.h"
2023-06-21 02:00:00 +02:00
/* #define SZ_ALLOC_DEBUG */
/* use SZ_ALLOC_DEBUG to debug alloc/free operations */
2021-12-27 01:00:00 +01:00
2023-06-21 02:00:00 +02:00
#ifdef SZ_ALLOC_DEBUG
2021-12-27 01:00:00 +01:00
2023-06-21 02:00:00 +02:00
/*
2021-12-27 01:00:00 +01:00
#ifdef _WIN32
2023-06-21 02:00:00 +02:00
#include "7zWindows.h"
2021-12-27 01:00:00 +01:00
#endif
2023-06-21 02:00:00 +02:00
*/
2021-12-27 01:00:00 +01:00
#include <stdio.h>
2023-06-21 02:00:00 +02:00
static int g_allocCount = 0;
static int g_allocCountTemp = 0;
2021-12-27 01:00:00 +01:00
2023-06-21 02:00:00 +02:00
static void Print_Alloc(const char *s, size_t size, int *counter)
{
const unsigned size2 = (unsigned)size;
fprintf(stderr, "\n%s count = %10d : %10u bytes; ", s, *counter, size2);
(*counter)++;
}
static void Print_Free(const char *s, int *counter)
{
(*counter)--;
fprintf(stderr, "\n%s count = %10d", s, *counter);
}
2021-12-27 01:00:00 +01:00
#endif
void *SzAlloc(ISzAllocPtr p, size_t size)
{
2023-06-21 02:00:00 +02:00
UNUSED_VAR(p)
2021-12-27 01:00:00 +01:00
if (size == 0)
return 0;
2023-06-21 02:00:00 +02:00
#ifdef SZ_ALLOC_DEBUG
Print_Alloc("Alloc", size, &g_allocCount);
2021-12-27 01:00:00 +01:00
#endif
return malloc(size);
}
void SzFree(ISzAllocPtr p, void *address)
{
2023-06-21 02:00:00 +02:00
UNUSED_VAR(p)
#ifdef SZ_ALLOC_DEBUG
if (address)
Print_Free("Free ", &g_allocCount);
2021-12-27 01:00:00 +01:00
#endif
free(address);
}
void *SzAllocTemp(ISzAllocPtr p, size_t size)
{
2023-06-21 02:00:00 +02:00
UNUSED_VAR(p)
2021-12-27 01:00:00 +01:00
if (size == 0)
return 0;
2023-06-21 02:00:00 +02:00
#ifdef SZ_ALLOC_DEBUG
Print_Alloc("Alloc_temp", size, &g_allocCountTemp);
/*
2021-12-27 01:00:00 +01:00
#ifdef _WIN32
return HeapAlloc(GetProcessHeap(), 0, size);
#endif
2023-06-21 02:00:00 +02:00
*/
2021-12-27 01:00:00 +01:00
#endif
return malloc(size);
}
void SzFreeTemp(ISzAllocPtr p, void *address)
{
2023-06-21 02:00:00 +02:00
UNUSED_VAR(p)
#ifdef SZ_ALLOC_DEBUG
if (address)
Print_Free("Free_temp ", &g_allocCountTemp);
/*
2021-12-27 01:00:00 +01:00
#ifdef _WIN32
HeapFree(GetProcessHeap(), 0, address);
return;
#endif
2023-06-21 02:00:00 +02:00
*/
2021-12-27 01:00:00 +01:00
#endif
free(address);
}