[148] | 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-2013 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 | |
---|
| 29 | #ifndef __MemoryStdAlloc_H__ |
---|
| 30 | #define __MemoryStdAlloc_H__ |
---|
| 31 | |
---|
| 32 | #include <memory> |
---|
| 33 | #include <limits> |
---|
| 34 | |
---|
| 35 | #include "OgreAlignedAllocator.h" |
---|
| 36 | #include "OgreMemoryTracker.h" |
---|
| 37 | #include "OgreHeaderPrefix.h" |
---|
| 38 | |
---|
| 39 | namespace Ogre |
---|
| 40 | { |
---|
| 41 | #if OGRE_MEMORY_ALLOCATOR == OGRE_MEMORY_ALLOCATOR_STD |
---|
| 42 | |
---|
| 43 | /** \addtogroup Core |
---|
| 44 | * @{ |
---|
| 45 | */ |
---|
| 46 | /** \addtogroup Memory |
---|
| 47 | * @{ |
---|
| 48 | */ |
---|
| 49 | /** A "standard" allocation policy for use with AllocatedObject and |
---|
| 50 | STLAllocator. This is the class that actually does the allocation |
---|
| 51 | and deallocation of physical memory, and is what you will want to |
---|
| 52 | provide a custom version of if you wish to change how memory is allocated. |
---|
| 53 | @par |
---|
| 54 | This class just delegates to the global malloc/free. |
---|
| 55 | */ |
---|
| 56 | class _OgreExport StdAllocPolicy |
---|
| 57 | { |
---|
| 58 | public: |
---|
| 59 | static inline void* allocateBytes(size_t count, |
---|
| 60 | #if OGRE_MEMORY_TRACKER |
---|
| 61 | const char* file = 0, int line = 0, const char* func = 0 |
---|
| 62 | #else |
---|
| 63 | const char* = 0, int = 0, const char* = 0 |
---|
| 64 | #endif |
---|
| 65 | ) |
---|
| 66 | { |
---|
| 67 | void* ptr = malloc(count); |
---|
| 68 | #if OGRE_MEMORY_TRACKER |
---|
| 69 | // this alloc policy doesn't do pools |
---|
| 70 | MemoryTracker::get()._recordAlloc(ptr, count, 0, file, line, func); |
---|
| 71 | #endif |
---|
| 72 | return ptr; |
---|
| 73 | } |
---|
| 74 | |
---|
| 75 | static inline void deallocateBytes(void* ptr) |
---|
| 76 | { |
---|
| 77 | #if OGRE_MEMORY_TRACKER |
---|
| 78 | MemoryTracker::get()._recordDealloc(ptr); |
---|
| 79 | #endif |
---|
| 80 | free(ptr); |
---|
| 81 | } |
---|
| 82 | |
---|
| 83 | /// Get the maximum size of a single allocation |
---|
| 84 | static inline size_t getMaxAllocationSize() |
---|
| 85 | { |
---|
| 86 | return std::numeric_limits<size_t>::max(); |
---|
| 87 | } |
---|
| 88 | private: |
---|
| 89 | // no instantiation |
---|
| 90 | StdAllocPolicy() |
---|
| 91 | { } |
---|
| 92 | }; |
---|
| 93 | |
---|
| 94 | /** A "standard" allocation policy for use with AllocatedObject and |
---|
| 95 | STLAllocator, which aligns memory at a given boundary (which should be |
---|
| 96 | a power of 2). This is the class that actually does the allocation |
---|
| 97 | and deallocation of physical memory, and is what you will want to |
---|
| 98 | provide a custom version of if you wish to change how memory is allocated. |
---|
| 99 | @par |
---|
| 100 | This class just delegates to the global malloc/free, via AlignedMemory. |
---|
| 101 | @note |
---|
| 102 | template parameter Alignment equal to zero means use default |
---|
| 103 | platform dependent alignment. |
---|
| 104 | |
---|
| 105 | */ |
---|
| 106 | template <size_t Alignment = 0> |
---|
| 107 | class StdAlignedAllocPolicy |
---|
| 108 | { |
---|
| 109 | public: |
---|
| 110 | // compile-time check alignment is available. |
---|
| 111 | typedef int IsValidAlignment |
---|
| 112 | [Alignment <= 128 && ((Alignment & (Alignment-1)) == 0) ? +1 : -1]; |
---|
| 113 | |
---|
| 114 | static inline void* allocateBytes(size_t count, |
---|
| 115 | #if OGRE_MEMORY_TRACKER |
---|
| 116 | const char* file = 0, int line = 0, const char* func = 0 |
---|
| 117 | #else |
---|
| 118 | const char* = 0, int = 0, const char* = 0 |
---|
| 119 | #endif |
---|
| 120 | ) |
---|
| 121 | { |
---|
| 122 | void* ptr = Alignment ? AlignedMemory::allocate(count, Alignment) |
---|
| 123 | : AlignedMemory::allocate(count); |
---|
| 124 | #if OGRE_MEMORY_TRACKER |
---|
| 125 | // this alloc policy doesn't do pools |
---|
| 126 | MemoryTracker::get()._recordAlloc(ptr, count, 0, file, line, func); |
---|
| 127 | #endif |
---|
| 128 | return ptr; |
---|
| 129 | } |
---|
| 130 | |
---|
| 131 | static inline void deallocateBytes(void* ptr) |
---|
| 132 | { |
---|
| 133 | #if OGRE_MEMORY_TRACKER |
---|
| 134 | MemoryTracker::get()._recordDealloc(ptr); |
---|
| 135 | #endif |
---|
| 136 | AlignedMemory::deallocate(ptr); |
---|
| 137 | } |
---|
| 138 | |
---|
| 139 | /// Get the maximum size of a single allocation |
---|
| 140 | static inline size_t getMaxAllocationSize() |
---|
| 141 | { |
---|
| 142 | return std::numeric_limits<size_t>::max(); |
---|
| 143 | } |
---|
| 144 | private: |
---|
| 145 | // No instantiation |
---|
| 146 | StdAlignedAllocPolicy() |
---|
| 147 | { } |
---|
| 148 | }; |
---|
| 149 | |
---|
| 150 | #endif |
---|
| 151 | /** @} */ |
---|
| 152 | /** @} */ |
---|
| 153 | |
---|
| 154 | }// namespace Ogre |
---|
| 155 | |
---|
| 156 | |
---|
| 157 | #include "OgreHeaderSuffix.h" |
---|
| 158 | |
---|
| 159 | #endif // __MemoryStdAlloc_H__ |
---|