oculus1

diff 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
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/libovr/Src/Kernel/OVR_Allocator.cpp	Sat Sep 14 16:14:59 2013 +0300
     1.3 @@ -0,0 +1,1 @@
     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