OVR_Allocator.cpp
Go to the documentation of this file.
00001 /************************************************************************************
00002 
00003 Filename    :   OVR_Allocator.cpp
00004 Content     :   Installable memory allocator implementation
00005 Created     :   September 19, 2012
00006 Notes       : 
00007 
00008 Copyright   :   Copyright 2012 Oculus VR, Inc. All Rights reserved.
00009 
00010 Use of this software is subject to the terms of the Oculus license
00011 agreement provided at the time of installation or download, or which
00012 otherwise accompanies this software in either electronic or hard copy form.
00013 
00014 ************************************************************************************/
00015 
00016 #include "OVR_Allocator.h"
00017 #ifdef OVR_OS_MAC
00018  #include <stdlib.h>
00019 #else
00020  #include <malloc.h>
00021 #endif
00022 
00023 namespace OVR {
00024 
00025 //-----------------------------------------------------------------------------------
00026 // ***** Allocator
00027 
00028 Allocator* Allocator::pInstance = 0;
00029 
00030 // Default AlignedAlloc implementation will delegate to Alloc/Free after doing rounding.
00031 void* Allocator::AllocAligned(UPInt size, UPInt align)
00032 {
00033     OVR_ASSERT((align & (align-1)) == 0);
00034     align = (align > sizeof(UPInt)) ? align : sizeof(UPInt);
00035     UPInt p = (UPInt)Alloc(size+align);
00036     UPInt aligned = 0;
00037     if (p)
00038     {
00039         aligned = (UPInt(p) + align-1) & ~(align-1);
00040         if (aligned == p) 
00041             aligned += align;
00042         *(((UPInt*)aligned)-1) = aligned-p;
00043     }
00044     return (void*)aligned;
00045 }
00046 
00047 void Allocator::FreeAligned(void* p)
00048 {
00049     UPInt src = UPInt(p) - *(((UPInt*)p)-1);
00050     Free((void*)src);
00051 }
00052 
00053 
00054 //------------------------------------------------------------------------
00055 // ***** Default Allocator
00056 
00057 // This allocator is created and used if no other allocator is installed.
00058 // Default allocator delegates to system malloc.
00059 
00060 void* DefaultAllocator::Alloc(UPInt size)
00061 {
00062     return malloc(size);
00063 }
00064 void* DefaultAllocator::AllocDebug(UPInt size, const char* file, unsigned line)
00065 {
00066 #if defined(OVR_CC_MSVC) && defined(_CRTDBG_MAP_ALLOC)
00067     return _malloc_dbg(size, _NORMAL_BLOCK, file, line);
00068 #else
00069     OVR_UNUSED2(file, line);
00070     return malloc(size);
00071 #endif
00072 }
00073 
00074 void* DefaultAllocator::Realloc(void* p, UPInt newSize)
00075 {
00076     return realloc(p, newSize);
00077 }
00078 void DefaultAllocator::Free(void *p)
00079 {
00080     return free(p);
00081 }
00082 
00083 
00084 } // OVR


oculus_sdk
Author(s):
autogenerated on Fri Aug 28 2015 11:53:10