Crazy Eddie's GUI System  0.8.4
MemoryAllocation.h
00001 /***********************************************************************
00002         created:        14/10/2010
00003         author:         Martin Preisler (inspired by Ogre3D)
00004 
00005         purpose:        Allows custom memory allocators to be used within CEGUI
00006 *************************************************************************/
00007 /***************************************************************************
00008  *   Copyright (C) 2004 - 2010 Paul D Turner & The CEGUI Development Team
00009  *
00010  *   Permission is hereby granted, free of charge, to any person obtaining
00011  *   a copy of this software and associated documentation files (the
00012  *   "Software"), to deal in the Software without restriction, including
00013  *   without limitation the rights to use, copy, modify, merge, publish,
00014  *   distribute, sublicense, and/or sell copies of the Software, and to
00015  *   permit persons to whom the Software is furnished to do so, subject to
00016  *   the following conditions:
00017  *
00018  *   The above copyright notice and this permission notice shall be
00019  *   included in all copies or substantial portions of the Software.
00020  *
00021  *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00022  *   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00023  *   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
00024  *   IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
00025  *   OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
00026  *   ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
00027  *   OTHER DEALINGS IN THE SOFTWARE.
00028  ***************************************************************************/
00029 #ifndef _CEGUIMemoryAllocation_h_
00030 #define _CEGUIMemoryAllocation_h_
00031 
00032 #ifndef _CEGUIBase_h_
00033 #   error Dont include this directly! Include CEGUIBase.h instead.
00034 #endif
00035 
00036 #define CEGUI_SET_DEFAULT_ALLOCATOR(A)\
00037 template<typename T>\
00038 struct AllocatorConfig\
00039 {\
00040     typedef A Allocator;\
00041 };
00042 
00043 #define CEGUI_SET_ALLOCATOR(Class, A)\
00044 template<>\
00045 struct AllocatorConfig<Class>\
00046 {\
00047     typedef A Allocator;\
00048 };
00049 
00050 #ifdef CEGUI_CUSTOM_ALLOCATORS
00051 
00052 namespace CEGUI
00053 {
00054 
00055 // stub classes uses for allocator configuration
00056 class STLAllocator {};
00057 class BufferAllocator {};
00058 
00059 // borrowed from Ogre, used to construct arrays
00060 template<typename T>
00061 T* constructN(T* basePtr, size_t count)
00062 {
00063         for (size_t i = 0; i < count; ++i)
00064         {
00065                 new ((void*)(basePtr+i)) T();
00066         }
00067         return basePtr;
00068 }
00069 
00070 // ogre doesn't do this template but I added it because it works even for types without
00071 // destructors where I was getting syntax errors with just the macro
00072 template<typename T>
00073 void destructN(T* basePtr, size_t count)
00074 {
00075     // iterate in reverse for consistency with delete []
00076         for (size_t i = count - 1; i-- > 0;)
00077         {
00078                 basePtr[i].~T();
00079     }
00080 }
00081 
00082 } // CEGUI namespace
00083 
00084 #ifndef CEGUI_CUSTOM_ALLOCATORS_DEBUG
00085 #   define CEGUI_NEW_AO new
00086 #   define CEGUI_DELETE_AO delete
00087 // for primitive types, types not inherited from AllocatedObject
00088 #   define CEGUI_NEW_PT(T, A) new (::CEGUI::AllocatorConfig<A>::Allocator::allocateBytes(sizeof(T))) T
00089 #   define CEGUI_NEW_ARRAY_PT(T, count, A) ::CEGUI::constructN(static_cast<T*>(::CEGUI::AllocatorConfig<A>::Allocator::allocateBytes(sizeof(T)*(count))), count)
00090 #   define CEGUI_DELETE_PT(ptr, T, A) do{if(ptr){(ptr)->~T(); ::CEGUI::AllocatorConfig<A>::Allocator::deallocateBytes((void*)ptr);}}while(0)
00091 #   define CEGUI_DELETE_ARRAY_PT(ptr, T, count, A) do{if(ptr){ ::CEGUI::destructN(static_cast<T*>(ptr), count); ::CEGUI::AllocatorConfig<A>::Allocator::deallocateBytes((void*)ptr);}}while(0)
00092 #else
00093 #   define CEGUI_NEW_AO new(__FILE__, __LINE__, __FUNCTION__)
00094 #   define CEGUI_DELETE_AO delete
00095 // for primitive types, types not inherited from AllocatedObject
00096 #   define CEGUI_NEW_PT(T, A) new (::CEGUI::AllocatorConfig<A>::Allocator::allocateBytes(sizeof(T), __FILE__, __LINE__, __FUNCTION__)) T
00097 #   define CEGUI_NEW_ARRAY_PT(T, count, A) ::CEGUI::constructN(static_cast<T*>(::CEGUI::AllocatorConfig<A>::Allocator::allocateBytes(sizeof(T)*(count), __FILE__, __LINE__, __FUNCTION__)), count)
00098 #   define CEGUI_DELETE_PT(ptr, T, A) do{if(ptr){(ptr)->~T(); ::CEGUI::AllocatorConfig<A>::Allocator::deallocateBytes((void*)ptr);}}while(0)
00099 #   define CEGUI_DELETE_ARRAY_PT(ptr, T, count, A) do{if(ptr){for (size_t b = count; b-- > 0;){ (ptr)[b].~T();} ::CEGUI::AllocatorConfig<A>::Allocator::deallocateBytes((void*)ptr);}}while(0)
00100 #endif
00101 
00102 #ifndef CEGUI_CUSTOM_ALLOCATORS_INCLUDE
00103 #   define CEGUI_CUSTOM_ALLOCATORS_INCLUDE "CEGUI/MemoryStdAllocator.h"
00104 #endif
00105 
00106 // all the wrappers have been declared, now we include the chosen memory allocator file
00107 #include CEGUI_CUSTOM_ALLOCATORS_INCLUDE
00108 
00109 #else
00110 
00111 // dummy macros
00112 #define CEGUI_NEW_AO new
00113 #define CEGUI_DELETE_AO delete
00114 // for primitive types, types not inherited from AllocatedObject
00115 #define CEGUI_NEW_PT(T, Allocator) new T
00116 #define CEGUI_NEW_ARRAY_PT(T, count, Allocator) new T[count]
00117 #define CEGUI_DELETE_PT(ptr, T, Allocator) delete ptr
00118 #define CEGUI_DELETE_ARRAY_PT(ptr, T, count, Allocator) delete [] ptr
00119 
00120 #endif
00121 
00122 #include "CEGUI/MemoryAllocatedObject.h"
00123 #include "CEGUI/MemorySTLWrapper.h"
00124 
00125 #endif  // end of guard _CEGUIMemoryAllocation_h_
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends