oculus1

annotate 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
rev   line source
nuclear@1 1 /************************************************************************************
nuclear@1 2
nuclear@1 3 PublicHeader: OVR.h
nuclear@1 4 Filename : OVR_Allocator.h
nuclear@1 5 Content : Installable memory allocator
nuclear@1 6 Created : September 19, 2012
nuclear@1 7 Notes :
nuclear@1 8
nuclear@1 9 Copyright : Copyright 2012 Oculus VR, Inc. All Rights reserved.
nuclear@1 10
nuclear@1 11 Use of this software is subject to the terms of the Oculus license
nuclear@1 12 agreement provided at the time of installation or download, or which
nuclear@1 13 otherwise accompanies this software in either electronic or hard copy form.
nuclear@1 14
nuclear@1 15 ************************************************************************************/
nuclear@1 16
nuclear@1 17 #ifndef OVR_Allocator_h
nuclear@1 18 #define OVR_Allocator_h
nuclear@1 19
nuclear@1 20 #include "OVR_Types.h"
nuclear@1 21
nuclear@1 22 //-----------------------------------------------------------------------------------
nuclear@1 23
nuclear@1 24 // ***** Disable template-unfriendly MS VC++ warnings
nuclear@1 25 #if defined(OVR_CC_MSVC)
nuclear@1 26 // Pragma to prevent long name warnings in in VC++
nuclear@1 27 #pragma warning(disable : 4503)
nuclear@1 28 #pragma warning(disable : 4786)
nuclear@1 29 // In MSVC 7.1, warning about placement new POD default initializer
nuclear@1 30 #pragma warning(disable : 4345)
nuclear@1 31 #endif
nuclear@1 32
nuclear@1 33 // Un-define new so that placement constructors work
nuclear@1 34 #undef new
nuclear@1 35
nuclear@1 36
nuclear@1 37 //-----------------------------------------------------------------------------------
nuclear@1 38 // ***** Placement new overrides
nuclear@1 39
nuclear@1 40 // Calls constructor on own memory created with "new(ptr) type"
nuclear@1 41 #ifndef __PLACEMENT_NEW_INLINE
nuclear@1 42 #define __PLACEMENT_NEW_INLINE
nuclear@1 43
nuclear@1 44 # if defined(OVR_CC_MWERKS) || defined(OVR_CC_BORLAND) || defined(OVR_CC_GNU)
nuclear@1 45 # include <new>
nuclear@1 46 # else
nuclear@1 47 // Useful on MSVC
nuclear@1 48 OVR_FORCE_INLINE void* operator new (OVR::UPInt n, void *ptr) { OVR_UNUSED(n); return ptr; }
nuclear@1 49 OVR_FORCE_INLINE void operator delete (void *, void *) { }
nuclear@1 50 # endif
nuclear@1 51
nuclear@1 52 #endif // __PLACEMENT_NEW_INLINE
nuclear@1 53
nuclear@1 54
nuclear@1 55
nuclear@1 56 //------------------------------------------------------------------------
nuclear@1 57 // ***** Macros to redefine class new/delete operators
nuclear@1 58
nuclear@1 59 // Types specifically declared to allow disambiguation of address in
nuclear@1 60 // class member operator new.
nuclear@1 61
nuclear@1 62 #define OVR_MEMORY_REDEFINE_NEW_IMPL(class_name, check_delete) \
nuclear@1 63 void* operator new(UPInt sz) \
nuclear@1 64 { void *p = OVR_ALLOC_DEBUG(sz, __FILE__, __LINE__); return p; } \
nuclear@1 65 void* operator new(UPInt sz, const char* file, int line) \
nuclear@1 66 { void* p = OVR_ALLOC_DEBUG(sz, file, line); OVR_UNUSED2(file, line); return p; } \
nuclear@1 67 void operator delete(void *p) \
nuclear@1 68 { check_delete(class_name, p); OVR_FREE(p); } \
nuclear@1 69 void operator delete(void *p, const char*, int) \
nuclear@1 70 { check_delete(class_name, p); OVR_FREE(p); }
nuclear@1 71
nuclear@1 72 #define OVR_MEMORY_DEFINE_PLACEMENT_NEW \
nuclear@1 73 void* operator new (UPInt n, void *ptr) { OVR_UNUSED(n); return ptr; } \
nuclear@1 74 void operator delete (void *ptr, void *ptr2) { OVR_UNUSED2(ptr,ptr2); }
nuclear@1 75
nuclear@1 76
nuclear@1 77 #define OVR_MEMORY_CHECK_DELETE_NONE(class_name, p)
nuclear@1 78
nuclear@1 79 // Redefined all delete/new operators in a class without custom memory initialization
nuclear@1 80 #define OVR_MEMORY_REDEFINE_NEW(class_name) \
nuclear@1 81 OVR_MEMORY_REDEFINE_NEW_IMPL(class_name, OVR_MEMORY_CHECK_DELETE_NONE)
nuclear@1 82
nuclear@1 83
nuclear@1 84 namespace OVR {
nuclear@1 85
nuclear@1 86 //-----------------------------------------------------------------------------------
nuclear@1 87 // ***** Construct / Destruct
nuclear@1 88
nuclear@1 89 // Construct/Destruct functions are useful when new is redefined, as they can
nuclear@1 90 // be called instead of placement new constructors.
nuclear@1 91
nuclear@1 92
nuclear@1 93 template <class T>
nuclear@1 94 OVR_FORCE_INLINE T* Construct(void *p)
nuclear@1 95 {
nuclear@1 96 return ::new(p) T;
nuclear@1 97 }
nuclear@1 98
nuclear@1 99 template <class T>
nuclear@1 100 OVR_FORCE_INLINE T* Construct(void *p, const T& source)
nuclear@1 101 {
nuclear@1 102 return ::new(p) T(source);
nuclear@1 103 }
nuclear@1 104
nuclear@1 105 // Same as above, but allows for a different type of constructor.
nuclear@1 106 template <class T, class S>
nuclear@1 107 OVR_FORCE_INLINE T* ConstructAlt(void *p, const S& source)
nuclear@1 108 {
nuclear@1 109 return ::new(p) T(source);
nuclear@1 110 }
nuclear@1 111
nuclear@1 112 template <class T, class S1, class S2>
nuclear@1 113 OVR_FORCE_INLINE T* ConstructAlt(void *p, const S1& src1, const S2& src2)
nuclear@1 114 {
nuclear@1 115 return ::new(p) T(src1, src2);
nuclear@1 116 }
nuclear@1 117
nuclear@1 118 template <class T>
nuclear@1 119 OVR_FORCE_INLINE void ConstructArray(void *p, UPInt count)
nuclear@1 120 {
nuclear@1 121 UByte *pdata = (UByte*)p;
nuclear@1 122 for (UPInt i=0; i< count; ++i, pdata += sizeof(T))
nuclear@1 123 {
nuclear@1 124 Construct<T>(pdata);
nuclear@1 125 }
nuclear@1 126 }
nuclear@1 127
nuclear@1 128 template <class T>
nuclear@1 129 OVR_FORCE_INLINE void ConstructArray(void *p, UPInt count, const T& source)
nuclear@1 130 {
nuclear@1 131 UByte *pdata = (UByte*)p;
nuclear@1 132 for (UPInt i=0; i< count; ++i, pdata += sizeof(T))
nuclear@1 133 {
nuclear@1 134 Construct<T>(pdata, source);
nuclear@1 135 }
nuclear@1 136 }
nuclear@1 137
nuclear@1 138 template <class T>
nuclear@1 139 OVR_FORCE_INLINE void Destruct(T *pobj)
nuclear@1 140 {
nuclear@1 141 pobj->~T();
nuclear@1 142 OVR_UNUSED1(pobj); // Fix incorrect 'unused variable' MSVC warning.
nuclear@1 143 }
nuclear@1 144
nuclear@1 145 template <class T>
nuclear@1 146 OVR_FORCE_INLINE void DestructArray(T *pobj, UPInt count)
nuclear@1 147 {
nuclear@1 148 for (UPInt i=0; i<count; ++i, ++pobj)
nuclear@1 149 pobj->~T();
nuclear@1 150 }
nuclear@1 151
nuclear@1 152
nuclear@1 153 //-----------------------------------------------------------------------------------
nuclear@1 154 // ***** Allocator
nuclear@1 155
nuclear@1 156 // Allocator defines a memory allocation interface that developers can override
nuclear@1 157 // to to provide memory for OVR; an instance of this class is typically created on
nuclear@1 158 // application startup and passed into System or OVR::System constructor.
nuclear@1 159 //
nuclear@1 160 //
nuclear@1 161 // Users implementing this interface must provide three functions: Alloc, Free,
nuclear@1 162 // and Realloc. Implementations of these functions must honor the requested alignment.
nuclear@1 163 // Although arbitrary alignment requests are possible, requested alignment will
nuclear@1 164 // typically be small, such as 16 bytes or less.
nuclear@1 165
nuclear@1 166 class Allocator
nuclear@1 167 {
nuclear@1 168 friend class System;
nuclear@1 169 public:
nuclear@1 170
nuclear@1 171 // *** Standard Alignment Alloc/Free
nuclear@1 172
nuclear@1 173 // Allocate memory of specified size with default alignment.
nuclear@1 174 // Alloc of size==0 will allocate a tiny block & return a valid pointer;
nuclear@1 175 // this makes it suitable for new operator.
nuclear@1 176 virtual void* Alloc(UPInt size) = 0;
nuclear@1 177 // Same as Alloc, but provides an option of passing debug data.
nuclear@1 178 virtual void* AllocDebug(UPInt size, const char* file, unsigned line)
nuclear@1 179 { OVR_UNUSED2(file, line); return Alloc(size); }
nuclear@1 180
nuclear@1 181 // Reallocate memory block to a new size, copying data if necessary. Returns the pointer to
nuclear@1 182 // new memory block, which may be the same as original pointer. Will return 0 if reallocation
nuclear@1 183 // failed, in which case previous memory is still valid.
nuclear@1 184 // Realloc to decrease size will never fail.
nuclear@1 185 // Realloc of pointer == 0 is equivalent to Alloc
nuclear@1 186 // Realloc to size == 0, shrinks to the minimal size, pointer remains valid and requires Free().
nuclear@1 187 virtual void* Realloc(void* p, UPInt newSize) = 0;
nuclear@1 188
nuclear@1 189 // Frees memory allocated by Alloc/Realloc.
nuclear@1 190 // Free of null pointer is valid and will do nothing.
nuclear@1 191 virtual void Free(void *p) = 0;
nuclear@1 192
nuclear@1 193
nuclear@1 194 // *** Standard Alignment Alloc/Free
nuclear@1 195
nuclear@1 196 // Allocate memory of specified alignment.
nuclear@1 197 // Memory allocated with AllocAligned MUST be freed with FreeAligned.
nuclear@1 198 // Default implementation will delegate to Alloc/Free after doing rounding.
nuclear@1 199 virtual void* AllocAligned(UPInt size, UPInt align);
nuclear@1 200 // Frees memory allocated with AllocAligned.
nuclear@1 201 virtual void FreeAligned(void* p);
nuclear@1 202
nuclear@1 203 // Returns the pointer to the current globally installed Allocator instance.
nuclear@1 204 // This pointer is used for most of the memory allocations.
nuclear@1 205 static Allocator* GetInstance() { return pInstance; }
nuclear@1 206
nuclear@1 207
nuclear@1 208 protected:
nuclear@1 209 // onSystemShutdown is called on the allocator during System::Shutdown.
nuclear@1 210 // At this point, all allocations should've been freed.
nuclear@1 211 virtual void onSystemShutdown() { }
nuclear@1 212
nuclear@1 213 public:
nuclear@1 214 static void setInstance(Allocator* palloc)
nuclear@1 215 {
nuclear@1 216 OVR_ASSERT((pInstance == 0) || (palloc == 0));
nuclear@1 217 pInstance = palloc;
nuclear@1 218 }
nuclear@1 219
nuclear@1 220 private:
nuclear@1 221
nuclear@1 222 static Allocator* pInstance;
nuclear@1 223 };
nuclear@1 224
nuclear@1 225
nuclear@1 226
nuclear@1 227 //------------------------------------------------------------------------
nuclear@1 228 // ***** Allocator_SingletonSupport
nuclear@1 229
nuclear@1 230 // Allocator_SingletonSupport is a Allocator wrapper class that implements
nuclear@1 231 // the InitSystemSingleton static function, used to create a global singleton
nuclear@1 232 // used for the OVR::System default argument initialization.
nuclear@1 233 //
nuclear@1 234 // End users implementing custom Allocator interface don't need to make use of this base
nuclear@1 235 // class; they can just create an instance of their own class on stack and pass it to System.
nuclear@1 236
nuclear@1 237 template<class D>
nuclear@1 238 class Allocator_SingletonSupport : public Allocator
nuclear@1 239 {
nuclear@1 240 struct AllocContainer
nuclear@1 241 {
nuclear@1 242 UPInt Data[(sizeof(D) + sizeof(UPInt)-1) / sizeof(UPInt)];
nuclear@1 243 bool Initialized;
nuclear@1 244 AllocContainer() : Initialized(0) { }
nuclear@1 245 };
nuclear@1 246
nuclear@1 247 AllocContainer* pContainer;
nuclear@1 248
nuclear@1 249 public:
nuclear@1 250 Allocator_SingletonSupport() : pContainer(0) { }
nuclear@1 251
nuclear@1 252 // Creates a singleton instance of this Allocator class used
nuclear@1 253 // on OVR_DEFAULT_ALLOCATOR during System initialization.
nuclear@1 254 static D* InitSystemSingleton()
nuclear@1 255 {
nuclear@1 256 static AllocContainer Container;
nuclear@1 257 OVR_ASSERT(Container.Initialized == false);
nuclear@1 258
nuclear@1 259 Allocator_SingletonSupport<D> *presult = Construct<D>((void*)Container.Data);
nuclear@1 260 presult->pContainer = &Container;
nuclear@1 261 Container.Initialized = true;
nuclear@1 262 return (D*)presult;
nuclear@1 263 }
nuclear@1 264
nuclear@1 265 protected:
nuclear@1 266 virtual void onSystemShutdown()
nuclear@1 267 {
nuclear@1 268 Allocator::onSystemShutdown();
nuclear@1 269 if (pContainer)
nuclear@1 270 {
nuclear@1 271 pContainer->Initialized = false;
nuclear@1 272 Destruct((D*)this);
nuclear@1 273 pContainer = 0;
nuclear@1 274 }
nuclear@1 275 }
nuclear@1 276 };
nuclear@1 277
nuclear@1 278 //------------------------------------------------------------------------
nuclear@1 279 // ***** Default Allocator
nuclear@1 280
nuclear@1 281 // This allocator is created and used if no other allocator is installed.
nuclear@1 282 // Default allocator delegates to system malloc.
nuclear@1 283
nuclear@1 284 class DefaultAllocator : public Allocator_SingletonSupport<DefaultAllocator>
nuclear@1 285 {
nuclear@1 286 public:
nuclear@1 287 virtual void* Alloc(UPInt size);
nuclear@1 288 virtual void* AllocDebug(UPInt size, const char* file, unsigned line);
nuclear@1 289 virtual void* Realloc(void* p, UPInt newSize);
nuclear@1 290 virtual void Free(void *p);
nuclear@1 291 };
nuclear@1 292
nuclear@1 293
nuclear@1 294 //------------------------------------------------------------------------
nuclear@1 295 // ***** Memory Allocation Macros
nuclear@1 296
nuclear@1 297 // These macros should be used for global allocation. In the future, these
nuclear@1 298 // macros will allows allocation to be extended with debug file/line information
nuclear@1 299 // if necessary.
nuclear@1 300
nuclear@1 301 #define OVR_REALLOC(p,s) OVR::Allocator::GetInstance()->Realloc((p),(s))
nuclear@1 302 #define OVR_FREE(p) OVR::Allocator::GetInstance()->Free((p))
nuclear@1 303 #define OVR_ALLOC_ALIGNED(s,a) OVR::Allocator::GetInstance()->AllocAligned((s),(a))
nuclear@1 304 #define OVR_FREE_ALIGNED(p) OVR::Allocator::GetInstance()->FreeAligned((p))
nuclear@1 305
nuclear@1 306 #ifdef OVR_BUILD_DEBUG
nuclear@1 307 #define OVR_ALLOC(s) OVR::Allocator::GetInstance()->AllocDebug((s), __FILE__, __LINE__)
nuclear@1 308 #define OVR_ALLOC_DEBUG(s,f,l) OVR::Allocator::GetInstance()->AllocDebug((s), f, l)
nuclear@1 309 #else
nuclear@1 310 #define OVR_ALLOC(s) OVR::Allocator::GetInstance()->Alloc((s))
nuclear@1 311 #define OVR_ALLOC_DEBUG(s,f,l) OVR::Allocator::GetInstance()->Alloc((s))
nuclear@1 312 #endif
nuclear@1 313
nuclear@1 314 //------------------------------------------------------------------------
nuclear@1 315
nuclear@1 316 // Base class that overrides the new and delete operators.
nuclear@1 317 // Deriving from this class, even as a multiple base, incurs no space overhead.
nuclear@1 318 class NewOverrideBase
nuclear@1 319 {
nuclear@1 320 public:
nuclear@1 321
nuclear@1 322 // Redefine all new & delete operators.
nuclear@1 323 OVR_MEMORY_REDEFINE_NEW(NewOverrideBase)
nuclear@1 324 };
nuclear@1 325
nuclear@1 326
nuclear@1 327 } // OVR
nuclear@1 328
nuclear@1 329
nuclear@1 330 // Redefine operator 'new' if necessary.
nuclear@1 331 #if defined(OVR_DEFINE_NEW)
nuclear@1 332 #define new OVR_DEFINE_NEW
nuclear@1 333 #endif
nuclear@1 334
nuclear@1 335
nuclear@1 336 #endif // OVR_Memory