| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279 |
- /************************************************************************************
- PublicHeader: OVR_Kernel.h
- Filename : OVR_ContainerAllocator.h
- Content : Template allocators and constructors for containers.
- Created : September 19, 2012
- Notes :
- Copyright : Copyright 2014 Oculus VR, LLC All Rights reserved.
- Licensed under the Oculus VR Rift SDK License Version 3.2 (the "License");
- you may not use the Oculus VR Rift SDK except in compliance with the License,
- which is provided at the time of installation or download, or which
- otherwise accompanies this software in either electronic or hard copy form.
- You may obtain a copy of the License at
- http://www.oculusvr.com/licenses/LICENSE-3.2
- Unless required by applicable law or agreed to in writing, the Oculus VR SDK
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
- ************************************************************************************/
- #ifndef OVR_ContainerAllocator_h
- #define OVR_ContainerAllocator_h
- #include "OVR_Allocator.h"
- #include <string.h>
- namespace OVR {
- //-----------------------------------------------------------------------------------
- // ***** Container Allocator
- // ContainerAllocator serves as a template argument for allocations done by
- // containers, such as Array and Hash; replacing it could allow allocator
- // substitution in containers.
- class ContainerAllocatorBase
- {
- public:
- static void* Alloc(size_t size)
- { return OVR_ALLOC(size); }
-
- static void* Realloc(void* p, size_t newSize)
- { return OVR_REALLOC(p, newSize); }
-
- static void Free(void *p)
- { OVR_FREE(p); }
- };
- //-----------------------------------------------------------------------------------
- // ***** Constructors, Destructors, Copiers
- // Plain Old Data - movable, no special constructors/destructor.
- template<class T>
- class ConstructorPOD
- {
- public:
- static void Construct(void *)
- {}
-
- static void Construct(void *p, const T& source)
- {
- *(T*)p = source;
- }
- // Same as above, but allows for a different type of constructor.
- template <class S>
- static void ConstructAlt(void *p, const S& source)
- {
- *(T*)p = source;
- }
- static void ConstructArray(void*, size_t)
- {}
- static void ConstructArray(void* p, size_t count, const T& source)
- {
- uint8_t *pdata = (uint8_t*)p;
- for (size_t i=0; i< count; ++i, pdata += sizeof(T))
- *(T*)pdata = source;
- }
- static void ConstructArray(void* p, size_t count, const T* psource)
- {
- memcpy(p, psource, sizeof(T) * count);
- }
- static void Destruct(T*)
- {}
-
- static void DestructArray(T*, size_t)
- {}
- static void CopyArrayForward(T* dst, const T* src, size_t count)
- {
- memmove(dst, src, count * sizeof(T));
- }
- static void CopyArrayBackward(T* dst, const T* src, size_t count)
- {
- memmove(dst, src, count * sizeof(T));
- }
- static bool IsMovable()
- { return true; }
- };
- //-----------------------------------------------------------------------------------
- // ***** ConstructorMov
- //
- // Correct C++ construction and destruction for movable objects
- template<class T>
- class ConstructorMov
- {
- public:
- static void Construct(void* p)
- {
- OVR::Construct<T>(p);
- }
- static void Construct(void* p, const T& source)
- {
- OVR::Construct<T>(p, source);
- }
- // Same as above, but allows for a different type of constructor.
- template <class S>
- static void ConstructAlt(void* p, const S& source)
- {
- OVR::ConstructAlt<T,S>(p, source);
- }
- static void ConstructArray(void* p, size_t count)
- {
- uint8_t* pdata = (uint8_t*)p;
- for (size_t i = 0; i < count; ++i, pdata += sizeof(T))
- Construct(pdata);
- }
- static void ConstructArray(void* p, size_t count, const T& source)
- {
- uint8_t* pdata = (uint8_t*)p;
- for (size_t i = 0; i < count; ++i, pdata += sizeof(T))
- Construct(pdata, source);
- }
- static void ConstructArray(void* p, size_t count, const T* psource)
- {
- uint8_t* pdata = (uint8_t*)p;
- for (size_t i = 0; i < count; ++i, pdata += sizeof(T))
- Construct(pdata, *psource++);
- }
- static void Destruct(T* p)
- {
- p->~T();
- OVR_UNUSED(p); // Suppress silly MSVC warning
- }
- static void DestructArray(T* p, size_t count)
- {
- for(size_t i = 0; i < count; ++i, ++p)
- p->~T();
- }
- static void CopyArrayForward(T* dst, const T* src, size_t count)
- {
- memmove(dst, src, count * sizeof(T));
- }
- static void CopyArrayBackward(T* dst, const T* src, size_t count)
- {
- memmove(dst, src, count * sizeof(T));
- }
- static bool IsMovable()
- { return true; }
- };
- //-----------------------------------------------------------------------------------
- // ***** ConstructorCPP
- //
- // Correct C++ construction and destruction for movable objects
- template<class T>
- class ConstructorCPP
- {
- public:
- static void Construct(void* p)
- {
- OVR::Construct<T>(p);
- }
- static void Construct(void* p, const T& source)
- {
- OVR::Construct<T>(p, source);
- }
- // Same as above, but allows for a different type of constructor.
- template <class S>
- static void ConstructAlt(void* p, const S& source)
- {
- OVR::ConstructAlt<T,S>(p, source);
- }
- static void ConstructArray(void* p, size_t count)
- {
- uint8_t* pdata = (uint8_t*)p;
- for (size_t i = 0; i < count; ++i, pdata += sizeof(T))
- Construct(pdata);
- }
- static void ConstructArray(void* p, size_t count, const T& source)
- {
- uint8_t* pdata = (uint8_t*)p;
- for (size_t i = 0; i < count; ++i, pdata += sizeof(T))
- Construct(pdata, source);
- }
- static void ConstructArray(void* p, size_t count, const T* psource)
- {
- uint8_t* pdata = (uint8_t*)p;
- for (size_t i = 0; i < count; ++i, pdata += sizeof(T))
- Construct(pdata, *psource++);
- }
- static void Destruct(T* p)
- {
- p->~T();
- OVR_UNUSED(p); // Suppress silly MSVC warning
- }
- static void DestructArray(T* p, size_t count)
- {
- for(size_t i = 0; i < count; ++i, ++p)
- p->~T();
- }
- static void CopyArrayForward(T* dst, const T* src, size_t count)
- {
- for(size_t i = 0; i < count; ++i)
- dst[i] = src[i];
- }
- static void CopyArrayBackward(T* dst, const T* src, size_t count)
- {
- for(size_t i = count; i; --i)
- dst[i-1] = src[i-1];
- }
- static bool IsMovable()
- { return false; }
- };
- //-----------------------------------------------------------------------------------
- // ***** Container Allocator with movement policy
- //
- // Simple wraps as specialized allocators
- template<class T> struct ContainerAllocator_POD : ContainerAllocatorBase, ConstructorPOD<T> {};
- template<class T> struct ContainerAllocator : ContainerAllocatorBase, ConstructorMov<T> {};
- template<class T> struct ContainerAllocator_CPP : ContainerAllocatorBase, ConstructorCPP<T> {};
- } // OVR
- #endif
|