rev |
line source |
nuclear@3
|
1 /************************************************************************************
|
nuclear@3
|
2
|
nuclear@3
|
3 Filename : OVR_Allocator.cpp
|
nuclear@3
|
4 Content : Installable memory allocator implementation
|
nuclear@3
|
5 Created : September 19, 2012
|
nuclear@3
|
6 Notes :
|
nuclear@3
|
7
|
nuclear@3
|
8 Copyright : Copyright 2012 Oculus VR, Inc. All Rights reserved.
|
nuclear@3
|
9
|
nuclear@3
|
10 Use of this software is subject to the terms of the Oculus license
|
nuclear@3
|
11 agreement provided at the time of installation or download, or which
|
nuclear@3
|
12 otherwise accompanies this software in either electronic or hard copy form.
|
nuclear@3
|
13
|
nuclear@3
|
14 ************************************************************************************/
|
nuclear@3
|
15
|
nuclear@3
|
16 #include "OVR_Allocator.h"
|
nuclear@3
|
17 #ifdef OVR_OS_MAC
|
nuclear@3
|
18 #include <stdlib.h>
|
nuclear@3
|
19 #else
|
nuclear@3
|
20 #include <malloc.h>
|
nuclear@3
|
21 #endif
|
nuclear@3
|
22
|
nuclear@3
|
23 namespace OVR {
|
nuclear@3
|
24
|
nuclear@3
|
25 //-----------------------------------------------------------------------------------
|
nuclear@3
|
26 // ***** Allocator
|
nuclear@3
|
27
|
nuclear@3
|
28 Allocator* Allocator::pInstance = 0;
|
nuclear@3
|
29
|
nuclear@3
|
30 // Default AlignedAlloc implementation will delegate to Alloc/Free after doing rounding.
|
nuclear@3
|
31 void* Allocator::AllocAligned(UPInt size, UPInt align)
|
nuclear@3
|
32 {
|
nuclear@3
|
33 OVR_ASSERT((align & (align-1)) == 0);
|
nuclear@3
|
34 align = (align > sizeof(UPInt)) ? align : sizeof(UPInt);
|
nuclear@3
|
35 UPInt p = (UPInt)Alloc(size+align);
|
nuclear@3
|
36 UPInt aligned = 0;
|
nuclear@3
|
37 if (p)
|
nuclear@3
|
38 {
|
nuclear@3
|
39 aligned = (UPInt(p) + align-1) & ~(align-1);
|
nuclear@3
|
40 if (aligned == p)
|
nuclear@3
|
41 aligned += align;
|
nuclear@3
|
42 *(((UPInt*)aligned)-1) = aligned-p;
|
nuclear@3
|
43 }
|
nuclear@3
|
44 return (void*)aligned;
|
nuclear@3
|
45 }
|
nuclear@3
|
46
|
nuclear@3
|
47 void Allocator::FreeAligned(void* p)
|
nuclear@3
|
48 {
|
nuclear@3
|
49 UPInt src = UPInt(p) - *(((UPInt*)p)-1);
|
nuclear@3
|
50 Free((void*)src);
|
nuclear@3
|
51 }
|
nuclear@3
|
52
|
nuclear@3
|
53
|
nuclear@3
|
54 //------------------------------------------------------------------------
|
nuclear@3
|
55 // ***** Default Allocator
|
nuclear@3
|
56
|
nuclear@3
|
57 // This allocator is created and used if no other allocator is installed.
|
nuclear@3
|
58 // Default allocator delegates to system malloc.
|
nuclear@3
|
59
|
nuclear@3
|
60 void* DefaultAllocator::Alloc(UPInt size)
|
nuclear@3
|
61 {
|
nuclear@3
|
62 return malloc(size);
|
nuclear@3
|
63 }
|
nuclear@3
|
64 void* DefaultAllocator::AllocDebug(UPInt size, const char* file, unsigned line)
|
nuclear@3
|
65 {
|
nuclear@3
|
66 #if defined(OVR_CC_MSVC) && defined(_CRTDBG_MAP_ALLOC)
|
nuclear@3
|
67 return _malloc_dbg(size, _NORMAL_BLOCK, file, line);
|
nuclear@3
|
68 #else
|
nuclear@3
|
69 OVR_UNUSED2(file, line);
|
nuclear@3
|
70 return malloc(size);
|
nuclear@3
|
71 #endif
|
nuclear@3
|
72 }
|
nuclear@3
|
73
|
nuclear@3
|
74 void* DefaultAllocator::Realloc(void* p, UPInt newSize)
|
nuclear@3
|
75 {
|
nuclear@3
|
76 return realloc(p, newSize);
|
nuclear@3
|
77 }
|
nuclear@3
|
78 void DefaultAllocator::Free(void *p)
|
nuclear@3
|
79 {
|
nuclear@3
|
80 return free(p);
|
nuclear@3
|
81 }
|
nuclear@3
|
82
|
nuclear@3
|
83
|
nuclear@3
|
84 } // OVR
|