AlignedAlloc.hpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /*
  2. Copyright (c) 2009-2010 Christopher A. Taylor. All rights reserved.
  3. Redistribution and use in source and binary forms, with or without
  4. modification, are permitted provided that the following conditions are met:
  5. * Redistributions of source code must retain the above copyright notice,
  6. this list of conditions and the following disclaimer.
  7. * Redistributions in binary form must reproduce the above copyright notice,
  8. this list of conditions and the following disclaimer in the documentation
  9. and/or other materials provided with the distribution.
  10. * Neither the name of LibCat nor the names of its contributors may be used
  11. to endorse or promote products derived from this software without
  12. specific prior written permission.
  13. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  14. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  15. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  16. ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
  17. LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  18. CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  19. SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  20. INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  21. CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  22. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  23. POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #ifndef CAT_ALIGNED_ALLOC_HPP
  26. #define CAT_ALIGNED_ALLOC_HPP
  27. #include <cat/Platform.hpp>
  28. #include <cstddef> // size_t
  29. #include <vector> // std::_Construct and std::_Destroy
  30. namespace cat {
  31. // Small to medium -size aligned heap allocator
  32. class Aligned
  33. {
  34. public:
  35. CAT_INLINE Aligned() {}
  36. // Acquires memory aligned to a CPU cache-line byte boundary from the heap
  37. static void *Acquire(u32 bytes);
  38. // Resizes an aligned pointer
  39. static void *Resize(void *ptr, u32 bytes);
  40. // Release an aligned pointer
  41. static void Release(void *ptr);
  42. template<class T>
  43. static inline void Delete(T *ptr)
  44. {
  45. ptr->~T();
  46. Release(ptr);
  47. }
  48. static Aligned ii;
  49. };
  50. #if 0
  51. // Use STLAlignedAllocator in place of the standard STL allocator
  52. // to make use of the Aligned in STL types.
  53. template<typename T>
  54. class STLAlignedAllocator
  55. {
  56. public:
  57. typedef std::size_t size_type;
  58. typedef std::size_t difference_type;
  59. typedef T *pointer;
  60. typedef const T *const_pointer;
  61. typedef T &reference;
  62. typedef const T &const_reference;
  63. typedef T value_type;
  64. template<typename S>
  65. struct rebind
  66. {
  67. typedef STLAlignedAllocator<S> other;
  68. };
  69. pointer address(reference X) const
  70. {
  71. return &X;
  72. }
  73. const_pointer address(const_reference X) const
  74. {
  75. return &X;
  76. }
  77. STLAlignedAllocator() throw ()
  78. {
  79. }
  80. template<typename S>
  81. STLAlignedAllocator(const STLAlignedAllocator<S> &cp) throw ()
  82. {
  83. }
  84. template<typename S>
  85. STLAlignedAllocator<T> &operator=(const STLAlignedAllocator<S> &cp) throw ()
  86. {
  87. return *this;
  88. }
  89. pointer allocate(size_type Count, const void *Hint = 0)
  90. {
  91. return (pointer)Aligned::Acquire((u32)Count * sizeof(T));
  92. }
  93. void deallocate(pointer Ptr, size_type Count)
  94. {
  95. Aligned::Release(Ptr);
  96. }
  97. void construct(pointer Ptr, const T &Val)
  98. {
  99. std::_Construct(Ptr, Val);
  100. }
  101. void destroy(pointer Ptr)
  102. {
  103. std::_Destroy(Ptr);
  104. }
  105. size_type max_size() const
  106. {
  107. return 0x00FFFFFF;
  108. }
  109. template<typename S>
  110. bool operator==(STLAlignedAllocator <S> const &) const throw()
  111. {
  112. return true;
  113. }
  114. template<typename S>
  115. bool operator!=(STLAlignedAllocator <S> const &) const throw()
  116. {
  117. return false;
  118. }
  119. };
  120. #endif
  121. // Large-size aligned heap allocator
  122. class LargeAligned
  123. {
  124. public:
  125. // Acquires memory aligned to a CPU cache-line byte boundary from the heap
  126. static void *Acquire(u32 bytes);
  127. // Release an aligned pointer
  128. static void Release(void *ptr);
  129. };
  130. #if 0
  131. // Use STLAlignedAllocator in place of the standard STL allocator
  132. // to make use of the Aligned in STL types.
  133. template<typename T>
  134. class STLLargeAlignedAllocator
  135. {
  136. public:
  137. typedef std::size_t size_type;
  138. typedef std::size_t difference_type;
  139. typedef T *pointer;
  140. typedef const T *const_pointer;
  141. typedef T &reference;
  142. typedef const T &const_reference;
  143. typedef T value_type;
  144. template<typename S>
  145. struct rebind
  146. {
  147. typedef STLLargeAlignedAllocator<S> other;
  148. };
  149. pointer address(reference X) const
  150. {
  151. return &X;
  152. }
  153. const_pointer address(const_reference X) const
  154. {
  155. return &X;
  156. }
  157. STLLargeAlignedAllocator() throw ()
  158. {
  159. }
  160. template<typename S>
  161. STLLargeAlignedAllocator(const STLLargeAlignedAllocator<S> &cp) throw ()
  162. {
  163. }
  164. template<typename S>
  165. STLLargeAlignedAllocator<T> &operator=(const STLLargeAlignedAllocator<S> &cp) throw ()
  166. {
  167. return *this;
  168. }
  169. pointer allocate(size_type Count, const void *Hint = 0)
  170. {
  171. return (pointer)LargeAligned::Acquire((u32)Count * sizeof(T));
  172. }
  173. void deallocate(pointer Ptr, size_type Count)
  174. {
  175. LargeAligned::Release(Ptr);
  176. }
  177. void construct(pointer Ptr, const T &Val)
  178. {
  179. std::_Construct(Ptr, Val);
  180. }
  181. void destroy(pointer Ptr)
  182. {
  183. std::_Destroy(Ptr);
  184. }
  185. size_type max_size() const
  186. {
  187. return 0x00FFFFFF;
  188. }
  189. template<typename S>
  190. bool operator==(STLLargeAlignedAllocator <S> const &) const throw()
  191. {
  192. return true;
  193. }
  194. template<typename S>
  195. bool operator!=(STLLargeAlignedAllocator <S> const &) const throw()
  196. {
  197. return false;
  198. }
  199. };
  200. #endif
  201. u32 GetCacheLineBytes();
  202. } // namespace cat
  203. // Provide placement new constructor and delete pair to allow for
  204. // an easy syntax to create objects from the RegionAllocator:
  205. // T *a = new (Aligned()) T();
  206. // The object can be freed with:
  207. // Aligned::Delete(a);
  208. // Which insures that the destructor is called before freeing memory
  209. CAT_INLINE void *operator new[](std::size_t bytes, cat::Aligned &) throw()
  210. {
  211. return cat::Aligned::Acquire((int)bytes);
  212. }
  213. // Placement "delete": Does not call destructor
  214. CAT_INLINE void operator delete(void *ptr, cat::Aligned &) throw()
  215. {
  216. cat::Aligned::Release(ptr);
  217. }
  218. #endif // CAT_ENDIAN_NEUTRAL_HPP
粤ICP备19079148号