Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

127 lines
2.6 KiB

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