7zAlloc.c 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /* 7zAlloc.c -- Allocation functions for 7z processing
  2. 2023-03-04 : 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. /*
  10. #ifdef _WIN32
  11. #include "7zWindows.h"
  12. #endif
  13. */
  14. #include <stdio.h>
  15. static int g_allocCount = 0;
  16. static int g_allocCountTemp = 0;
  17. static void Print_Alloc(const char *s, size_t size, int *counter)
  18. {
  19. const unsigned size2 = (unsigned)size;
  20. fprintf(stderr, "\n%s count = %10d : %10u bytes; ", s, *counter, size2);
  21. (*counter)++;
  22. }
  23. static void Print_Free(const char *s, int *counter)
  24. {
  25. (*counter)--;
  26. fprintf(stderr, "\n%s count = %10d", s, *counter);
  27. }
  28. #endif
  29. void *SzAlloc(ISzAllocPtr p, size_t size)
  30. {
  31. UNUSED_VAR(p)
  32. if (size == 0)
  33. return 0;
  34. #ifdef SZ_ALLOC_DEBUG
  35. Print_Alloc("Alloc", size, &g_allocCount);
  36. #endif
  37. return malloc(size);
  38. }
  39. void SzFree(ISzAllocPtr p, void *address)
  40. {
  41. UNUSED_VAR(p)
  42. #ifdef SZ_ALLOC_DEBUG
  43. if (address)
  44. Print_Free("Free ", &g_allocCount);
  45. #endif
  46. free(address);
  47. }
  48. void *SzAllocTemp(ISzAllocPtr p, size_t size)
  49. {
  50. UNUSED_VAR(p)
  51. if (size == 0)
  52. return 0;
  53. #ifdef SZ_ALLOC_DEBUG
  54. Print_Alloc("Alloc_temp", size, &g_allocCountTemp);
  55. /*
  56. #ifdef _WIN32
  57. return HeapAlloc(GetProcessHeap(), 0, size);
  58. #endif
  59. */
  60. #endif
  61. return malloc(size);
  62. }
  63. void SzFreeTemp(ISzAllocPtr p, void *address)
  64. {
  65. UNUSED_VAR(p)
  66. #ifdef SZ_ALLOC_DEBUG
  67. if (address)
  68. Print_Free("Free_temp ", &g_allocCountTemp);
  69. /*
  70. #ifdef _WIN32
  71. HeapFree(GetProcessHeap(), 0, address);
  72. return;
  73. #endif
  74. */
  75. #endif
  76. free(address);
  77. }