Debian Codegraph @

Source/Utilities/Memory/CObjectAllocator.h

raw
1 /*
2 * Copyright (C) 2013-2015, 2019 by the Konclude Developer Team.
3 *
4 * This file is part of the reasoning system Konclude.
5 * For details and support, see <http://konclude.com/>.
6 *
7 * Konclude is free software: you can redistribute it and/or modify
8 * it under the terms of version 3 of the GNU Lesser General Public
9 * License (LGPLv3) as published by the Free Software Foundation.
10 *
11 * Konclude is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU (Lesser) General Public License for more details.
15 *
16 * You should have received a copy of the GNU (Lesser) General Public
17 * License along with Konclude. If not, see <http://www.gnu.org/licenses/>.
18 *
19 */
20
21 #ifndef KONCLUDE_UTILITIES_MEMORY_COBJECTALLOCATOR_H
22 #define KONCLUDE_UTILITIES_MEMORY_COBJECTALLOCATOR_H
23
24 // Library includes
25
26
27 // Namespace includes
28 #include "MemorySettings.h"
29 #include "CMemoryAllocationManager.h"
30
31 // Other includes
32
33
34 // Logger includes
35 #include "Logger/CLogger.h"
36
37 using namespace std;
38
39 namespace Konclude {
40
41 namespace Utilities {
42
43 namespace Memory {
44
45
46 /*!
47 *
48 * \class CObjectAllocator
49 * \author Andreas Steigmiller
50 * \version 0.1
51 * \brief TODO
52 *
53 */
54 template<class T>
55 class CObjectAllocator {
56 // public methods
57 public:
58 //! Constructor
59 CObjectAllocator() {
60 }
61
62 //! Destructor
63 virtual ~CObjectAllocator() {
64 }
65
66 static inline T* allocateAndConstruct(CMemoryAllocationManager* memoryAllocatorManager) {
67 T* object = 0;
68 if (memoryAllocatorManager) {
69 void* memAddr = memoryAllocatorManager->allocate(sizeof(T));
70 object = (T*)memAddr;
71 new (object) T();
72 } else {
73 object = new T();
74 }
75 return object;
76 }
77
78 static inline T* allocate(CMemoryAllocationManager* memoryAllocatorManager) {
79 T* object = 0;
80 if (memoryAllocatorManager) {
81 void* memAddr = memoryAllocatorManager->allocate(sizeof(T));
82 object = (T*)memAddr;
83 } else {
84 object = new T();
85 }
86 return object;
87 }
88
89 static inline T* allocateMemory(CMemoryAllocationManager* memoryAllocatorManager) {
90 T* object = 0;
91 if (memoryAllocatorManager) {
92 void* memAddr = memoryAllocatorManager->allocate(sizeof(T));
93 object = (T*)memAddr;
94 } else {
95 object = new T();
96 }
97 return object;
98 }
99
100 static inline T* allocateAndConstructArray(CMemoryAllocationManager* memoryAllocatorManager, cint arrayCount) {
101 T* objectArray = 0;
102 if (memoryAllocatorManager) {
103 void* memAddr = memoryAllocatorManager->allocate(sizeof(T)*arrayCount);
104 objectArray = (T*)memAddr;
105 T* nextObject = objectArray;
106 for (cint idx = 0; idx < arrayCount; ++idx) {
107 new (nextObject) T();
108 nextObject += 1;
109 }
110 } else {
111 objectArray = new T[arrayCount];
112 T* nextObject = objectArray;
113 for (cint idx = 0; idx < arrayCount; ++idx) {
114 new (nextObject) T();
115 nextObject += 1;
116 }
117 }
118 return objectArray;
119 }
120
121 static inline T* allocateArray(CMemoryAllocationManager* memoryAllocatorManager, cint arrayCount) {
122 T* objectArray = 0;
123 if (memoryAllocatorManager) {
124 void* memAddr = memoryAllocatorManager->allocate(sizeof(T)*arrayCount);
125 objectArray = (T*)memAddr;
126 } else {
127 objectArray = new T[arrayCount];
128 }
129 return objectArray;
130 }
131
132 static inline void release(T* object, CMemoryAllocationManager* memoryAllocatorManager) {
133 if (object) {
134 if (memoryAllocatorManager) {
135 memoryAllocatorManager->release(object);
136 } else {
137 ::operator delete (object);
138 }
139 }
140 }
141
142 static inline void destroyAndRelease(T* object, CMemoryAllocationManager* memoryAllocatorManager) {
143 if (object) {
144 if (memoryAllocatorManager) {
145 object->T::~T();
146 memoryAllocatorManager->release(object);
147 } else {
148 object->~T();
149 ::operator delete (object);
150 }
151 }
152 }
153
154 static inline void destroyAndReleaseArray(T* object, CMemoryAllocationManager* memoryAllocatorManager) {
155 // TODO: destroy object in array somehow
156 if (object) {
157 if (memoryAllocatorManager) {
158 memoryAllocatorManager->release(object);
159 } else {
160 ::operator delete [] (object);
161 }
162 }
163 }
164
165 static inline void releaseArray(T* object, CMemoryAllocationManager* memoryAllocatorManager) {
166 if (object) {
167 if (memoryAllocatorManager) {
168 memoryAllocatorManager->release(object);
169 } else {
170 ::operator delete [] (object);
171 }
172 }
173 }
174
175 // protected methods
176 protected:
177
178 // protected variables
179 protected:
180
181 // private methods
182 private:
183
184 // private variables
185 private:
186 };
187
188 template<class T>
189 inline void COADestroyAndRelease(T* object, CMemoryAllocationManager* memoryAllocatorManager) {
190 CObjectAllocator<T>::destroyAndRelease(object,memoryAllocatorManager);
191 }
192
193
194 }; // end namespace Memory
195
196 }; // end namespace Utilities
197
198 }; // end namespace Konclude
199
200 #include "CObjectAllocator.cpp"
201
202 #endif // KONCLUDE_UTILITIES_MEMORY_COBJECTALLOCATOR_H