oculus1
diff libovr/Src/Kernel/OVR_Allocator.cpp @ 3:b069a5c27388
added a couple more stuff, fixed all the LibOVR line endings
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sun, 15 Sep 2013 04:10:05 +0300 |
parents | e2f9e4603129 |
children |
line diff
1.1 --- a/libovr/Src/Kernel/OVR_Allocator.cpp Sat Sep 14 17:51:03 2013 +0300 1.2 +++ b/libovr/Src/Kernel/OVR_Allocator.cpp Sun Sep 15 04:10:05 2013 +0300 1.3 @@ -1,1 +1,84 @@ 1.4 -/************************************************************************************ 1.5 1.6 Filename : OVR_Allocator.cpp 1.7 Content : Installable memory allocator implementation 1.8 Created : September 19, 2012 1.9 Notes : 1.10 1.11 Copyright : Copyright 2012 Oculus VR, Inc. All Rights reserved. 1.12 1.13 Use of this software is subject to the terms of the Oculus license 1.14 agreement provided at the time of installation or download, or which 1.15 otherwise accompanies this software in either electronic or hard copy form. 1.16 1.17 ************************************************************************************/ 1.18 1.19 #include "OVR_Allocator.h" 1.20 #ifdef OVR_OS_MAC 1.21 #include <stdlib.h> 1.22 #else 1.23 #include <malloc.h> 1.24 #endif 1.25 1.26 namespace OVR { 1.27 1.28 //----------------------------------------------------------------------------------- 1.29 // ***** Allocator 1.30 1.31 Allocator* Allocator::pInstance = 0; 1.32 1.33 // Default AlignedAlloc implementation will delegate to Alloc/Free after doing rounding. 1.34 void* Allocator::AllocAligned(UPInt size, UPInt align) 1.35 { 1.36 OVR_ASSERT((align & (align-1)) == 0); 1.37 align = (align > sizeof(UPInt)) ? align : sizeof(UPInt); 1.38 UPInt p = (UPInt)Alloc(size+align); 1.39 UPInt aligned = 0; 1.40 if (p) 1.41 { 1.42 aligned = (UPInt(p) + align-1) & ~(align-1); 1.43 if (aligned == p) 1.44 aligned += align; 1.45 *(((UPInt*)aligned)-1) = aligned-p; 1.46 } 1.47 return (void*)aligned; 1.48 } 1.49 1.50 void Allocator::FreeAligned(void* p) 1.51 { 1.52 UPInt src = UPInt(p) - *(((UPInt*)p)-1); 1.53 Free((void*)src); 1.54 } 1.55 1.56 1.57 //------------------------------------------------------------------------ 1.58 // ***** Default Allocator 1.59 1.60 // This allocator is created and used if no other allocator is installed. 1.61 // Default allocator delegates to system malloc. 1.62 1.63 void* DefaultAllocator::Alloc(UPInt size) 1.64 { 1.65 return malloc(size); 1.66 } 1.67 void* DefaultAllocator::AllocDebug(UPInt size, const char* file, unsigned line) 1.68 { 1.69 #if defined(OVR_CC_MSVC) && defined(_CRTDBG_MAP_ALLOC) 1.70 return _malloc_dbg(size, _NORMAL_BLOCK, file, line); 1.71 #else 1.72 OVR_UNUSED2(file, line); 1.73 return malloc(size); 1.74 #endif 1.75 } 1.76 1.77 void* DefaultAllocator::Realloc(void* p, UPInt newSize) 1.78 { 1.79 return realloc(p, newSize); 1.80 } 1.81 void DefaultAllocator::Free(void *p) 1.82 { 1.83 return free(p); 1.84 } 1.85 1.86 1.87 } // OVR 1.88 \ No newline at end of file 1.89 +/************************************************************************************ 1.90 + 1.91 +Filename : OVR_Allocator.cpp 1.92 +Content : Installable memory allocator implementation 1.93 +Created : September 19, 2012 1.94 +Notes : 1.95 + 1.96 +Copyright : Copyright 2012 Oculus VR, Inc. All Rights reserved. 1.97 + 1.98 +Use of this software is subject to the terms of the Oculus license 1.99 +agreement provided at the time of installation or download, or which 1.100 +otherwise accompanies this software in either electronic or hard copy form. 1.101 + 1.102 +************************************************************************************/ 1.103 + 1.104 +#include "OVR_Allocator.h" 1.105 +#ifdef OVR_OS_MAC 1.106 + #include <stdlib.h> 1.107 +#else 1.108 + #include <malloc.h> 1.109 +#endif 1.110 + 1.111 +namespace OVR { 1.112 + 1.113 +//----------------------------------------------------------------------------------- 1.114 +// ***** Allocator 1.115 + 1.116 +Allocator* Allocator::pInstance = 0; 1.117 + 1.118 +// Default AlignedAlloc implementation will delegate to Alloc/Free after doing rounding. 1.119 +void* Allocator::AllocAligned(UPInt size, UPInt align) 1.120 +{ 1.121 + OVR_ASSERT((align & (align-1)) == 0); 1.122 + align = (align > sizeof(UPInt)) ? align : sizeof(UPInt); 1.123 + UPInt p = (UPInt)Alloc(size+align); 1.124 + UPInt aligned = 0; 1.125 + if (p) 1.126 + { 1.127 + aligned = (UPInt(p) + align-1) & ~(align-1); 1.128 + if (aligned == p) 1.129 + aligned += align; 1.130 + *(((UPInt*)aligned)-1) = aligned-p; 1.131 + } 1.132 + return (void*)aligned; 1.133 +} 1.134 + 1.135 +void Allocator::FreeAligned(void* p) 1.136 +{ 1.137 + UPInt src = UPInt(p) - *(((UPInt*)p)-1); 1.138 + Free((void*)src); 1.139 +} 1.140 + 1.141 + 1.142 +//------------------------------------------------------------------------ 1.143 +// ***** Default Allocator 1.144 + 1.145 +// This allocator is created and used if no other allocator is installed. 1.146 +// Default allocator delegates to system malloc. 1.147 + 1.148 +void* DefaultAllocator::Alloc(UPInt size) 1.149 +{ 1.150 + return malloc(size); 1.151 +} 1.152 +void* DefaultAllocator::AllocDebug(UPInt size, const char* file, unsigned line) 1.153 +{ 1.154 +#if defined(OVR_CC_MSVC) && defined(_CRTDBG_MAP_ALLOC) 1.155 + return _malloc_dbg(size, _NORMAL_BLOCK, file, line); 1.156 +#else 1.157 + OVR_UNUSED2(file, line); 1.158 + return malloc(size); 1.159 +#endif 1.160 +} 1.161 + 1.162 +void* DefaultAllocator::Realloc(void* p, UPInt newSize) 1.163 +{ 1.164 + return realloc(p, newSize); 1.165 +} 1.166 +void DefaultAllocator::Free(void *p) 1.167 +{ 1.168 + return free(p); 1.169 +} 1.170 + 1.171 + 1.172 +} // OVR