7zAlloc.c 1.2 KB

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