/src/ogre/OgreMain/src/OgreAlignedAllocator.cpp
Line | Count | Source |
1 | | /* |
2 | | ----------------------------------------------------------------------------- |
3 | | This source file is part of OGRE |
4 | | (Object-oriented Graphics Rendering Engine) |
5 | | For the latest info, see http://www.ogre3d.org/ |
6 | | |
7 | | Copyright (c) 2000-2014 Torus Knot Software Ltd |
8 | | |
9 | | Permission is hereby granted, free of charge, to any person obtaining a copy |
10 | | of this software and associated documentation files (the "Software"), to deal |
11 | | in the Software without restriction, including without limitation the rights |
12 | | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
13 | | copies of the Software, and to permit persons to whom the Software is |
14 | | furnished to do so, subject to the following conditions: |
15 | | |
16 | | The above copyright notice and this permission notice shall be included in |
17 | | all copies or substantial portions of the Software. |
18 | | |
19 | | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
20 | | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
21 | | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
22 | | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
23 | | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
24 | | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
25 | | THE SOFTWARE. |
26 | | ----------------------------------------------------------------------------- |
27 | | */ |
28 | | #include "OgreStableHeaders.h" |
29 | | #include "OgrePlatformInformation.h" |
30 | | |
31 | | /** |
32 | | * |
33 | | * |___2___|3|_________5__________|__6__| |
34 | | * ^ ^ |
35 | | * 1 4 |
36 | | * |
37 | | * 1 -> Pointer to start of the block allocated by new. |
38 | | * 2 -> Gap used to get 4 aligned on given alignment |
39 | | * 3 -> Byte offset between 1 and 4 |
40 | | * 4 -> Pointer to the start of data block. |
41 | | * 5 -> Data block. |
42 | | * 6 -> Wasted memory at rear of data block. |
43 | | */ |
44 | | |
45 | | namespace Ogre { |
46 | | |
47 | | /** Allocate memory with given alignment. |
48 | | @param |
49 | | size The size of memory need to allocate. |
50 | | @param |
51 | | alignment The alignment of result pointer, must be power of two |
52 | | and in range [1, 128]. |
53 | | @return |
54 | | The allocated memory pointer. |
55 | | @par |
56 | | On failure, exception will be throw. |
57 | | */ |
58 | | void* AlignedMemory::allocate(size_t size, size_t alignment) |
59 | 0 | { |
60 | 0 | assert(0 < alignment && alignment <= 128 && Bitwise::isPO2(alignment)); |
61 | |
|
62 | 0 | unsigned char* p = new unsigned char[size + alignment]; |
63 | 0 | size_t offset = alignment - (size_t(p) & (alignment-1)); |
64 | |
|
65 | 0 | unsigned char* result = p + offset; |
66 | 0 | result[-1] = (unsigned char)offset; |
67 | |
|
68 | 0 | return result; |
69 | 0 | } |
70 | | //--------------------------------------------------------------------- |
71 | | void* AlignedMemory::allocate(size_t size) |
72 | 0 | { |
73 | 0 | return allocate(size, OGRE_SIMD_ALIGNMENT); |
74 | 0 | } |
75 | | //--------------------------------------------------------------------- |
76 | | void AlignedMemory::deallocate(void* p) |
77 | 0 | { |
78 | 0 | if (p) |
79 | 0 | { |
80 | 0 | unsigned char* mem = (unsigned char*)p; |
81 | 0 | mem = mem - mem[-1]; |
82 | 0 | delete [] mem; |
83 | 0 | } |
84 | 0 | } |
85 | | |
86 | | } |