OVR_ContainerAllocator.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. /************************************************************************************
  2. PublicHeader: OVR_Kernel.h
  3. Filename : OVR_ContainerAllocator.h
  4. Content : Template allocators and constructors for containers.
  5. Created : September 19, 2012
  6. Notes :
  7. Copyright : Copyright 2014 Oculus VR, LLC All Rights reserved.
  8. Licensed under the Oculus VR Rift SDK License Version 3.2 (the "License");
  9. you may not use the Oculus VR Rift SDK except in compliance with the License,
  10. which is provided at the time of installation or download, or which
  11. otherwise accompanies this software in either electronic or hard copy form.
  12. You may obtain a copy of the License at
  13. http://www.oculusvr.com/licenses/LICENSE-3.2
  14. Unless required by applicable law or agreed to in writing, the Oculus VR SDK
  15. distributed under the License is distributed on an "AS IS" BASIS,
  16. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. See the License for the specific language governing permissions and
  18. limitations under the License.
  19. ************************************************************************************/
  20. #ifndef OVR_ContainerAllocator_h
  21. #define OVR_ContainerAllocator_h
  22. #include "OVR_Allocator.h"
  23. #include <string.h>
  24. namespace OVR {
  25. //-----------------------------------------------------------------------------------
  26. // ***** Container Allocator
  27. // ContainerAllocator serves as a template argument for allocations done by
  28. // containers, such as Array and Hash; replacing it could allow allocator
  29. // substitution in containers.
  30. class ContainerAllocatorBase
  31. {
  32. public:
  33. static void* Alloc(size_t size)
  34. { return OVR_ALLOC(size); }
  35. static void* Realloc(void* p, size_t newSize)
  36. { return OVR_REALLOC(p, newSize); }
  37. static void Free(void *p)
  38. { OVR_FREE(p); }
  39. };
  40. //-----------------------------------------------------------------------------------
  41. // ***** Constructors, Destructors, Copiers
  42. // Plain Old Data - movable, no special constructors/destructor.
  43. template<class T>
  44. class ConstructorPOD
  45. {
  46. public:
  47. static void Construct(void *)
  48. {}
  49. static void Construct(void *p, const T& source)
  50. {
  51. *(T*)p = source;
  52. }
  53. // Same as above, but allows for a different type of constructor.
  54. template <class S>
  55. static void ConstructAlt(void *p, const S& source)
  56. {
  57. *(T*)p = source;
  58. }
  59. static void ConstructArray(void*, size_t)
  60. {}
  61. static void ConstructArray(void* p, size_t count, const T& source)
  62. {
  63. uint8_t *pdata = (uint8_t*)p;
  64. for (size_t i=0; i< count; ++i, pdata += sizeof(T))
  65. *(T*)pdata = source;
  66. }
  67. static void ConstructArray(void* p, size_t count, const T* psource)
  68. {
  69. memcpy(p, psource, sizeof(T) * count);
  70. }
  71. static void Destruct(T*)
  72. {}
  73. static void DestructArray(T*, size_t)
  74. {}
  75. static void CopyArrayForward(T* dst, const T* src, size_t count)
  76. {
  77. memmove(dst, src, count * sizeof(T));
  78. }
  79. static void CopyArrayBackward(T* dst, const T* src, size_t count)
  80. {
  81. memmove(dst, src, count * sizeof(T));
  82. }
  83. static bool IsMovable()
  84. { return true; }
  85. };
  86. //-----------------------------------------------------------------------------------
  87. // ***** ConstructorMov
  88. //
  89. // Correct C++ construction and destruction for movable objects
  90. template<class T>
  91. class ConstructorMov
  92. {
  93. public:
  94. static void Construct(void* p)
  95. {
  96. OVR::Construct<T>(p);
  97. }
  98. static void Construct(void* p, const T& source)
  99. {
  100. OVR::Construct<T>(p, source);
  101. }
  102. // Same as above, but allows for a different type of constructor.
  103. template <class S>
  104. static void ConstructAlt(void* p, const S& source)
  105. {
  106. OVR::ConstructAlt<T,S>(p, source);
  107. }
  108. static void ConstructArray(void* p, size_t count)
  109. {
  110. uint8_t* pdata = (uint8_t*)p;
  111. for (size_t i = 0; i < count; ++i, pdata += sizeof(T))
  112. Construct(pdata);
  113. }
  114. static void ConstructArray(void* p, size_t count, const T& source)
  115. {
  116. uint8_t* pdata = (uint8_t*)p;
  117. for (size_t i = 0; i < count; ++i, pdata += sizeof(T))
  118. Construct(pdata, source);
  119. }
  120. static void ConstructArray(void* p, size_t count, const T* psource)
  121. {
  122. uint8_t* pdata = (uint8_t*)p;
  123. for (size_t i = 0; i < count; ++i, pdata += sizeof(T))
  124. Construct(pdata, *psource++);
  125. }
  126. static void Destruct(T* p)
  127. {
  128. p->~T();
  129. OVR_UNUSED(p); // Suppress silly MSVC warning
  130. }
  131. static void DestructArray(T* p, size_t count)
  132. {
  133. for(size_t i = 0; i < count; ++i, ++p)
  134. p->~T();
  135. }
  136. static void CopyArrayForward(T* dst, const T* src, size_t count)
  137. {
  138. memmove(dst, src, count * sizeof(T));
  139. }
  140. static void CopyArrayBackward(T* dst, const T* src, size_t count)
  141. {
  142. memmove(dst, src, count * sizeof(T));
  143. }
  144. static bool IsMovable()
  145. { return true; }
  146. };
  147. //-----------------------------------------------------------------------------------
  148. // ***** ConstructorCPP
  149. //
  150. // Correct C++ construction and destruction for movable objects
  151. template<class T>
  152. class ConstructorCPP
  153. {
  154. public:
  155. static void Construct(void* p)
  156. {
  157. OVR::Construct<T>(p);
  158. }
  159. static void Construct(void* p, const T& source)
  160. {
  161. OVR::Construct<T>(p, source);
  162. }
  163. // Same as above, but allows for a different type of constructor.
  164. template <class S>
  165. static void ConstructAlt(void* p, const S& source)
  166. {
  167. OVR::ConstructAlt<T,S>(p, source);
  168. }
  169. static void ConstructArray(void* p, size_t count)
  170. {
  171. uint8_t* pdata = (uint8_t*)p;
  172. for (size_t i = 0; i < count; ++i, pdata += sizeof(T))
  173. Construct(pdata);
  174. }
  175. static void ConstructArray(void* p, size_t count, const T& source)
  176. {
  177. uint8_t* pdata = (uint8_t*)p;
  178. for (size_t i = 0; i < count; ++i, pdata += sizeof(T))
  179. Construct(pdata, source);
  180. }
  181. static void ConstructArray(void* p, size_t count, const T* psource)
  182. {
  183. uint8_t* pdata = (uint8_t*)p;
  184. for (size_t i = 0; i < count; ++i, pdata += sizeof(T))
  185. Construct(pdata, *psource++);
  186. }
  187. static void Destruct(T* p)
  188. {
  189. p->~T();
  190. OVR_UNUSED(p); // Suppress silly MSVC warning
  191. }
  192. static void DestructArray(T* p, size_t count)
  193. {
  194. for(size_t i = 0; i < count; ++i, ++p)
  195. p->~T();
  196. }
  197. static void CopyArrayForward(T* dst, const T* src, size_t count)
  198. {
  199. for(size_t i = 0; i < count; ++i)
  200. dst[i] = src[i];
  201. }
  202. static void CopyArrayBackward(T* dst, const T* src, size_t count)
  203. {
  204. for(size_t i = count; i; --i)
  205. dst[i-1] = src[i-1];
  206. }
  207. static bool IsMovable()
  208. { return false; }
  209. };
  210. //-----------------------------------------------------------------------------------
  211. // ***** Container Allocator with movement policy
  212. //
  213. // Simple wraps as specialized allocators
  214. template<class T> struct ContainerAllocator_POD : ContainerAllocatorBase, ConstructorPOD<T> {};
  215. template<class T> struct ContainerAllocator : ContainerAllocatorBase, ConstructorMov<T> {};
  216. template<class T> struct ContainerAllocator_CPP : ContainerAllocatorBase, ConstructorCPP<T> {};
  217. } // OVR
  218. #endif