oculus1

annotate libovr/Src/Kernel/OVR_Allocator.cpp @ 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 Filename : OVR_Allocator.cpp
nuclear@1 4 Content : Installable memory allocator implementation
nuclear@1 5 Created : September 19, 2012
nuclear@1 6 Notes :
nuclear@1 7
nuclear@1 8 Copyright : Copyright 2012 Oculus VR, Inc. All Rights reserved.
nuclear@1 9
nuclear@1 10 Use of this software is subject to the terms of the Oculus license
nuclear@1 11 agreement provided at the time of installation or download, or which
nuclear@1 12 otherwise accompanies this software in either electronic or hard copy form.
nuclear@1 13
nuclear@1 14 ************************************************************************************/
nuclear@1 15
nuclear@1 16 #include "OVR_Allocator.h"
nuclear@1 17 #ifdef OVR_OS_MAC
nuclear@1 18 #include <stdlib.h>
nuclear@1 19 #else
nuclear@1 20 #include <malloc.h>
nuclear@1 21 #endif
nuclear@1 22
nuclear@1 23 namespace OVR {
nuclear@1 24
nuclear@1 25 //-----------------------------------------------------------------------------------
nuclear@1 26 // ***** Allocator
nuclear@1 27
nuclear@1 28 Allocator* Allocator::pInstance = 0;
nuclear@1 29
nuclear@1 30 // Default AlignedAlloc implementation will delegate to Alloc/Free after doing rounding.
nuclear@1 31 void* Allocator::AllocAligned(UPInt size, UPInt align)
nuclear@1 32 {
nuclear@1 33 OVR_ASSERT((align & (align-1)) == 0);
nuclear@1 34 align = (align > sizeof(UPInt)) ? align : sizeof(UPInt);
nuclear@1 35 UPInt p = (UPInt)Alloc(size+align);
nuclear@1 36 UPInt aligned = 0;
nuclear@1 37 if (p)
nuclear@1 38 {
nuclear@1 39 aligned = (UPInt(p) + align-1) & ~(align-1);
nuclear@1 40 if (aligned == p)
nuclear@1 41 aligned += align;
nuclear@1 42 *(((UPInt*)aligned)-1) = aligned-p;
nuclear@1 43 }
nuclear@1 44 return (void*)aligned;
nuclear@1 45 }
nuclear@1 46
nuclear@1 47 void Allocator::FreeAligned(void* p)
nuclear@1 48 {
nuclear@1 49 UPInt src = UPInt(p) - *(((UPInt*)p)-1);
nuclear@1 50 Free((void*)src);
nuclear@1 51 }
nuclear@1 52
nuclear@1 53
nuclear@1 54 //------------------------------------------------------------------------
nuclear@1 55 // ***** Default Allocator
nuclear@1 56
nuclear@1 57 // This allocator is created and used if no other allocator is installed.
nuclear@1 58 // Default allocator delegates to system malloc.
nuclear@1 59
nuclear@1 60 void* DefaultAllocator::Alloc(UPInt size)
nuclear@1 61 {
nuclear@1 62 return malloc(size);
nuclear@1 63 }
nuclear@1 64 void* DefaultAllocator::AllocDebug(UPInt size, const char* file, unsigned line)
nuclear@1 65 {
nuclear@1 66 #if defined(OVR_CC_MSVC) && defined(_CRTDBG_MAP_ALLOC)
nuclear@1 67 return _malloc_dbg(size, _NORMAL_BLOCK, file, line);
nuclear@1 68 #else
nuclear@1 69 OVR_UNUSED2(file, line);
nuclear@1 70 return malloc(size);
nuclear@1 71 #endif
nuclear@1 72 }
nuclear@1 73
nuclear@1 74 void* DefaultAllocator::Realloc(void* p, UPInt newSize)
nuclear@1 75 {
nuclear@1 76 return realloc(p, newSize);
nuclear@1 77 }
nuclear@1 78 void DefaultAllocator::Free(void *p)
nuclear@1 79 {
nuclear@1 80 return free(p);
nuclear@1 81 }
nuclear@1 82
nuclear@1 83
nuclear@1 84 } // OVR