ovr_sdk

diff LibOVR/Src/Kernel/OVR_Allocator.cpp @ 0:1b39a1b46319

initial 0.4.4
author John Tsiombikas <nuclear@member.fsf.org>
date Wed, 14 Jan 2015 06:51:16 +0200
parents
children
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/LibOVR/Src/Kernel/OVR_Allocator.cpp	Wed Jan 14 06:51:16 2015 +0200
     1.3 @@ -0,0 +1,141 @@
     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 2014 Oculus VR, LLC All Rights reserved.
    1.12 +
    1.13 +Licensed under the Oculus VR Rift SDK License Version 3.2 (the "License"); 
    1.14 +you may not use the Oculus VR Rift SDK except in compliance with the License, 
    1.15 +which is provided at the time of installation or download, or which 
    1.16 +otherwise accompanies this software in either electronic or hard copy form.
    1.17 +
    1.18 +You may obtain a copy of the License at
    1.19 +
    1.20 +http://www.oculusvr.com/licenses/LICENSE-3.2 
    1.21 +
    1.22 +Unless required by applicable law or agreed to in writing, the Oculus VR SDK 
    1.23 +distributed under the License is distributed on an "AS IS" BASIS,
    1.24 +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    1.25 +See the License for the specific language governing permissions and
    1.26 +limitations under the License.
    1.27 +
    1.28 +************************************************************************************/
    1.29 +
    1.30 +#include "OVR_Allocator.h"
    1.31 +#ifdef OVR_OS_MAC
    1.32 + #include <stdlib.h>
    1.33 +#else
    1.34 + #include <malloc.h>
    1.35 +#endif
    1.36 +
    1.37 +#if defined(OVR_OS_MS)
    1.38 + #include <Windows.h>
    1.39 +#elif defined(OVR_OS_MAC) || defined(OVR_OS_UNIX)
    1.40 + #include <unistd.h>
    1.41 + #include <sys/mman.h>
    1.42 +#endif
    1.43 +
    1.44 +
    1.45 +namespace OVR {
    1.46 +
    1.47 +//-----------------------------------------------------------------------------------
    1.48 +// ***** Allocator
    1.49 +
    1.50 +Allocator* Allocator::pInstance = 0;
    1.51 +
    1.52 +// Default AlignedAlloc implementation will delegate to Alloc/Free after doing rounding.
    1.53 +void* Allocator::AllocAligned(size_t size, size_t align)
    1.54 +{
    1.55 +    OVR_ASSERT((align & (align-1)) == 0);
    1.56 +    align = (align > sizeof(size_t)) ? align : sizeof(size_t);
    1.57 +    size_t p = (size_t)Alloc(size+align);
    1.58 +    size_t aligned = 0;
    1.59 +    if (p)
    1.60 +    {
    1.61 +        aligned = (size_t(p) + align-1) & ~(align-1);
    1.62 +        if (aligned == p) 
    1.63 +            aligned += align;
    1.64 +        *(((size_t*)aligned)-1) = aligned-p;
    1.65 +    }
    1.66 +    return (void*)aligned;
    1.67 +}
    1.68 +
    1.69 +void Allocator::FreeAligned(void* p)
    1.70 +{
    1.71 +    size_t src = size_t(p) - *(((size_t*)p)-1);
    1.72 +    Free((void*)src);
    1.73 +}
    1.74 +
    1.75 +
    1.76 +//------------------------------------------------------------------------
    1.77 +// ***** Default Allocator
    1.78 +
    1.79 +// This allocator is created and used if no other allocator is installed.
    1.80 +// Default allocator delegates to system malloc.
    1.81 +
    1.82 +void* DefaultAllocator::Alloc(size_t size)
    1.83 +{
    1.84 +    return malloc(size);
    1.85 +}
    1.86 +void* DefaultAllocator::AllocDebug(size_t size, const char* file, unsigned line)
    1.87 +{
    1.88 +	OVR_UNUSED2(file, line); // should be here for debugopt config
    1.89 +#if defined(OVR_CC_MSVC) && defined(_CRTDBG_MAP_ALLOC)
    1.90 +    return _malloc_dbg(size, _NORMAL_BLOCK, file, line);
    1.91 +#else
    1.92 +    return malloc(size);
    1.93 +#endif
    1.94 +}
    1.95 +
    1.96 +void* DefaultAllocator::Realloc(void* p, size_t newSize)
    1.97 +{
    1.98 +    return realloc(p, newSize);
    1.99 +}
   1.100 +void DefaultAllocator::Free(void *p)
   1.101 +{
   1.102 +    return free(p);
   1.103 +}
   1.104 +
   1.105 +
   1.106 +
   1.107 +
   1.108 +void* MMapAlloc(size_t size)
   1.109 +{
   1.110 +    #if defined(OVR_OS_MS)
   1.111 +        return VirtualAlloc(NULL, size, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE); // size is rounded up to a page. // Returned memory is 0-filled.
   1.112 +
   1.113 +    #elif defined(OVR_OS_MAC) || defined(OVR_OS_UNIX)
   1.114 +        #if !defined(MAP_FAILED)
   1.115 +            #define MAP_FAILED ((void*)-1)
   1.116 +        #endif
   1.117 +
   1.118 +        void* result = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0); // Returned memory is 0-filled.
   1.119 +        if(result == MAP_FAILED) // mmap returns MAP_FAILED (-1) upon failure.
   1.120 +            result = NULL;
   1.121 +        return result;
   1.122 +    #endif
   1.123 +}
   1.124 +
   1.125 +
   1.126 +
   1.127 +
   1.128 +void MMapFree(void* memory, size_t size)
   1.129 +{
   1.130 +    #if defined(OVR_OS_MS)
   1.131 +        OVR_UNUSED(size);
   1.132 +        VirtualFree(memory, 0, MEM_RELEASE);
   1.133 +
   1.134 +    #elif defined(OVR_OS_MAC) || defined(OVR_OS_UNIX)
   1.135 +        size_t pageSize = getpagesize();
   1.136 +        size = (((size + (pageSize - 1)) / pageSize) * pageSize);
   1.137 +        munmap(memory, size); // Must supply the size to munmap.
   1.138 +    #endif
   1.139 +}
   1.140 +
   1.141 +
   1.142 +
   1.143 +
   1.144 +} // OVR