Alloc.c 2.6 KB

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