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