| 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_CDYNAMICEXPANDINGMEMORYMANAGER_H |
| 22 |
#define KONCLUDE_UTILITIES_CDYNAMICEXPANDINGMEMORYMANAGER_H |
| 23 |
|
| 24 |
// Libraries includes |
| 25 |
#include <QLinkedList> |
| 26 |
|
| 27 |
|
| 28 |
// Namespace includes |
| 29 |
#include "CAllocationObject.h" |
| 30 |
#include "UtilitiesSettings.h" |
| 31 |
#include "CMemoryReleaser.h" |
| 32 |
#include "CMemoryManager.hpp" |
| 33 |
|
| 34 |
// Other includes |
| 35 |
|
| 36 |
|
| 37 |
// Logger includes |
| 38 |
#include "Logger/CLogger.h" |
| 39 |
|
| 40 |
using namespace Konclude::Logger; |
| 41 |
|
| 42 |
|
| 43 |
namespace Konclude { |
| 44 |
|
| 45 |
namespace Utilities { |
| 46 |
|
| 47 |
|
| 48 |
/*! |
| 49 |
* |
| 50 |
* \class CDynamicExpandingMemoryManager |
| 51 |
* \author Andreas Steigmiller |
| 52 |
* \version 0.1 |
| 53 |
* \brief TODO |
| 54 |
* |
| 55 |
*/ |
| 56 |
template<class T> |
| 57 |
class CDynamicExpandingMemoryManager : public CMemoryManager<T> { |
| 58 |
// public methods |
| 59 |
public: |
| 60 |
//! Constructor |
| 61 |
CDynamicExpandingMemoryManager(qreal expansionFactor = 10., qint64 beginExtensionSize = 8, qint64 maxExtensionSize = 2040, qint64 beginAllocationSize = 0); |
| 62 |
|
| 63 |
//! Destructor |
| 64 |
virtual ~CDynamicExpandingMemoryManager(); |
| 65 |
|
| 66 |
virtual T *allocate(); |
| 67 |
virtual void release(T *&mem); |
| 68 |
virtual void release(CAllocationObject *object); |
| 69 |
|
| 70 |
void clear(); |
| 71 |
|
| 72 |
// protected methods |
| 73 |
protected: |
| 74 |
|
| 75 |
// private methods |
| 76 |
private: |
| 77 |
|
| 78 |
void extendMemory(); |
| 79 |
|
| 80 |
// private variables |
| 81 |
private: |
| 82 |
qint64 totalElements; |
| 83 |
qint64 freeElements; |
| 84 |
|
| 85 |
qint64 nextMemVecIndex; |
| 86 |
qint64 releaseMemVecIndex; |
| 87 |
|
| 88 |
qint64 vecSize; |
| 89 |
|
| 90 |
qreal extendFac; |
| 91 |
qint64 minExtSize; |
| 92 |
qint64 maxExtSize; |
| 93 |
|
| 94 |
qint64 currExtSizeInt; |
| 95 |
qreal currExtSizeReal; |
| 96 |
|
| 97 |
QList<T *> memory; |
| 98 |
QList<T **> free; |
| 99 |
QList<T **> memoryPtr; |
| 100 |
QList<T **> freeMemPtr; |
| 101 |
|
| 102 |
T **releaseMemVec; |
| 103 |
T **nextMemVec; |
| 104 |
|
| 105 |
bool hasAllocationContextChecked; |
| 106 |
bool hasAllocationContext; |
| 107 |
}; |
| 108 |
|
| 109 |
|
| 110 |
template<class T> |
| 111 |
CDynamicExpandingMemoryManager<T>::CDynamicExpandingMemoryManager(qreal expansionFactor, qint64 beginExtensionSize, qint64 maxExtensionSize, qint64 beginAllocationSize) { |
| 112 |
vecSize = maxExtensionSize; |
| 113 |
|
| 114 |
extendFac = expansionFactor; |
| 115 |
minExtSize = beginExtensionSize; |
| 116 |
maxExtSize = maxExtensionSize; |
| 117 |
|
| 118 |
currExtSizeInt = minExtSize; |
| 119 |
currExtSizeReal = minExtSize; |
| 120 |
|
| 121 |
releaseMemVec = 0; |
| 122 |
nextMemVec = 0; |
| 123 |
nextMemVecIndex = 0; |
| 124 |
releaseMemVecIndex = 0; |
| 125 |
|
| 126 |
freeElements = 0; |
| 127 |
totalElements = 0; |
| 128 |
|
| 129 |
hasAllocationContextChecked = false; |
| 130 |
hasAllocationContext = false; |
| 131 |
|
| 132 |
for (qint64 i = 0; i < beginAllocationSize; i += vecSize) { |
| 133 |
extendMemory(); |
| 134 |
} |
| 135 |
|
| 136 |
} |
| 137 |
|
| 138 |
template<class T> |
| 139 |
CDynamicExpandingMemoryManager<T>::~CDynamicExpandingMemoryManager() { |
| 140 |
clear(); |
| 141 |
} |
| 142 |
|
| 143 |
template<class T> |
| 144 |
void CDynamicExpandingMemoryManager<T>::clear() { |
| 145 |
releaseMemVec = 0; |
| 146 |
nextMemVec = 0; |
| 147 |
nextMemVecIndex = 0; |
| 148 |
releaseMemVecIndex = 0; |
| 149 |
|
| 150 |
freeElements = 0; |
| 151 |
totalElements = 0; |
| 152 |
|
| 153 |
foreach (T *mem, memory) { |
| 154 |
delete [] mem; |
| 155 |
} |
| 156 |
foreach (T **memPtr, memoryPtr) { |
| 157 |
delete [] memPtr; |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
memory.clear(); |
| 162 |
free.clear(); |
| 163 |
memoryPtr.clear(); |
| 164 |
freeMemPtr.clear(); |
| 165 |
} |
| 166 |
|
| 167 |
template<class T> |
| 168 |
void CDynamicExpandingMemoryManager<T>::extendMemory() { |
| 169 |
T *newMem = new T[currExtSizeInt]; |
| 170 |
|
| 171 |
if (!hasAllocationContextChecked) { |
| 172 |
CAllocationObject *allCon = dynamic_cast<CAllocationObject *>(&newMem[0]); |
| 173 |
if (allCon == 0) { |
| 174 |
hasAllocationContext = false; |
| 175 |
} else { |
| 176 |
hasAllocationContext = true; |
| 177 |
} |
| 178 |
hasAllocationContextChecked = true; |
| 179 |
} |
| 180 |
|
| 181 |
freeElements += currExtSizeInt; |
| 182 |
totalElements += currExtSizeInt; |
| 183 |
memory.append(newMem); |
| 184 |
T **newMemPtr = new T *[vecSize]; |
| 185 |
memoryPtr.append(newMemPtr); |
| 186 |
for (int i = 0; i < currExtSizeInt; ++i) { |
| 187 |
newMemPtr[i] = &(newMem[i]); |
| 188 |
} |
| 189 |
for (int i = currExtSizeInt; i < vecSize; ++i) { |
| 190 |
newMemPtr[i] = 0; |
| 191 |
} |
| 192 |
free.append(newMemPtr); |
| 193 |
|
| 194 |
currExtSizeReal *= extendFac; |
| 195 |
if (currExtSizeReal > maxExtSize) { |
| 196 |
currExtSizeReal = maxExtSize; |
| 197 |
} |
| 198 |
currExtSizeInt = (qint64)currExtSizeReal; |
| 199 |
} |
| 200 |
|
| 201 |
|
| 202 |
template<class T> |
| 203 |
T *CDynamicExpandingMemoryManager<T>::allocate() { |
| 204 |
|
| 205 |
T *memElement = 0; |
| 206 |
while (!memElement) { |
| 207 |
if (nextMemVec) { |
| 208 |
memElement = nextMemVec[nextMemVecIndex++]; |
| 209 |
if (nextMemVecIndex >= vecSize) { |
| 210 |
freeMemPtr.append(nextMemVec); |
| 211 |
nextMemVec = 0; |
| 212 |
} |
| 213 |
} |
| 214 |
if (memElement == 0) { |
| 215 |
if (free.isEmpty()) { |
| 216 |
extendMemory(); |
| 217 |
} |
| 218 |
nextMemVec = free.takeFirst(); |
| 219 |
nextMemVecIndex = 0; |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
freeElements--; |
| 224 |
if (hasAllocationContext) { |
| 225 |
CAllocationObject *allCon = (CAllocationObject *)(memElement); |
| 226 |
allCon->setMemoryReleaser(this); |
| 227 |
} |
| 228 |
return memElement; |
| 229 |
} |
| 230 |
|
| 231 |
|
| 232 |
template<class T> |
| 233 |
void CDynamicExpandingMemoryManager<T>::release(T *&mem) { |
| 234 |
|
| 235 |
if (releaseMemVec == 0) { |
| 236 |
if (!freeMemPtr.isEmpty()) { |
| 237 |
releaseMemVec = freeMemPtr.takeFirst(); |
| 238 |
} else { |
| 239 |
releaseMemVec = new T *[vecSize]; |
| 240 |
memoryPtr.append(releaseMemVec); |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
freeElements++; |
| 245 |
releaseMemVec[releaseMemVecIndex++] = mem; |
| 246 |
mem = 0; |
| 247 |
|
| 248 |
if (releaseMemVecIndex >= vecSize) { |
| 249 |
releaseMemVecIndex = 0; |
| 250 |
if (releaseMemVec != 0) { |
| 251 |
free.append(releaseMemVec); |
| 252 |
releaseMemVec = 0; |
| 253 |
} |
| 254 |
} |
| 255 |
} |
| 256 |
|
| 257 |
template<class T> |
| 258 |
void CDynamicExpandingMemoryManager<T>::release(CAllocationObject *object) { |
| 259 |
T *mem = (T*)object; |
| 260 |
release(mem); |
| 261 |
} |
| 262 |
|
| 263 |
}; // end namespace Utilities |
| 264 |
|
| 265 |
}; // end namespace Konclude |
| 266 |
|
| 267 |
#endif // KONCLUDE_UTILITIES_CDYNAMICEXPANDINGMEMORYMANAGER_H |