oculus1
view libovr/Src/Kernel/OVR_Allocator.cpp @ 24:8419d8a13cee
foo
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Fri, 04 Oct 2013 14:50:26 +0300 |
parents | e2f9e4603129 |
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 2012 Oculus VR, Inc. All Rights reserved.
10 Use of this software is subject to the terms of the Oculus license
11 agreement provided at the time of installation or download, or which
12 otherwise accompanies this software in either electronic or hard copy form.
14 ************************************************************************************/
16 #include "OVR_Allocator.h"
17 #ifdef OVR_OS_MAC
18 #include <stdlib.h>
19 #else
20 #include <malloc.h>
21 #endif
23 namespace OVR {
25 //-----------------------------------------------------------------------------------
26 // ***** Allocator
28 Allocator* Allocator::pInstance = 0;
30 // Default AlignedAlloc implementation will delegate to Alloc/Free after doing rounding.
31 void* Allocator::AllocAligned(UPInt size, UPInt align)
32 {
33 OVR_ASSERT((align & (align-1)) == 0);
34 align = (align > sizeof(UPInt)) ? align : sizeof(UPInt);
35 UPInt p = (UPInt)Alloc(size+align);
36 UPInt aligned = 0;
37 if (p)
38 {
39 aligned = (UPInt(p) + align-1) & ~(align-1);
40 if (aligned == p)
41 aligned += align;
42 *(((UPInt*)aligned)-1) = aligned-p;
43 }
44 return (void*)aligned;
45 }
47 void Allocator::FreeAligned(void* p)
48 {
49 UPInt src = UPInt(p) - *(((UPInt*)p)-1);
50 Free((void*)src);
51 }
54 //------------------------------------------------------------------------
55 // ***** Default Allocator
57 // This allocator is created and used if no other allocator is installed.
58 // Default allocator delegates to system malloc.
60 void* DefaultAllocator::Alloc(UPInt size)
61 {
62 return malloc(size);
63 }
64 void* DefaultAllocator::AllocDebug(UPInt size, const char* file, unsigned line)
65 {
66 #if defined(OVR_CC_MSVC) && defined(_CRTDBG_MAP_ALLOC)
67 return _malloc_dbg(size, _NORMAL_BLOCK, file, line);
68 #else
69 OVR_UNUSED2(file, line);
70 return malloc(size);
71 #endif
72 }
74 void* DefaultAllocator::Realloc(void* p, UPInt newSize)
75 {
76 return realloc(p, newSize);
77 }
78 void DefaultAllocator::Free(void *p)
79 {
80 return free(p);
81 }
84 } // OVR