Crazy Eddie's GUI System  0.8.1
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Pages
MemoryAllocation.h
1 /***********************************************************************
2  filename: CEGUIMemoryAllocation.h
3  created: 14/10/2010
4  author: Martin Preisler (inspired by Ogre3D)
5 
6  purpose: Allows custom memory allocators to be used within CEGUI
7 *************************************************************************/
8 /***************************************************************************
9  * Copyright (C) 2004 - 2010 Paul D Turner & The CEGUI Development Team
10  *
11  * Permission is hereby granted, free of charge, to any person obtaining
12  * a copy of this software and associated documentation files (the
13  * "Software"), to deal in the Software without restriction, including
14  * without limitation the rights to use, copy, modify, merge, publish,
15  * distribute, sublicense, and/or sell copies of the Software, and to
16  * permit persons to whom the Software is furnished to do so, subject to
17  * the following conditions:
18  *
19  * The above copyright notice and this permission notice shall be
20  * included in all copies or substantial portions of the Software.
21  *
22  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
25  * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
26  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
27  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
28  * OTHER DEALINGS IN THE SOFTWARE.
29  ***************************************************************************/
30 #ifndef _CEGUIMemoryAllocation_h_
31 #define _CEGUIMemoryAllocation_h_
32 
33 #ifndef _CEGUIBase_h_
34 # error Dont include this directly! Include CEGUIBase.h instead.
35 #endif
36 
37 #define CEGUI_SET_DEFAULT_ALLOCATOR(A)\
38 template<typename T>\
39 struct AllocatorConfig\
40 {\
41  typedef A Allocator;\
42 };
43 
44 #define CEGUI_SET_ALLOCATOR(Class, A)\
45 template<>\
46 struct AllocatorConfig<Class>\
47 {\
48  typedef A Allocator;\
49 };
50 
51 #ifdef CEGUI_CUSTOM_ALLOCATORS
52 
53 namespace CEGUI
54 {
55 
56 // stub classes uses for allocator configuration
57 class STLAllocator {};
58 class BufferAllocator {};
59 
60 // borrowed from Ogre, used to construct arrays
61 template<typename T>
62 T* constructN(T* basePtr, size_t count)
63 {
64  for (size_t i = 0; i < count; ++i)
65  {
66  new ((void*)(basePtr+i)) T();
67  }
68  return basePtr;
69 }
70 
71 // ogre doesn't do this template but I added it because it works even for types without
72 // destructors where I was getting syntax errors with just the macro
73 template<typename T>
74 void destructN(T* basePtr, size_t count)
75 {
76  // iterate in reverse for consistency with delete []
77  for (size_t i = count - 1; i-- > 0;)
78  {
79  basePtr[i].~T();
80  }
81 }
82 
83 } // CEGUI namespace
84 
85 #ifndef CEGUI_CUSTOM_ALLOCATORS_DEBUG
86 # define CEGUI_NEW_AO new
87 # define CEGUI_DELETE_AO delete
88 // for primitive types, types not inherited from AllocatedObject
89 # define CEGUI_NEW_PT(T, A) new (::CEGUI::AllocatorConfig<A>::Allocator::allocateBytes(sizeof(T))) T
90 # define CEGUI_NEW_ARRAY_PT(T, count, A) ::CEGUI::constructN(static_cast<T*>(::CEGUI::AllocatorConfig<A>::Allocator::allocateBytes(sizeof(T)*(count))), count)
91 # define CEGUI_DELETE_PT(ptr, T, A) do{if(ptr){(ptr)->~T(); ::CEGUI::AllocatorConfig<A>::Allocator::deallocateBytes((void*)ptr);}}while(0)
92 # 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)
93 #else
94 # define CEGUI_NEW_AO new(__FILE__, __LINE__, __FUNCTION__)
95 # define CEGUI_DELETE_AO delete
96 // for primitive types, types not inherited from AllocatedObject
97 # define CEGUI_NEW_PT(T, A) new (::CEGUI::AllocatorConfig<A>::Allocator::allocateBytes(sizeof(T), __FILE__, __LINE__, __FUNCTION__)) T
98 # 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)
99 # define CEGUI_DELETE_PT(ptr, T, A) do{if(ptr){(ptr)->~T(); ::CEGUI::AllocatorConfig<A>::Allocator::deallocateBytes((void*)ptr);}}while(0)
100 # 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)
101 #endif
102 
103 #ifndef CEGUI_CUSTOM_ALLOCATORS_INCLUDE
104 # define CEGUI_CUSTOM_ALLOCATORS_INCLUDE "CEGUI/MemoryStdAllocator.h"
105 #endif
106 
107 // all the wrappers have been declared, now we include the chosen memory allocator file
108 #include CEGUI_CUSTOM_ALLOCATORS_INCLUDE
109 
110 #else
111 
112 // dummy macros
113 #define CEGUI_NEW_AO new
114 #define CEGUI_DELETE_AO delete
115 // for primitive types, types not inherited from AllocatedObject
116 #define CEGUI_NEW_PT(T, Allocator) new T
117 #define CEGUI_NEW_ARRAY_PT(T, count, Allocator) new T[count]
118 #define CEGUI_DELETE_PT(ptr, T, Allocator) delete ptr
119 #define CEGUI_DELETE_ARRAY_PT(ptr, T, count, Allocator) delete [] ptr
120 
121 #endif
122 
123 #include "CEGUI/MemoryAllocatedObject.h"
124 #include "CEGUI/MemorySTLWrapper.h"
125 
126 #endif // end of guard _CEGUIMemoryAllocation_h_