oculus1

diff libovr/Src/Kernel/OVR_Allocator.h @ 1:e2f9e4603129

added LibOVR and started a simple vr wrapper.
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 14 Sep 2013 16:14:59 +0300
parents
children b069a5c27388
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/libovr/Src/Kernel/OVR_Allocator.h	Sat Sep 14 16:14:59 2013 +0300
     1.3 @@ -0,0 +1,1 @@
     1.4 +/************************************************************************************
     1.5 
     1.6 PublicHeader:   OVR.h
     1.7 Filename    :   OVR_Allocator.h
     1.8 Content     :   Installable memory allocator
     1.9 Created     :   September 19, 2012
    1.10 Notes       : 
    1.11 
    1.12 Copyright   :   Copyright 2012 Oculus VR, Inc. All Rights reserved.
    1.13 
    1.14 Use of this software is subject to the terms of the Oculus license
    1.15 agreement provided at the time of installation or download, or which
    1.16 otherwise accompanies this software in either electronic or hard copy form.
    1.17 
    1.18 ************************************************************************************/
    1.19 
    1.20 #ifndef OVR_Allocator_h
    1.21 #define OVR_Allocator_h
    1.22 
    1.23 #include "OVR_Types.h"
    1.24 
    1.25 //-----------------------------------------------------------------------------------
    1.26 
    1.27 // ***** Disable template-unfriendly MS VC++ warnings
    1.28 #if defined(OVR_CC_MSVC)
    1.29 // Pragma to prevent long name warnings in in VC++
    1.30 #pragma warning(disable : 4503)
    1.31 #pragma warning(disable : 4786)
    1.32 // In MSVC 7.1, warning about placement new POD default initializer
    1.33 #pragma warning(disable : 4345)
    1.34 #endif
    1.35 
    1.36 // Un-define new so that placement constructors work
    1.37 #undef new
    1.38 
    1.39 
    1.40 //-----------------------------------------------------------------------------------
    1.41 // ***** Placement new overrides
    1.42 
    1.43 // Calls constructor on own memory created with "new(ptr) type"
    1.44 #ifndef __PLACEMENT_NEW_INLINE
    1.45 #define __PLACEMENT_NEW_INLINE
    1.46 
    1.47 #   if defined(OVR_CC_MWERKS) || defined(OVR_CC_BORLAND) || defined(OVR_CC_GNU)
    1.48 #      include <new>
    1.49 #   else
    1.50     // Useful on MSVC
    1.51     OVR_FORCE_INLINE void* operator new     (OVR::UPInt n, void *ptr) { OVR_UNUSED(n); return ptr; }
    1.52     OVR_FORCE_INLINE void  operator delete  (void *, void *)     { }
    1.53 #   endif
    1.54 
    1.55 #endif // __PLACEMENT_NEW_INLINE
    1.56 
    1.57 
    1.58 
    1.59 //------------------------------------------------------------------------
    1.60 // ***** Macros to redefine class new/delete operators
    1.61 
    1.62 // Types specifically declared to allow disambiguation of address in
    1.63 // class member operator new.
    1.64 
    1.65 #define OVR_MEMORY_REDEFINE_NEW_IMPL(class_name, check_delete)                          \
    1.66     void*   operator new(UPInt sz)                                                      \
    1.67     { void *p = OVR_ALLOC_DEBUG(sz, __FILE__, __LINE__); return p; }                                              \
    1.68     void*   operator new(UPInt sz, const char* file, int line)                          \
    1.69     { void* p = OVR_ALLOC_DEBUG(sz, file, line); OVR_UNUSED2(file, line); return p; }   \
    1.70     void    operator delete(void *p)                                                    \
    1.71     { check_delete(class_name, p); OVR_FREE(p); }                                       \
    1.72     void    operator delete(void *p, const char*, int)                                  \
    1.73     { check_delete(class_name, p); OVR_FREE(p); }                          
    1.74 
    1.75 #define OVR_MEMORY_DEFINE_PLACEMENT_NEW                                                 \
    1.76     void*   operator new        (UPInt n, void *ptr)    { OVR_UNUSED(n); return ptr; }  \
    1.77     void    operator delete     (void *ptr, void *ptr2) { OVR_UNUSED2(ptr,ptr2); }
    1.78 
    1.79 
    1.80 #define OVR_MEMORY_CHECK_DELETE_NONE(class_name, p)
    1.81 
    1.82 // Redefined all delete/new operators in a class without custom memory initialization
    1.83 #define OVR_MEMORY_REDEFINE_NEW(class_name) \
    1.84     OVR_MEMORY_REDEFINE_NEW_IMPL(class_name, OVR_MEMORY_CHECK_DELETE_NONE)
    1.85 
    1.86 
    1.87 namespace OVR {
    1.88 
    1.89 //-----------------------------------------------------------------------------------
    1.90 // ***** Construct / Destruct
    1.91 
    1.92 // Construct/Destruct functions are useful when new is redefined, as they can
    1.93 // be called instead of placement new constructors.
    1.94 
    1.95 
    1.96 template <class T>
    1.97 OVR_FORCE_INLINE T*  Construct(void *p)
    1.98 {
    1.99     return ::new(p) T;
   1.100 }
   1.101 
   1.102 template <class T>
   1.103 OVR_FORCE_INLINE T*  Construct(void *p, const T& source)
   1.104 {
   1.105     return ::new(p) T(source);
   1.106 }
   1.107 
   1.108 // Same as above, but allows for a different type of constructor.
   1.109 template <class T, class S>
   1.110 OVR_FORCE_INLINE T*  ConstructAlt(void *p, const S& source)
   1.111 {
   1.112     return ::new(p) T(source);
   1.113 }
   1.114 
   1.115 template <class T, class S1, class S2>
   1.116 OVR_FORCE_INLINE T*  ConstructAlt(void *p, const S1& src1, const S2& src2)
   1.117 {
   1.118     return ::new(p) T(src1, src2);
   1.119 }
   1.120 
   1.121 template <class T>
   1.122 OVR_FORCE_INLINE void ConstructArray(void *p, UPInt count)
   1.123 {
   1.124     UByte *pdata = (UByte*)p;
   1.125     for (UPInt i=0; i< count; ++i, pdata += sizeof(T))
   1.126     {
   1.127         Construct<T>(pdata);
   1.128     }
   1.129 }
   1.130 
   1.131 template <class T>
   1.132 OVR_FORCE_INLINE void ConstructArray(void *p, UPInt count, const T& source)
   1.133 {
   1.134     UByte *pdata = (UByte*)p;
   1.135     for (UPInt i=0; i< count; ++i, pdata += sizeof(T))
   1.136     {
   1.137         Construct<T>(pdata, source);
   1.138     }
   1.139 }
   1.140 
   1.141 template <class T>
   1.142 OVR_FORCE_INLINE void Destruct(T *pobj)
   1.143 {
   1.144     pobj->~T();
   1.145     OVR_UNUSED1(pobj); // Fix incorrect 'unused variable' MSVC warning.
   1.146 }
   1.147 
   1.148 template <class T>
   1.149 OVR_FORCE_INLINE void DestructArray(T *pobj, UPInt count)
   1.150 {   
   1.151     for (UPInt i=0; i<count; ++i, ++pobj)
   1.152         pobj->~T();
   1.153 }
   1.154 
   1.155 
   1.156 //-----------------------------------------------------------------------------------
   1.157 // ***** Allocator
   1.158 
   1.159 // Allocator defines a memory allocation interface that developers can override
   1.160 // to to provide memory for OVR; an instance of this class is typically created on
   1.161 // application startup and passed into System or OVR::System constructor.
   1.162 // 
   1.163 //
   1.164 // Users implementing this interface must provide three functions: Alloc, Free,
   1.165 // and Realloc. Implementations of these functions must honor the requested alignment.
   1.166 // Although arbitrary alignment requests are possible, requested alignment will
   1.167 // typically be small, such as 16 bytes or less.
   1.168 
   1.169 class Allocator
   1.170 {
   1.171     friend class System;
   1.172 public:
   1.173 
   1.174     // *** Standard Alignment Alloc/Free
   1.175 
   1.176     // Allocate memory of specified size with default alignment.
   1.177     // Alloc of size==0 will allocate a tiny block & return a valid pointer;
   1.178     // this makes it suitable for new operator.
   1.179     virtual void*   Alloc(UPInt size) = 0;
   1.180     // Same as Alloc, but provides an option of passing debug data.
   1.181     virtual void*   AllocDebug(UPInt size, const char* file, unsigned line)
   1.182     { OVR_UNUSED2(file, line); return Alloc(size); }
   1.183 
   1.184     // Reallocate memory block to a new size, copying data if necessary. Returns the pointer to
   1.185     // new memory block, which may be the same as original pointer. Will return 0 if reallocation
   1.186     // failed, in which case previous memory is still valid.
   1.187     // Realloc to decrease size will never fail.
   1.188     // Realloc of pointer == 0 is equivalent to Alloc
   1.189     // Realloc to size == 0, shrinks to the minimal size, pointer remains valid and requires Free().
   1.190     virtual void*   Realloc(void* p, UPInt newSize) = 0;
   1.191 
   1.192     // Frees memory allocated by Alloc/Realloc.
   1.193     // Free of null pointer is valid and will do nothing.
   1.194     virtual void    Free(void *p) = 0;
   1.195 
   1.196 
   1.197     // *** Standard Alignment Alloc/Free
   1.198 
   1.199     // Allocate memory of specified alignment.
   1.200     // Memory allocated with AllocAligned MUST be freed with FreeAligned.
   1.201     // Default implementation will delegate to Alloc/Free after doing rounding.
   1.202     virtual void*   AllocAligned(UPInt size, UPInt align);    
   1.203     // Frees memory allocated with AllocAligned.
   1.204     virtual void    FreeAligned(void* p);
   1.205     
   1.206     // Returns the pointer to the current globally installed Allocator instance.
   1.207     // This pointer is used for most of the memory allocations.
   1.208     static Allocator* GetInstance() { return pInstance; }
   1.209 
   1.210 
   1.211 protected:
   1.212     // onSystemShutdown is called on the allocator during System::Shutdown.
   1.213     // At this point, all allocations should've been freed.
   1.214     virtual void    onSystemShutdown() { }
   1.215 
   1.216 public:
   1.217     static  void    setInstance(Allocator* palloc)    
   1.218     {
   1.219         OVR_ASSERT((pInstance == 0) || (palloc == 0));
   1.220         pInstance = palloc;
   1.221     }
   1.222 
   1.223 private:
   1.224 
   1.225     static Allocator* pInstance;
   1.226 };
   1.227 
   1.228 
   1.229 
   1.230 //------------------------------------------------------------------------
   1.231 // ***** Allocator_SingletonSupport
   1.232 
   1.233 // Allocator_SingletonSupport is a Allocator wrapper class that implements
   1.234 // the InitSystemSingleton static function, used to create a global singleton
   1.235 // used for the OVR::System default argument initialization.
   1.236 //
   1.237 // End users implementing custom Allocator interface don't need to make use of this base
   1.238 // class; they can just create an instance of their own class on stack and pass it to System.
   1.239 
   1.240 template<class D>
   1.241 class Allocator_SingletonSupport : public Allocator
   1.242 {
   1.243     struct AllocContainer
   1.244     {        
   1.245         UPInt Data[(sizeof(D) + sizeof(UPInt)-1) / sizeof(UPInt)];
   1.246         bool  Initialized;
   1.247         AllocContainer() : Initialized(0) { }
   1.248     };
   1.249 
   1.250     AllocContainer* pContainer;
   1.251 
   1.252 public:
   1.253     Allocator_SingletonSupport() : pContainer(0) { }
   1.254 
   1.255     // Creates a singleton instance of this Allocator class used
   1.256     // on OVR_DEFAULT_ALLOCATOR during System initialization.
   1.257     static  D*  InitSystemSingleton()
   1.258     {
   1.259         static AllocContainer Container;
   1.260         OVR_ASSERT(Container.Initialized == false);
   1.261 
   1.262         Allocator_SingletonSupport<D> *presult = Construct<D>((void*)Container.Data);
   1.263         presult->pContainer   = &Container;
   1.264         Container.Initialized = true;
   1.265         return (D*)presult;
   1.266     }
   1.267 
   1.268 protected:
   1.269     virtual void onSystemShutdown()
   1.270     {
   1.271         Allocator::onSystemShutdown();
   1.272         if (pContainer)
   1.273         {
   1.274             pContainer->Initialized = false;
   1.275             Destruct((D*)this);
   1.276             pContainer = 0;
   1.277         }
   1.278     }
   1.279 };
   1.280 
   1.281 //------------------------------------------------------------------------
   1.282 // ***** Default Allocator
   1.283 
   1.284 // This allocator is created and used if no other allocator is installed.
   1.285 // Default allocator delegates to system malloc.
   1.286 
   1.287 class DefaultAllocator : public Allocator_SingletonSupport<DefaultAllocator>
   1.288 {
   1.289 public:
   1.290     virtual void*   Alloc(UPInt size);
   1.291     virtual void*   AllocDebug(UPInt size, const char* file, unsigned line);
   1.292     virtual void*   Realloc(void* p, UPInt newSize);
   1.293     virtual void    Free(void *p);
   1.294 };
   1.295 
   1.296 
   1.297 //------------------------------------------------------------------------
   1.298 // ***** Memory Allocation Macros
   1.299 
   1.300 // These macros should be used for global allocation. In the future, these
   1.301 // macros will allows allocation to be extended with debug file/line information
   1.302 // if necessary.
   1.303 
   1.304 #define OVR_REALLOC(p,s)        OVR::Allocator::GetInstance()->Realloc((p),(s))
   1.305 #define OVR_FREE(p)             OVR::Allocator::GetInstance()->Free((p))
   1.306 #define OVR_ALLOC_ALIGNED(s,a)  OVR::Allocator::GetInstance()->AllocAligned((s),(a))
   1.307 #define OVR_FREE_ALIGNED(p)     OVR::Allocator::GetInstance()->FreeAligned((p))
   1.308 
   1.309 #ifdef OVR_BUILD_DEBUG
   1.310 #define OVR_ALLOC(s)            OVR::Allocator::GetInstance()->AllocDebug((s), __FILE__, __LINE__)
   1.311 #define OVR_ALLOC_DEBUG(s,f,l)  OVR::Allocator::GetInstance()->AllocDebug((s), f, l)
   1.312 #else
   1.313 #define OVR_ALLOC(s)            OVR::Allocator::GetInstance()->Alloc((s))
   1.314 #define OVR_ALLOC_DEBUG(s,f,l)  OVR::Allocator::GetInstance()->Alloc((s))
   1.315 #endif
   1.316 
   1.317 //------------------------------------------------------------------------
   1.318 
   1.319 // Base class that overrides the new and delete operators.
   1.320 // Deriving from this class, even as a multiple base, incurs no space overhead.
   1.321 class NewOverrideBase
   1.322 {
   1.323 public:
   1.324 
   1.325     // Redefine all new & delete operators.
   1.326     OVR_MEMORY_REDEFINE_NEW(NewOverrideBase)
   1.327 };
   1.328 
   1.329 
   1.330 } // OVR
   1.331 
   1.332 
   1.333 // Redefine operator 'new' if necessary.
   1.334 #if defined(OVR_DEFINE_NEW)
   1.335 #define new OVR_DEFINE_NEW
   1.336 #endif
   1.337 
   1.338 
   1.339 #endif // OVR_Memory
   1.340 \ No newline at end of file