system-alloc.cc 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. // Copyright (c) 2013, Google Inc.
  2. // All rights reserved.
  3. //
  4. // Redistribution and use in source and binary forms, with or without
  5. // modification, are permitted provided that the following conditions are
  6. // met:
  7. //
  8. // * Redistributions of source code must retain the above copyright
  9. // notice, this list of conditions and the following disclaimer.
  10. // * Redistributions in binary form must reproduce the above
  11. // copyright notice, this list of conditions and the following disclaimer
  12. // in the documentation and/or other materials provided with the
  13. // distribution.
  14. // * Neither the name of Google Inc. nor the names of its
  15. // contributors may be used to endorse or promote products derived from
  16. // this software without specific prior written permission.
  17. //
  18. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. // ---
  30. // Author: Petr Hosek
  31. #ifndef _WIN32
  32. # error You should only be including windows/system-alloc.cc in a windows environment!
  33. #endif
  34. #include <config.h>
  35. #include <windows.h>
  36. #include <algorithm> // std::min
  37. #include <gperftools/malloc_extension.h>
  38. #include "base/logging.h"
  39. #include "base/spinlock.h"
  40. #include "internal_logging.h"
  41. #include "system-alloc.h"
  42. static SpinLock spinlock(SpinLock::LINKER_INITIALIZED);
  43. // The current system allocator declaration
  44. SysAllocator* sys_alloc = NULL;
  45. // Number of bytes taken from system.
  46. size_t TCMalloc_SystemTaken = 0;
  47. class VirtualSysAllocator : public SysAllocator {
  48. public:
  49. VirtualSysAllocator() : SysAllocator() {
  50. }
  51. void* Alloc(size_t size, size_t *actual_size, size_t alignment);
  52. };
  53. static char virtual_space[sizeof(VirtualSysAllocator)];
  54. // This is mostly like MmapSysAllocator::Alloc, except it does these weird
  55. // munmap's in the middle of the page, which is forbidden in windows.
  56. void* VirtualSysAllocator::Alloc(size_t size, size_t *actual_size,
  57. size_t alignment) {
  58. // Align on the pagesize boundary
  59. const int pagesize = getpagesize();
  60. if (alignment < pagesize) alignment = pagesize;
  61. size = ((size + alignment - 1) / alignment) * alignment;
  62. // Report the total number of bytes the OS actually delivered. This might be
  63. // greater than |size| because of alignment concerns. The full size is
  64. // necessary so that adjacent spans can be coalesced.
  65. // TODO(antonm): proper processing of alignments
  66. // in actual_size and decommitting.
  67. if (actual_size) {
  68. *actual_size = size;
  69. }
  70. // We currently do not support alignments larger than the pagesize or
  71. // alignments that are not multiples of the pagesize after being floored.
  72. // If this ability is needed it can be done by the caller (assuming it knows
  73. // the page size).
  74. assert(alignment <= pagesize);
  75. void* result = VirtualAlloc(0, size,
  76. MEM_COMMIT|MEM_RESERVE, PAGE_READWRITE);
  77. if (result == NULL)
  78. return NULL;
  79. // If the result is not aligned memory fragmentation will result which can
  80. // lead to pathological memory use.
  81. assert((reinterpret_cast<uintptr_t>(result) & (alignment - 1)) == 0);
  82. return result;
  83. }
  84. #ifdef _MSC_VER
  85. extern "C" SysAllocator* tc_get_sysalloc_override(SysAllocator *def);
  86. extern "C" SysAllocator* tc_get_sysalloc_default(SysAllocator *def)
  87. {
  88. return def;
  89. }
  90. #if defined(_M_IX86)
  91. #pragma comment(linker, "/alternatename:_tc_get_sysalloc_override=_tc_get_sysalloc_default")
  92. #elif defined(_M_X64)
  93. #pragma comment(linker, "/alternatename:tc_get_sysalloc_override=tc_get_sysalloc_default")
  94. #endif
  95. #else // !_MSC_VER
  96. extern "C" ATTRIBUTE_NOINLINE
  97. SysAllocator* tc_get_sysalloc_override(SysAllocator *def)
  98. {
  99. return def;
  100. }
  101. #endif
  102. static bool system_alloc_inited = false;
  103. void InitSystemAllocators(void) {
  104. VirtualSysAllocator *alloc = new (virtual_space) VirtualSysAllocator();
  105. sys_alloc = tc_get_sysalloc_override(alloc);
  106. }
  107. extern PERFTOOLS_DLL_DECL
  108. void* TCMalloc_SystemAlloc(size_t size, size_t *actual_size,
  109. size_t alignment) {
  110. SpinLockHolder lock_holder(&spinlock);
  111. if (!system_alloc_inited) {
  112. InitSystemAllocators();
  113. system_alloc_inited = true;
  114. }
  115. void* result = sys_alloc->Alloc(size, actual_size, alignment);
  116. if (result != NULL) {
  117. if (actual_size) {
  118. TCMalloc_SystemTaken += *actual_size;
  119. } else {
  120. TCMalloc_SystemTaken += size;
  121. }
  122. }
  123. return result;
  124. }
  125. extern PERFTOOLS_DLL_DECL
  126. bool TCMalloc_SystemRelease(void* start, size_t length) {
  127. if (VirtualFree(start, length, MEM_DECOMMIT))
  128. return true;
  129. // The decommit may fail if the memory region consists of allocations
  130. // from more than one call to VirtualAlloc. In this case, fall back to
  131. // using VirtualQuery to retrieve the allocation boundaries and decommit
  132. // them each individually.
  133. char* ptr = static_cast<char*>(start);
  134. char* end = ptr + length;
  135. MEMORY_BASIC_INFORMATION info;
  136. while (ptr < end) {
  137. size_t resultSize = VirtualQuery(ptr, &info, sizeof(info));
  138. assert(resultSize == sizeof(info));
  139. size_t decommitSize = std::min<size_t>(info.RegionSize, end - ptr);
  140. BOOL success = VirtualFree(ptr, decommitSize, MEM_DECOMMIT);
  141. assert(success == TRUE);
  142. ptr += decommitSize;
  143. }
  144. return true;
  145. }
  146. extern PERFTOOLS_DLL_DECL
  147. void TCMalloc_SystemCommit(void* start, size_t length) {
  148. if (VirtualAlloc(start, length, MEM_COMMIT, PAGE_READWRITE) == start)
  149. return;
  150. // The commit may fail if the memory region consists of allocations
  151. // from more than one call to VirtualAlloc. In this case, fall back to
  152. // using VirtualQuery to retrieve the allocation boundaries and commit them
  153. // each individually.
  154. char* ptr = static_cast<char*>(start);
  155. char* end = ptr + length;
  156. MEMORY_BASIC_INFORMATION info;
  157. while (ptr < end) {
  158. size_t resultSize = VirtualQuery(ptr, &info, sizeof(info));
  159. assert(resultSize == sizeof(info));
  160. size_t commitSize = std::min<size_t>(info.RegionSize, end - ptr);
  161. void* newAddress = VirtualAlloc(ptr, commitSize, MEM_COMMIT,
  162. PAGE_READWRITE);
  163. assert(newAddress == ptr);
  164. ptr += commitSize;
  165. }
  166. }
  167. bool RegisterSystemAllocator(SysAllocator *allocator, int priority) {
  168. return false; // we don't allow registration on windows, right now
  169. }
  170. void DumpSystemAllocatorStats(TCMalloc_Printer* printer) {
  171. // We don't dump stats on windows, right now
  172. }