alloc.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. #pragma once
  17. #include "platform.h"
  18. #include <vector>
  19. namespace embree
  20. {
  21. #define ALIGNED_STRUCT \
  22. void* operator new(size_t size) { return alignedMalloc(size); } \
  23. void operator delete(void* ptr) { alignedFree(ptr); } \
  24. void* operator new[](size_t size) { return alignedMalloc(size); } \
  25. void operator delete[](void* ptr) { alignedFree(ptr); }
  26. #define ALIGNED_STRUCT_(align) \
  27. void* operator new(size_t size) { return alignedMalloc(size,align); } \
  28. void operator delete(void* ptr) { alignedFree(ptr); } \
  29. void* operator new[](size_t size) { return alignedMalloc(size,align); } \
  30. void operator delete[](void* ptr) { alignedFree(ptr); }
  31. #define ALIGNED_CLASS \
  32. public: \
  33. ALIGNED_STRUCT \
  34. private:
  35. #define ALIGNED_CLASS_(align) \
  36. public: \
  37. ALIGNED_STRUCT_(align) \
  38. private:
  39. /*! aligned allocation */
  40. void* alignedMalloc(size_t size, size_t align = 64);
  41. void alignedFree(void* ptr);
  42. /*! allocator that performs aligned allocations */
  43. template<typename T, size_t alignment = 64>
  44. struct aligned_allocator
  45. {
  46. typedef T value_type;
  47. typedef T* pointer;
  48. typedef const T* const_pointer;
  49. typedef T& reference;
  50. typedef const T& const_reference;
  51. typedef std::size_t size_type;
  52. typedef std::ptrdiff_t difference_type;
  53. __forceinline pointer allocate( size_type n ) {
  54. return (pointer) alignedMalloc(n*sizeof(value_type),alignment);
  55. }
  56. __forceinline void deallocate( pointer p, size_type n ) {
  57. return alignedFree(p);
  58. }
  59. __forceinline void construct( pointer p, const_reference val ) {
  60. new (p) T(val);
  61. }
  62. __forceinline void destroy( pointer p ) {
  63. p->~T();
  64. }
  65. };
  66. /*! allocates pages directly from OS */
  67. void* os_malloc (size_t bytes);
  68. void* os_reserve(size_t bytes);
  69. void os_commit (void* ptr, size_t bytes);
  70. size_t os_shrink (void* ptr, size_t bytesNew, size_t bytesOld);
  71. void os_free (void* ptr, size_t bytes);
  72. void os_advise (void* ptr, size_t bytes);
  73. /*! allocator that performs OS allocations */
  74. template<typename T>
  75. struct os_allocator
  76. {
  77. typedef T value_type;
  78. typedef T* pointer;
  79. typedef const T* const_pointer;
  80. typedef T& reference;
  81. typedef const T& const_reference;
  82. typedef std::size_t size_type;
  83. typedef std::ptrdiff_t difference_type;
  84. __forceinline pointer allocate( size_type n ) {
  85. return (pointer) os_malloc(n*sizeof(value_type));
  86. }
  87. __forceinline void deallocate( pointer p, size_type n ) {
  88. return os_free(p,n*sizeof(value_type));
  89. }
  90. __forceinline void construct( pointer p, const_reference val ) {
  91. new (p) T(val);
  92. }
  93. __forceinline void destroy( pointer p ) {
  94. p->~T();
  95. }
  96. };
  97. /*! allocator for IDs */
  98. template<typename T>
  99. struct IDPool
  100. {
  101. typedef T value_type;
  102. IDPool ()
  103. : nextID(0) {}
  104. __forceinline T allocate()
  105. {
  106. /* return ID from list */
  107. if (IDs.size())
  108. {
  109. T id = IDs.back();
  110. IDs.pop_back();
  111. return id;
  112. }
  113. /* allocate new ID */
  114. else {
  115. return nextID++;
  116. }
  117. }
  118. __forceinline void deallocate( T id )
  119. {
  120. assert(id < nextID);
  121. IDs.push_back(id);
  122. }
  123. __forceinline size_t size() const {
  124. return nextID;
  125. }
  126. private:
  127. std::vector<T> IDs; //!< stores deallocated IDs to be reused
  128. size_t nextID; //!< next ID to use when IDs vector is empty
  129. };
  130. }