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