Alloc.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /* Alloc.c */
  2. #ifdef _WIN32
  3. #include <windows.h>
  4. #endif
  5. #include <stdlib.h>
  6. #include "Alloc.h"
  7. /* #define _SZ_ALLOC_DEBUG */
  8. /* use _SZ_ALLOC_DEBUG to debug alloc/free operations */
  9. #ifdef _SZ_ALLOC_DEBUG
  10. #include <stdio.h>
  11. int g_allocCount = 0;
  12. int g_allocCountMid = 0;
  13. int g_allocCountBig = 0;
  14. #endif
  15. void *MyAlloc(size_t size)
  16. {
  17. if (size == 0)
  18. return 0;
  19. #ifdef _SZ_ALLOC_DEBUG
  20. fprintf(stderr, "\nAlloc %10d bytes; count = %10d", size, g_allocCount++);
  21. #endif
  22. return malloc(size);
  23. }
  24. void MyFree(void *address)
  25. {
  26. #ifdef _SZ_ALLOC_DEBUG
  27. if (address != 0)
  28. fprintf(stderr, "\nFree; count = %10d", --g_allocCount);
  29. #endif
  30. free(address);
  31. }
  32. #ifdef _WIN32
  33. void *MidAlloc(size_t size)
  34. {
  35. if (size == 0)
  36. return 0;
  37. #ifdef _SZ_ALLOC_DEBUG
  38. fprintf(stderr, "\nAlloc_Mid %10d bytes; count = %10d", size, g_allocCountMid++);
  39. #endif
  40. return VirtualAlloc(0, size, MEM_COMMIT, PAGE_READWRITE);
  41. }
  42. void MidFree(void *address)
  43. {
  44. #ifdef _SZ_ALLOC_DEBUG
  45. if (address != 0)
  46. fprintf(stderr, "\nFree_Mid; count = %10d", --g_allocCountMid);
  47. #endif
  48. if (address == 0)
  49. return;
  50. VirtualFree(address, 0, MEM_RELEASE);
  51. }
  52. #ifndef MEM_LARGE_PAGES
  53. #undef _7ZIP_LARGE_PAGES
  54. #endif
  55. #ifdef _7ZIP_LARGE_PAGES
  56. SIZE_T g_LargePageSize = 0;
  57. typedef SIZE_T (WINAPI *GetLargePageMinimumP)();
  58. #endif
  59. void SetLargePageSize()
  60. {
  61. #ifdef _7ZIP_LARGE_PAGES
  62. SIZE_T size = 0;
  63. GetLargePageMinimumP largePageMinimum = (GetLargePageMinimumP)
  64. GetProcAddress(GetModuleHandle(TEXT("kernel32.dll")), "GetLargePageMinimum");
  65. if (largePageMinimum == 0)
  66. return;
  67. size = largePageMinimum();
  68. if (size == 0 || (size & (size - 1)) != 0)
  69. return;
  70. g_LargePageSize = size;
  71. #endif
  72. }
  73. void *BigAlloc(size_t size)
  74. {
  75. if (size == 0)
  76. return 0;
  77. #ifdef _SZ_ALLOC_DEBUG
  78. fprintf(stderr, "\nAlloc_Big %10d bytes; count = %10d", size, g_allocCountBig++);
  79. #endif
  80. #ifdef _7ZIP_LARGE_PAGES
  81. if (g_LargePageSize != 0 && g_LargePageSize <= (1 << 30) && size >= (1 << 18))
  82. {
  83. void *res = VirtualAlloc(0, (size + g_LargePageSize - 1) & (~(g_LargePageSize - 1)),
  84. MEM_COMMIT | MEM_LARGE_PAGES, PAGE_READWRITE);
  85. if (res != 0)
  86. return res;
  87. }
  88. #endif
  89. return VirtualAlloc(0, size, MEM_COMMIT, PAGE_READWRITE);
  90. }
  91. void BigFree(void *address)
  92. {
  93. #ifdef _SZ_ALLOC_DEBUG
  94. if (address != 0)
  95. fprintf(stderr, "\nFree_Big; count = %10d", --g_allocCountBig);
  96. #endif
  97. if (address == 0)
  98. return;
  99. VirtualFree(address, 0, MEM_RELEASE);
  100. }
  101. #endif