7zAlloc.c 1.5 KB

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