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