ovr_sdk

diff LibOVR/Src/Kernel/OVR_ContainerAllocator.h @ 0:1b39a1b46319

initial 0.4.4
author John Tsiombikas <nuclear@member.fsf.org>
date Wed, 14 Jan 2015 06:51:16 +0200
parents
children
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/LibOVR/Src/Kernel/OVR_ContainerAllocator.h	Wed Jan 14 06:51:16 2015 +0200
     1.3 @@ -0,0 +1,267 @@
     1.4 +/************************************************************************************
     1.5 +
     1.6 +PublicHeader:   OVR_Kernel.h
     1.7 +Filename    :   OVR_ContainerAllocator.h
     1.8 +Content     :   Template allocators and constructors for containers.
     1.9 +Created     :   September 19, 2012
    1.10 +Notes       : 
    1.11 +
    1.12 +Copyright   :   Copyright 2014 Oculus VR, LLC All Rights reserved.
    1.13 +
    1.14 +Licensed under the Oculus VR Rift SDK License Version 3.2 (the "License"); 
    1.15 +you may not use the Oculus VR Rift SDK except in compliance with the License, 
    1.16 +which is provided at the time of installation or download, or which 
    1.17 +otherwise accompanies this software in either electronic or hard copy form.
    1.18 +
    1.19 +You may obtain a copy of the License at
    1.20 +
    1.21 +http://www.oculusvr.com/licenses/LICENSE-3.2 
    1.22 +
    1.23 +Unless required by applicable law or agreed to in writing, the Oculus VR SDK 
    1.24 +distributed under the License is distributed on an "AS IS" BASIS,
    1.25 +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    1.26 +See the License for the specific language governing permissions and
    1.27 +limitations under the License.
    1.28 +
    1.29 +************************************************************************************/
    1.30 +
    1.31 +#ifndef OVR_ContainerAllocator_h
    1.32 +#define OVR_ContainerAllocator_h
    1.33 +
    1.34 +#include "OVR_Allocator.h"
    1.35 +#include <string.h>
    1.36 +
    1.37 +
    1.38 +namespace OVR {
    1.39 +
    1.40 +
    1.41 +//-----------------------------------------------------------------------------------
    1.42 +// ***** Container Allocator
    1.43 +
    1.44 +// ContainerAllocator serves as a template argument for allocations done by
    1.45 +// containers, such as Array and Hash; replacing it could allow allocator
    1.46 +// substitution in containers.
    1.47 +
    1.48 +class ContainerAllocatorBase
    1.49 +{
    1.50 +public:
    1.51 +    static void* Alloc(size_t size)                { return OVR_ALLOC(size); }
    1.52 +    static void* Realloc(void* p, size_t newSize)  { return OVR_REALLOC(p, newSize); }
    1.53 +    static void  Free(void *p)                    { OVR_FREE(p); }
    1.54 +};
    1.55 +
    1.56 +
    1.57 +
    1.58 +//-----------------------------------------------------------------------------------
    1.59 +// ***** Constructors, Destructors, Copiers
    1.60 +
    1.61 +// Plain Old Data - movable, no special constructors/destructor.
    1.62 +template<class T> 
    1.63 +class ConstructorPOD
    1.64 +{
    1.65 +public:
    1.66 +    static void Construct(void *) {}
    1.67 +    static void Construct(void *p, const T& source) 
    1.68 +    { 
    1.69 +        *(T*)p = source;
    1.70 +    }
    1.71 +
    1.72 +    // Same as above, but allows for a different type of constructor.
    1.73 +    template <class S> 
    1.74 +    static void ConstructAlt(void *p, const S& source)
    1.75 +    {
    1.76 +        *(T*)p = source;
    1.77 +    }
    1.78 +
    1.79 +    static void ConstructArray(void*, size_t) {}
    1.80 +
    1.81 +    static void ConstructArray(void* p, size_t count, const T& source)
    1.82 +    {
    1.83 +        uint8_t *pdata = (uint8_t*)p;
    1.84 +        for (size_t i=0; i< count; ++i, pdata += sizeof(T))
    1.85 +            *(T*)pdata = source;
    1.86 +    }
    1.87 +
    1.88 +    static void ConstructArray(void* p, size_t count, const T* psource)
    1.89 +    {
    1.90 +        memcpy(p, psource, sizeof(T) * count);
    1.91 +    }
    1.92 +
    1.93 +    static void Destruct(T*) {}
    1.94 +    static void DestructArray(T*, size_t) {}
    1.95 +
    1.96 +    static void CopyArrayForward(T* dst, const T* src, size_t count)
    1.97 +    {
    1.98 +        memmove(dst, src, count * sizeof(T));
    1.99 +    }
   1.100 +
   1.101 +    static void CopyArrayBackward(T* dst, const T* src, size_t count)
   1.102 +    {
   1.103 +        memmove(dst, src, count * sizeof(T));
   1.104 +    }
   1.105 +
   1.106 +    static bool IsMovable() { return true; }
   1.107 +};
   1.108 +
   1.109 +
   1.110 +//-----------------------------------------------------------------------------------
   1.111 +// ***** ConstructorMov
   1.112 +//
   1.113 +// Correct C++ construction and destruction for movable objects
   1.114 +template<class T> 
   1.115 +class ConstructorMov
   1.116 +{
   1.117 +public:
   1.118 +    static void Construct(void* p) 
   1.119 +    { 
   1.120 +        OVR::Construct<T>(p);
   1.121 +    }
   1.122 +
   1.123 +    static void Construct(void* p, const T& source) 
   1.124 +    { 
   1.125 +        OVR::Construct<T>(p, source);
   1.126 +    }
   1.127 +
   1.128 +    // Same as above, but allows for a different type of constructor.
   1.129 +    template <class S> 
   1.130 +    static void ConstructAlt(void* p, const S& source)
   1.131 +    {
   1.132 +        OVR::ConstructAlt<T,S>(p, source);
   1.133 +    }
   1.134 +
   1.135 +    static void ConstructArray(void* p, size_t count)
   1.136 +    {
   1.137 +        uint8_t* pdata = (uint8_t*)p;
   1.138 +        for (size_t i=0; i< count; ++i, pdata += sizeof(T))
   1.139 +            Construct(pdata);
   1.140 +    }
   1.141 +
   1.142 +    static void ConstructArray(void* p, size_t count, const T& source)
   1.143 +    {
   1.144 +        uint8_t* pdata = (uint8_t*)p;
   1.145 +        for (size_t i=0; i< count; ++i, pdata += sizeof(T))
   1.146 +            Construct(pdata, source);
   1.147 +    }
   1.148 +
   1.149 +    static void ConstructArray(void* p, size_t count, const T* psource)
   1.150 +    {
   1.151 +        uint8_t* pdata = (uint8_t*)p;
   1.152 +        for (size_t i=0; i< count; ++i, pdata += sizeof(T))
   1.153 +            Construct(pdata, *psource++);
   1.154 +    }
   1.155 +
   1.156 +    static void Destruct(T* p)
   1.157 +    {
   1.158 +        p->~T();
   1.159 +        OVR_UNUSED(p); // Suppress silly MSVC warning
   1.160 +    }
   1.161 +
   1.162 +    static void DestructArray(T* p, size_t count)
   1.163 +    {   
   1.164 +        p += count - 1;
   1.165 +        for (size_t i=0; i<count; ++i, --p)
   1.166 +            p->~T();
   1.167 +    }
   1.168 +
   1.169 +    static void CopyArrayForward(T* dst, const T* src, size_t count)
   1.170 +    {
   1.171 +        memmove(dst, src, count * sizeof(T));
   1.172 +    }
   1.173 +
   1.174 +    static void CopyArrayBackward(T* dst, const T* src, size_t count)
   1.175 +    {
   1.176 +        memmove(dst, src, count * sizeof(T));
   1.177 +    }
   1.178 +
   1.179 +    static bool IsMovable() { return true; }
   1.180 +};
   1.181 +
   1.182 +
   1.183 +//-----------------------------------------------------------------------------------
   1.184 +// ***** ConstructorCPP
   1.185 +//
   1.186 +// Correct C++ construction and destruction for movable objects
   1.187 +template<class T> 
   1.188 +class ConstructorCPP
   1.189 +{
   1.190 +public:
   1.191 +    static void Construct(void* p) 
   1.192 +    { 
   1.193 +        OVR::Construct<T>(p);        
   1.194 +    }
   1.195 +
   1.196 +    static void Construct(void* p, const T& source) 
   1.197 +    { 
   1.198 +        OVR::Construct<T>(p, source);        
   1.199 +    }
   1.200 +
   1.201 +    // Same as above, but allows for a different type of constructor.
   1.202 +    template <class S> 
   1.203 +    static void ConstructAlt(void* p, const S& source)
   1.204 +    {
   1.205 +        OVR::ConstructAlt<T,S>(p, source);        
   1.206 +    }
   1.207 +
   1.208 +    static void ConstructArray(void* p, size_t count)
   1.209 +    {
   1.210 +        uint8_t* pdata = (uint8_t*)p;
   1.211 +        for (size_t i=0; i< count; ++i, pdata += sizeof(T))
   1.212 +            Construct(pdata);
   1.213 +    }
   1.214 +
   1.215 +    static void ConstructArray(void* p, size_t count, const T& source)
   1.216 +    {
   1.217 +        uint8_t* pdata = (uint8_t*)p;
   1.218 +        for (size_t i=0; i< count; ++i, pdata += sizeof(T))
   1.219 +            Construct(pdata, source);
   1.220 +    }
   1.221 +
   1.222 +    static void ConstructArray(void* p, size_t count, const T* psource)
   1.223 +    {
   1.224 +        uint8_t* pdata = (uint8_t*)p;
   1.225 +        for (size_t i=0; i< count; ++i, pdata += sizeof(T))
   1.226 +            Construct(pdata, *psource++);
   1.227 +    }
   1.228 +
   1.229 +    static void Destruct(T* p)
   1.230 +    {
   1.231 +        p->~T();
   1.232 +        OVR_UNUSED(p); // Suppress silly MSVC warning
   1.233 +    }
   1.234 +
   1.235 +    static void DestructArray(T* p, size_t count)
   1.236 +    {   
   1.237 +        p += count - 1;
   1.238 +        for (size_t i=0; i<count; ++i, --p)
   1.239 +            p->~T();
   1.240 +    }
   1.241 +
   1.242 +    static void CopyArrayForward(T* dst, const T* src, size_t count)
   1.243 +    {
   1.244 +        for(size_t i = 0; i < count; ++i)
   1.245 +            dst[i] = src[i];
   1.246 +    }
   1.247 +
   1.248 +    static void CopyArrayBackward(T* dst, const T* src, size_t count)
   1.249 +    {
   1.250 +        for(size_t i = count; i; --i)
   1.251 +            dst[i-1] = src[i-1];
   1.252 +    }
   1.253 +
   1.254 +    static bool IsMovable() { return false; }
   1.255 +};
   1.256 +
   1.257 +
   1.258 +//-----------------------------------------------------------------------------------
   1.259 +// ***** Container Allocator with movement policy
   1.260 +//
   1.261 +// Simple wraps as specialized allocators
   1.262 +template<class T> struct ContainerAllocator_POD : ContainerAllocatorBase, ConstructorPOD<T> {};
   1.263 +template<class T> struct ContainerAllocator     : ContainerAllocatorBase, ConstructorMov<T> {};
   1.264 +template<class T> struct ContainerAllocator_CPP : ContainerAllocatorBase, ConstructorCPP<T> {};
   1.265 +
   1.266 +
   1.267 +} // OVR
   1.268 +
   1.269 +
   1.270 +#endif