diff options
Diffstat (limited to 'LibOVR/Src/Kernel/OVR_Allocator.cpp')
-rw-r--r-- | LibOVR/Src/Kernel/OVR_Allocator.cpp | 74 |
1 files changed, 60 insertions, 14 deletions
diff --git a/LibOVR/Src/Kernel/OVR_Allocator.cpp b/LibOVR/Src/Kernel/OVR_Allocator.cpp index 0f82561..f963d08 100644 --- a/LibOVR/Src/Kernel/OVR_Allocator.cpp +++ b/LibOVR/Src/Kernel/OVR_Allocator.cpp @@ -5,16 +5,16 @@ Content : Installable memory allocator implementation Created : September 19, 2012 Notes : -Copyright : Copyright 2014 Oculus VR, Inc. All Rights reserved. +Copyright : Copyright 2014 Oculus VR, LLC All Rights reserved. -Licensed under the Oculus VR Rift SDK License Version 3.1 (the "License"); +Licensed under the Oculus VR Rift SDK License Version 3.2 (the "License"); you may not use the Oculus VR Rift SDK except in compliance with the License, which is provided at the time of installation or download, or which otherwise accompanies this software in either electronic or hard copy form. You may obtain a copy of the License at -http://www.oculusvr.com/licenses/LICENSE-3.1 +http://www.oculusvr.com/licenses/LICENSE-3.2 Unless required by applicable law or agreed to in writing, the Oculus VR SDK distributed under the License is distributed on an "AS IS" BASIS, @@ -31,6 +31,14 @@ limitations under the License. #include <malloc.h> #endif +#if defined(OVR_OS_MS) + #include <Windows.h> +#elif defined(OVR_OS_MAC) || defined(OVR_OS_UNIX) + #include <unistd.h> + #include <sys/mman.h> +#endif + + namespace OVR { //----------------------------------------------------------------------------------- @@ -39,25 +47,25 @@ namespace OVR { Allocator* Allocator::pInstance = 0; // Default AlignedAlloc implementation will delegate to Alloc/Free after doing rounding. -void* Allocator::AllocAligned(UPInt size, UPInt align) +void* Allocator::AllocAligned(size_t size, size_t align) { OVR_ASSERT((align & (align-1)) == 0); - align = (align > sizeof(UPInt)) ? align : sizeof(UPInt); - UPInt p = (UPInt)Alloc(size+align); - UPInt aligned = 0; + align = (align > sizeof(size_t)) ? align : sizeof(size_t); + size_t p = (size_t)Alloc(size+align); + size_t aligned = 0; if (p) { - aligned = (UPInt(p) + align-1) & ~(align-1); + aligned = (size_t(p) + align-1) & ~(align-1); if (aligned == p) aligned += align; - *(((UPInt*)aligned)-1) = aligned-p; + *(((size_t*)aligned)-1) = aligned-p; } return (void*)aligned; } void Allocator::FreeAligned(void* p) { - UPInt src = UPInt(p) - *(((UPInt*)p)-1); + size_t src = size_t(p) - *(((size_t*)p)-1); Free((void*)src); } @@ -68,21 +76,21 @@ void Allocator::FreeAligned(void* p) // This allocator is created and used if no other allocator is installed. // Default allocator delegates to system malloc. -void* DefaultAllocator::Alloc(UPInt size) +void* DefaultAllocator::Alloc(size_t size) { return malloc(size); } -void* DefaultAllocator::AllocDebug(UPInt size, const char* file, unsigned line) +void* DefaultAllocator::AllocDebug(size_t size, const char* file, unsigned line) { + OVR_UNUSED2(file, line); // should be here for debugopt config #if defined(OVR_CC_MSVC) && defined(_CRTDBG_MAP_ALLOC) return _malloc_dbg(size, _NORMAL_BLOCK, file, line); #else - OVR_UNUSED2(file, line); return malloc(size); #endif } -void* DefaultAllocator::Realloc(void* p, UPInt newSize) +void* DefaultAllocator::Realloc(void* p, size_t newSize) { return realloc(p, newSize); } @@ -92,4 +100,42 @@ void DefaultAllocator::Free(void *p) } + + +void* MMapAlloc(size_t size) +{ + #if defined(OVR_OS_MS) + return VirtualAlloc(NULL, size, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE); // size is rounded up to a page. // Returned memory is 0-filled. + + #elif defined(OVR_OS_MAC) || defined(OVR_OS_UNIX) + #if !defined(MAP_FAILED) + #define MAP_FAILED ((void*)-1) + #endif + + void* result = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0); // Returned memory is 0-filled. + if(result == MAP_FAILED) // mmap returns MAP_FAILED (-1) upon failure. + result = NULL; + return result; + #endif +} + + + + +void MMapFree(void* memory, size_t size) +{ + #if defined(OVR_OS_MS) + OVR_UNUSED(size); + VirtualFree(memory, 0, MEM_RELEASE); + + #elif defined(OVR_OS_MAC) || defined(OVR_OS_UNIX) + size_t pageSize = getpagesize(); + size = (((size + (pageSize - 1)) / pageSize) * pageSize); + munmap(memory, size); // Must supply the size to munmap. + #endif +} + + + + } // OVR |