7zAlloc.c 1.4 KB

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