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