ovr_sdk

view 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 source
1 /************************************************************************************
3 Filename : OVR_Allocator.cpp
4 Content : Installable memory allocator implementation
5 Created : September 19, 2012
6 Notes :
8 Copyright : Copyright 2014 Oculus VR, LLC All Rights reserved.
10 Licensed under the Oculus VR Rift SDK License Version 3.2 (the "License");
11 you may not use the Oculus VR Rift SDK except in compliance with the License,
12 which is provided at the time of installation or download, or which
13 otherwise accompanies this software in either electronic or hard copy form.
15 You may obtain a copy of the License at
17 http://www.oculusvr.com/licenses/LICENSE-3.2
19 Unless required by applicable law or agreed to in writing, the Oculus VR SDK
20 distributed under the License is distributed on an "AS IS" BASIS,
21 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 See the License for the specific language governing permissions and
23 limitations under the License.
25 ************************************************************************************/
27 #include "OVR_Allocator.h"
28 #ifdef OVR_OS_MAC
29 #include <stdlib.h>
30 #else
31 #include <malloc.h>
32 #endif
34 #if defined(OVR_OS_MS)
35 #include <Windows.h>
36 #elif defined(OVR_OS_MAC) || defined(OVR_OS_UNIX)
37 #include <unistd.h>
38 #include <sys/mman.h>
39 #endif
42 namespace OVR {
44 //-----------------------------------------------------------------------------------
45 // ***** Allocator
47 Allocator* Allocator::pInstance = 0;
49 // Default AlignedAlloc implementation will delegate to Alloc/Free after doing rounding.
50 void* Allocator::AllocAligned(size_t size, size_t align)
51 {
52 OVR_ASSERT((align & (align-1)) == 0);
53 align = (align > sizeof(size_t)) ? align : sizeof(size_t);
54 size_t p = (size_t)Alloc(size+align);
55 size_t aligned = 0;
56 if (p)
57 {
58 aligned = (size_t(p) + align-1) & ~(align-1);
59 if (aligned == p)
60 aligned += align;
61 *(((size_t*)aligned)-1) = aligned-p;
62 }
63 return (void*)aligned;
64 }
66 void Allocator::FreeAligned(void* p)
67 {
68 size_t src = size_t(p) - *(((size_t*)p)-1);
69 Free((void*)src);
70 }
73 //------------------------------------------------------------------------
74 // ***** Default Allocator
76 // This allocator is created and used if no other allocator is installed.
77 // Default allocator delegates to system malloc.
79 void* DefaultAllocator::Alloc(size_t size)
80 {
81 return malloc(size);
82 }
83 void* DefaultAllocator::AllocDebug(size_t size, const char* file, unsigned line)
84 {
85 OVR_UNUSED2(file, line); // should be here for debugopt config
86 #if defined(OVR_CC_MSVC) && defined(_CRTDBG_MAP_ALLOC)
87 return _malloc_dbg(size, _NORMAL_BLOCK, file, line);
88 #else
89 return malloc(size);
90 #endif
91 }
93 void* DefaultAllocator::Realloc(void* p, size_t newSize)
94 {
95 return realloc(p, newSize);
96 }
97 void DefaultAllocator::Free(void *p)
98 {
99 return free(p);
100 }
105 void* MMapAlloc(size_t size)
106 {
107 #if defined(OVR_OS_MS)
108 return VirtualAlloc(NULL, size, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE); // size is rounded up to a page. // Returned memory is 0-filled.
110 #elif defined(OVR_OS_MAC) || defined(OVR_OS_UNIX)
111 #if !defined(MAP_FAILED)
112 #define MAP_FAILED ((void*)-1)
113 #endif
115 void* result = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0); // Returned memory is 0-filled.
116 if(result == MAP_FAILED) // mmap returns MAP_FAILED (-1) upon failure.
117 result = NULL;
118 return result;
119 #endif
120 }
125 void MMapFree(void* memory, size_t size)
126 {
127 #if defined(OVR_OS_MS)
128 OVR_UNUSED(size);
129 VirtualFree(memory, 0, MEM_RELEASE);
131 #elif defined(OVR_OS_MAC) || defined(OVR_OS_UNIX)
132 size_t pageSize = getpagesize();
133 size = (((size + (pageSize - 1)) / pageSize) * pageSize);
134 munmap(memory, size); // Must supply the size to munmap.
135 #endif
136 }
141 } // OVR