Bike-X  0.8
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
OVR_Allocator.cpp
Go to the documentation of this file.
1 /************************************************************************************
2 
3 Filename : OVR_Allocator.cpp
4 Content : Installable memory allocator implementation
5 Created : September 19, 2012
6 Notes :
7 
8 Copyright : Copyright 2014 Oculus VR, Inc. All Rights reserved.
9 
10 Licensed under the Oculus VR Rift SDK License Version 3.1 (the "License");
11 you may not use the Oculus VR Rift SDK except in compliance with the License,
12 which is provided at the time of installation or download, or which
13 otherwise accompanies this software in either electronic or hard copy form.
14 
15 You may obtain a copy of the License at
16 
17 http://www.oculusvr.com/licenses/LICENSE-3.1
18 
19 Unless required by applicable law or agreed to in writing, the Oculus VR SDK
20 distributed under the License is distributed on an "AS IS" BASIS,
21 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 See the License for the specific language governing permissions and
23 limitations under the License.
24 
25 ************************************************************************************/
26 
27 #include "OVR_Allocator.h"
28 #ifdef OVR_OS_MAC
29  #include <stdlib.h>
30 #else
31  #include <malloc.h>
32 #endif
33 
34 namespace OVR {
35 
36 //-----------------------------------------------------------------------------------
37 // ***** Allocator
38 
39 Allocator* Allocator::pInstance = 0;
40 
41 // Default AlignedAlloc implementation will delegate to Alloc/Free after doing rounding.
43 {
44  OVR_ASSERT((align & (align-1)) == 0);
45  align = (align > sizeof(UPInt)) ? align : sizeof(UPInt);
46  UPInt p = (UPInt)Alloc(size+align);
47  UPInt aligned = 0;
48  if (p)
49  {
50  aligned = (UPInt(p) + align-1) & ~(align-1);
51  if (aligned == p)
52  aligned += align;
53  *(((UPInt*)aligned)-1) = aligned-p;
54  }
55  return (void*)aligned;
56 }
57 
59 {
60  UPInt src = UPInt(p) - *(((UPInt*)p)-1);
61  Free((void*)src);
62 }
63 
64 
65 //------------------------------------------------------------------------
66 // ***** Default Allocator
67 
68 // This allocator is created and used if no other allocator is installed.
69 // Default allocator delegates to system malloc.
70 
72 {
73  return malloc(size);
74 }
75 void* DefaultAllocator::AllocDebug(UPInt size, const char* file, unsigned line)
76 {
77 #if defined(OVR_CC_MSVC) && defined(_CRTDBG_MAP_ALLOC)
78  return _malloc_dbg(size, _NORMAL_BLOCK, file, line);
79 #else
80  OVR_UNUSED2(file, line);
81  return malloc(size);
82 #endif
83 }
84 
85 void* DefaultAllocator::Realloc(void* p, UPInt newSize)
86 {
87  return realloc(p, newSize);
88 }
90 {
91  return free(p);
92 }
93 
94 
95 } // OVR
virtual void * Realloc(void *p, UPInt newSize)
size_t UPInt
Definition: OVR_Types.h:218
virtual void * Alloc(UPInt size)
virtual void Free(void *p)
#define OVR_ASSERT(p)
virtual void * AllocDebug(UPInt size, const char *file, unsigned line)
static Allocator * pInstance
virtual void * Alloc(UPInt size)=0
virtual void FreeAligned(void *p)
virtual void Free(void *p)=0
virtual void * AllocAligned(UPInt size, UPInt align)
#define OVR_UNUSED2(a1, a2)