alloc.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. // ======================================================================== //
  2. // Copyright 2009-2017 Intel Corporation //
  3. // //
  4. // Licensed under the Apache License, Version 2.0 (the "License"); //
  5. // you may not use this file except in compliance with the License. //
  6. // You may obtain a copy of the License at //
  7. // //
  8. // http://www.apache.org/licenses/LICENSE-2.0 //
  9. // //
  10. // Unless required by applicable law or agreed to in writing, software //
  11. // distributed under the License is distributed on an "AS IS" BASIS, //
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. //
  13. // See the License for the specific language governing permissions and //
  14. // limitations under the License. //
  15. // ======================================================================== //
  16. #include "config.h"
  17. #include "alloc.h"
  18. #include "intrinsics.h"
  19. #include "sysinfo.h"
  20. ////////////////////////////////////////////////////////////////////////////////
  21. /// Windows Platform
  22. ////////////////////////////////////////////////////////////////////////////////
  23. #ifdef _WIN32
  24. #define WIN32_LEAN_AND_MEAN
  25. #include <windows.h>
  26. #include <malloc.h>
  27. namespace embree
  28. {
  29. void* os_malloc(size_t bytes)
  30. {
  31. int flags = MEM_COMMIT | MEM_RESERVE;
  32. char* ptr = (char*) VirtualAlloc(nullptr,bytes,flags,PAGE_READWRITE);
  33. if (ptr == nullptr) throw std::bad_alloc();
  34. return ptr;
  35. }
  36. void* os_reserve(size_t bytes)
  37. {
  38. char* ptr = (char*) VirtualAlloc(nullptr,bytes,MEM_RESERVE,PAGE_READWRITE);
  39. if (ptr == nullptr) throw std::bad_alloc();
  40. return ptr;
  41. }
  42. void os_commit (void* ptr, size_t bytes) {
  43. VirtualAlloc(ptr,bytes,MEM_COMMIT,PAGE_READWRITE);
  44. }
  45. size_t os_shrink(void* ptr, size_t bytesNew, size_t bytesOld)
  46. {
  47. size_t pageSize = 4096;
  48. bytesNew = (bytesNew+pageSize-1) & ~(pageSize-1);
  49. assert(bytesNew <= bytesOld);
  50. if (bytesNew < bytesOld)
  51. VirtualFree((char*)ptr+bytesNew,bytesOld-bytesNew,MEM_DECOMMIT);
  52. return bytesNew;
  53. }
  54. void os_free(void* ptr, size_t bytes) {
  55. if (bytes == 0) return;
  56. if (!VirtualFree(ptr,0,MEM_RELEASE))
  57. /*throw std::bad_alloc()*/ return; // we on purpose do not throw an exception when an error occurs, to avoid throwing an exception during error handling
  58. }
  59. void os_advise(void *ptr, size_t bytes)
  60. {
  61. }
  62. }
  63. #endif
  64. ////////////////////////////////////////////////////////////////////////////////
  65. /// Unix Platform
  66. ////////////////////////////////////////////////////////////////////////////////
  67. #if defined(__UNIX__)
  68. #include <sys/mman.h>
  69. #include <errno.h>
  70. #include <stdlib.h>
  71. #include <string.h>
  72. #define UPGRADE_TO_2M_PAGE_LIMIT (256*1024)
  73. namespace embree
  74. {
  75. __forceinline bool isHugePageCandidate(const size_t bytes)
  76. {
  77. /* try to use huge pages for large allocations */
  78. if (bytes >= PAGE_SIZE_2M)
  79. {
  80. /* multiple of page size */
  81. if ((bytes % PAGE_SIZE_2M) == 0)
  82. return true;
  83. else if (bytes >= 64 * PAGE_SIZE_2M) /* will only introduce a 3% overhead */
  84. return true;
  85. }
  86. return false;
  87. }
  88. #if !defined(__MACOSX__)
  89. static bool tryDirectHugePageAllocation = true;
  90. #endif
  91. /* hint for transparent huge pages (THP) */
  92. void os_advise(void *pptr, size_t bytes)
  93. {
  94. #if defined(MADV_HUGEPAGE)
  95. if (isHugePageCandidate(bytes))
  96. madvise(pptr,bytes,MADV_HUGEPAGE);
  97. #endif
  98. }
  99. void* os_malloc(size_t bytes)
  100. {
  101. int flags = MAP_PRIVATE | MAP_ANON;
  102. if (isHugePageCandidate(bytes))
  103. {
  104. bytes = (bytes+PAGE_SIZE_2M-1)&ssize_t(-PAGE_SIZE_2M);
  105. #if !defined(__MACOSX__)
  106. /* try direct huge page allocation first */
  107. if (tryDirectHugePageAllocation)
  108. {
  109. int huge_flags = flags;
  110. #ifdef MAP_HUGETLB
  111. huge_flags |= MAP_HUGETLB;
  112. #endif
  113. #ifdef MAP_ALIGNED_SUPER
  114. huge_flags |= MAP_ALIGNED_SUPER;
  115. #endif
  116. void* ptr = mmap(0, bytes, PROT_READ | PROT_WRITE, huge_flags, -1, 0);
  117. if (ptr == nullptr || ptr == MAP_FAILED)
  118. {
  119. /* direct huge page allocation failed, disable it for the future */
  120. tryDirectHugePageAllocation = false;
  121. }
  122. else
  123. return ptr;
  124. }
  125. #endif
  126. }
  127. else
  128. bytes = (bytes+PAGE_SIZE_4K-1)&ssize_t(-PAGE_SIZE_4K);
  129. /* standard mmap call */
  130. void* ptr = (char*) mmap(0, bytes, PROT_READ | PROT_WRITE, flags, -1, 0);
  131. assert( ptr != MAP_FAILED );
  132. if (ptr == nullptr || ptr == MAP_FAILED) throw std::bad_alloc();
  133. /* advise huge page hint for THP */
  134. os_advise(ptr,bytes);
  135. return ptr;
  136. }
  137. void* os_reserve(size_t bytes)
  138. {
  139. /* linux always allocates pages on demand, thus just call allocate */
  140. return os_malloc(bytes);
  141. }
  142. void os_commit (void* ptr, size_t bytes) {
  143. }
  144. size_t os_shrink(void* ptr, size_t bytesNew, size_t bytesOld)
  145. {
  146. /* first try with 4KB pages */
  147. bytesNew = (bytesNew+PAGE_SIZE_4K-1) & ~(PAGE_SIZE_4K-1);
  148. assert(bytesNew <= bytesOld);
  149. if (bytesNew >= bytesOld)
  150. return bytesOld;
  151. if (munmap((char*)ptr+bytesNew,bytesOld-bytesNew) != -1)
  152. return bytesNew;
  153. /* now try with 2MB pages */
  154. bytesNew = (bytesNew+PAGE_SIZE_2M-1) & ~(PAGE_SIZE_2M-1);
  155. assert(bytesNew <= bytesOld);
  156. if (bytesNew >= bytesOld)
  157. return bytesOld;
  158. if (munmap((char*)ptr+bytesNew,bytesOld-bytesNew) != -1)
  159. return bytesNew; // this may be too small in case we really used 2MB pages
  160. throw std::bad_alloc();
  161. }
  162. void os_free(void* ptr, size_t bytes)
  163. {
  164. if (bytes == 0)
  165. return;
  166. size_t pageSize = PAGE_SIZE_4K;
  167. if (isHugePageCandidate(bytes))
  168. pageSize = PAGE_SIZE_2M;
  169. bytes = (bytes+pageSize-1)&ssize_t(-pageSize);
  170. if (munmap(ptr,bytes) == -1)
  171. /*throw std::bad_alloc()*/ return; // we on purpose do not throw an exception when an error occurs, to avoid throwing an exception during error handling
  172. }
  173. }
  174. #endif
  175. ////////////////////////////////////////////////////////////////////////////////
  176. /// All Platforms
  177. ////////////////////////////////////////////////////////////////////////////////
  178. namespace embree
  179. {
  180. void* alignedMalloc(size_t size, size_t align)
  181. {
  182. assert((align & (align-1)) == 0);
  183. void* ptr = _mm_malloc(size,align);
  184. if (size != 0 && ptr == nullptr)
  185. throw std::bad_alloc();
  186. return ptr;
  187. }
  188. void alignedFree(void* ptr) {
  189. _mm_free(ptr);
  190. }
  191. }