| 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 |
#include "CMemoryPoolAllocationManager.h" |
| 22 |
|
| 23 |
|
| 24 |
namespace Konclude { |
| 25 |
|
| 26 |
namespace Utilities { |
| 27 |
|
| 28 |
namespace Memory { |
| 29 |
|
| 30 |
|
| 31 |
CMemoryPoolAllocationManager::CMemoryPoolAllocationManager(CMemoryPoolProvider* memoryPoolProvider) { |
| 32 |
mMemoryPoolProvider = memoryPoolProvider; |
| 33 |
mFreeMemoryPoolList = nullptr; |
| 34 |
mUsedMemoryPoolList = nullptr; |
| 35 |
mUsingMemoryPool = nullptr; |
| 36 |
mFreeMemoryPoolCount = 0; |
| 37 |
mStatMemToUsingCount = 0; |
| 38 |
mStatMemToFreeCount = 0; |
| 39 |
} |
| 40 |
|
| 41 |
CMemoryPoolAllocationManager::~CMemoryPoolAllocationManager() { |
| 42 |
// release all memory blocks |
| 43 |
while (mFreeMemoryPoolList) { |
| 44 |
CMemoryPool* tmpMemPool = mFreeMemoryPoolList; |
| 45 |
mFreeMemoryPoolList = mFreeMemoryPoolList->getNextMemoryPool(); |
| 46 |
tmpMemPool->clearNext(); |
| 47 |
mMemoryPoolProvider->releaseMemoryPool(tmpMemPool); |
| 48 |
} |
| 49 |
while (mUsedMemoryPoolList) { |
| 50 |
CMemoryPool* tmpMemPool = mUsedMemoryPoolList; |
| 51 |
mUsedMemoryPoolList = mUsedMemoryPoolList->getNextMemoryPool(); |
| 52 |
tmpMemPool->clearNext(); |
| 53 |
mMemoryPoolProvider->releaseMemoryPool(tmpMemPool); |
| 54 |
} |
| 55 |
if (mUsingMemoryPool) { |
| 56 |
mMemoryPoolProvider->releaseMemoryPool(mUsingMemoryPool); |
| 57 |
} |
| 58 |
mFreeMemoryPoolCount = 0; |
| 59 |
} |
| 60 |
|
| 61 |
void* CMemoryPoolAllocationManager::allocate(size_t size) { |
| 62 |
void* memPtr = 0; |
| 63 |
char* memBlock = 0; |
| 64 |
if (size >= 8) { |
| 65 |
memBlock = getMemoryBlock(size*sizeof(char),8); |
| 66 |
} else if (size >= 4) { |
| 67 |
memBlock = getMemoryBlock(size*sizeof(char),4); |
| 68 |
} else if (size >= 2) { |
| 69 |
memBlock = getMemoryBlock(size*sizeof(char),2); |
| 70 |
} else { |
| 71 |
memBlock = getMemoryBlock(size*sizeof(char)); |
| 72 |
} |
| 73 |
memPtr = memBlock; |
| 74 |
return memPtr; |
| 75 |
} |
| 76 |
|
| 77 |
CMemoryAllocationManager* CMemoryPoolAllocationManager::release(void* address) { |
| 78 |
// not supported by this pool memory manager, use releaseAllMemory() |
| 79 |
return this; |
| 80 |
} |
| 81 |
|
| 82 |
void* CMemoryPoolAllocationManager::allocateAligned(size_t size, int alignment) { |
| 83 |
void* memPtr = 0; |
| 84 |
char* memBlock = getMemoryBlock(size*sizeof(char),alignment); |
| 85 |
memPtr = memBlock; |
| 86 |
return memPtr; |
| 87 |
} |
| 88 |
|
| 89 |
char* CMemoryPoolAllocationManager::getMemoryBlock(cint64 memoryBlockSize) { |
| 90 |
char* memBlock = 0; |
| 91 |
bool allocated = false; |
| 92 |
while (!allocated) { |
| 93 |
if (!mUsingMemoryPool) { |
| 94 |
if (!mFreeMemoryPoolList) { |
| 95 |
mFreeMemoryPoolList = mMemoryPoolProvider->acquireMemoryPool(memoryBlockSize); |
| 96 |
++mFreeMemoryPoolCount; |
| 97 |
} |
| 98 |
mUsingMemoryPool = mFreeMemoryPoolList; |
| 99 |
mFreeMemoryPoolList = mFreeMemoryPoolList->getNextMemoryPool(); |
| 100 |
mUsingMemoryPool->clearNext(); |
| 101 |
--mFreeMemoryPoolCount; |
| 102 |
++mStatMemToUsingCount; |
| 103 |
} |
| 104 |
if (mUsingMemoryPool) { |
| 105 |
char* memBlockEnd = mUsingMemoryPool->getMemoryBlockEnd(); |
| 106 |
char* memBlockPointer = mUsingMemoryPool->getMemoryBlockPointer(); |
| 107 |
if (memBlockPointer+memoryBlockSize <= memBlockEnd) { |
| 108 |
memBlock = memBlockPointer; |
| 109 |
mUsingMemoryPool->setMemoryBlockPointer(memBlockPointer+memoryBlockSize); |
| 110 |
allocated = true; |
| 111 |
} else { |
| 112 |
// move pool to used |
| 113 |
CMemoryPool* tmpMemPool = mUsingMemoryPool; |
| 114 |
mUsedMemoryPoolList = tmpMemPool->setNextMemoryPool(mUsedMemoryPoolList); |
| 115 |
mUsingMemoryPool = nullptr; |
| 116 |
} |
| 117 |
} |
| 118 |
} |
| 119 |
return memBlock; |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
char* CMemoryPoolAllocationManager::getMemoryBlock(cint64 memoryBlockSize, cint64 alignment) { |
| 124 |
char* memBlock = 0; |
| 125 |
bool allocated = false; |
| 126 |
while (!allocated) { |
| 127 |
if (!mUsingMemoryPool) { |
| 128 |
if (!mFreeMemoryPoolList) { |
| 129 |
mFreeMemoryPoolList = mMemoryPoolProvider->acquireMemoryPool(memoryBlockSize+alignment); |
| 130 |
++mFreeMemoryPoolCount; |
| 131 |
} |
| 132 |
mUsingMemoryPool = mFreeMemoryPoolList; |
| 133 |
mFreeMemoryPoolList = mFreeMemoryPoolList->getNextMemoryPool(); |
| 134 |
mUsingMemoryPool->clearNext(); |
| 135 |
--mFreeMemoryPoolCount; |
| 136 |
++mStatMemToUsingCount; |
| 137 |
} |
| 138 |
if (mUsingMemoryPool) { |
| 139 |
char* memBlockEnd = mUsingMemoryPool->getMemoryBlockEnd(); |
| 140 |
char* memBlockPointer = mUsingMemoryPool->getMemoryBlockPointer(); |
| 141 |
cint64 maxBlockAlloc = (cint64)memBlockPointer+(cint64)memoryBlockSize+alignment; |
| 142 |
if (maxBlockAlloc <= (cint64)memBlockEnd) { |
| 143 |
// alignment is 2^N |
| 144 |
cint64 alignmentMask = alignment-1; |
| 145 |
memBlock = memBlockPointer; |
| 146 |
if (alignmentMask & cint64(memBlock)) { |
| 147 |
memBlock = (char*)((cint64(memBlock) & ~alignmentMask) + alignment); |
| 148 |
} |
| 149 |
mUsingMemoryPool->setMemoryBlockPointer(memBlock+memoryBlockSize); |
| 150 |
allocated = true; |
| 151 |
} else { |
| 152 |
// move pool to used |
| 153 |
CMemoryPool* tmpMemPool = mUsingMemoryPool; |
| 154 |
mUsedMemoryPoolList = tmpMemPool->setNextMemoryPool(mUsedMemoryPoolList); |
| 155 |
mUsingMemoryPool = nullptr; |
| 156 |
} |
| 157 |
} |
| 158 |
} |
| 159 |
return memBlock; |
| 160 |
} |
| 161 |
|
| 162 |
|
| 163 |
CMemoryTemporaryAllocationManager* CMemoryPoolAllocationManager::releaseAllMemory() { |
| 164 |
// move all used memory pools to free list |
| 165 |
CMemoryPool* tmpMemPool = nullptr; |
| 166 |
while (mUsedMemoryPoolList) { |
| 167 |
tmpMemPool = mUsedMemoryPoolList; |
| 168 |
tmpMemPool->resetMemoryBlockPointer(); |
| 169 |
mUsedMemoryPoolList = mUsedMemoryPoolList->getNextMemoryPool(); |
| 170 |
mFreeMemoryPoolList = tmpMemPool->setNextMemoryPool(mFreeMemoryPoolList); |
| 171 |
++mFreeMemoryPoolCount; |
| 172 |
++mStatMemToFreeCount; |
| 173 |
} |
| 174 |
if (mUsingMemoryPool) { |
| 175 |
mUsingMemoryPool->resetMemoryBlockPointer(); |
| 176 |
mFreeMemoryPoolList = mUsingMemoryPool->setNextMemoryPool(mFreeMemoryPoolList); |
| 177 |
++mFreeMemoryPoolCount; |
| 178 |
++mStatMemToFreeCount; |
| 179 |
mUsingMemoryPool = nullptr; |
| 180 |
} |
| 181 |
return this; |
| 182 |
} |
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
CMemoryPool* CMemoryPoolAllocationManager::acqireTemporaryMemoryPool(size_t size) { |
| 187 |
CMemoryPool* tmpMemoryPool = nullptr; |
| 188 |
if (mFreeMemoryPoolList) { |
| 189 |
if (mFreeMemoryPoolList->getMemoryBlockSize() >= size) { |
| 190 |
tmpMemoryPool = mFreeMemoryPoolList; |
| 191 |
mFreeMemoryPoolList = mFreeMemoryPoolList->getNextMemoryPool(); |
| 192 |
--mFreeMemoryPoolCount; |
| 193 |
} |
| 194 |
} |
| 195 |
if (!tmpMemoryPool) { |
| 196 |
tmpMemoryPool = mMemoryPoolProvider->acquireMemoryPool(size); |
| 197 |
} |
| 198 |
if (tmpMemoryPool) { |
| 199 |
++mStatMemToUsingCount; |
| 200 |
tmpMemoryPool->clearNext(); |
| 201 |
} |
| 202 |
return tmpMemoryPool; |
| 203 |
} |
| 204 |
|
| 205 |
|
| 206 |
CMemoryTemporaryAllocationManager* CMemoryPoolAllocationManager::releaseTemporaryMemoryPool(CMemoryPool* memoryPool) { |
| 207 |
memoryPool->resetMemoryBlockPointer(); |
| 208 |
memoryPool->setNextMemoryPool(mFreeMemoryPoolList); |
| 209 |
mFreeMemoryPoolList = memoryPool; |
| 210 |
++mFreeMemoryPoolCount; |
| 211 |
++mStatMemToFreeCount; |
| 212 |
return this; |
| 213 |
} |
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
cint64 CMemoryPoolAllocationManager::getOptimizedMoreAllocationSize(cint64 size) { |
| 218 |
if (mUsingMemoryPool) { |
| 219 |
cint64 availableSizeThisPool = mUsingMemoryPool->getMemoryBlockEnd() - mUsingMemoryPool->getMemoryBlockData(); |
| 220 |
if (availableSizeThisPool >= size) { |
| 221 |
return qMax(availableSizeThisPool,size<<2); |
| 222 |
} else if (mUsingMemoryPool->getMemoryBlockSize() >= size) { |
| 223 |
return qMax(mUsingMemoryPool->getMemoryBlockSize(),size<<2); |
| 224 |
} |
| 225 |
} else if (mFreeMemoryPoolList) { |
| 226 |
cint64 availableSizeThisPool = mFreeMemoryPoolList->getMemoryBlockEnd() - mFreeMemoryPoolList->getMemoryBlockData(); |
| 227 |
if (availableSizeThisPool >= size) { |
| 228 |
return qMax(availableSizeThisPool,size<<2); |
| 229 |
} else if (mUsingMemoryPool->getMemoryBlockSize() >= size) { |
| 230 |
return qMax(mUsingMemoryPool->getMemoryBlockSize(),size<<2); |
| 231 |
} |
| 232 |
} |
| 233 |
return size; |
| 234 |
} |
| 235 |
|
| 236 |
|
| 237 |
CMemoryTemporaryAllocationManager* CMemoryPoolAllocationManager::releaseMemoryPoolContainer(CMemoryPoolContainer* memoryPoolContainer) { |
| 238 |
CMemoryPool* memoryPoolLinker = memoryPoolContainer->takeMemoryPools(); |
| 239 |
return releaseTemporaryMemoryPools(memoryPoolLinker); |
| 240 |
} |
| 241 |
|
| 242 |
|
| 243 |
cint64 CMemoryPoolAllocationManager::getFreeMemoryPoolCount() { |
| 244 |
return mFreeMemoryPoolCount; |
| 245 |
} |
| 246 |
|
| 247 |
CMemoryTemporaryAllocationManager* CMemoryPoolAllocationManager::addUsedMemoryPools(CMemoryPool* memoryPools) { |
| 248 |
mUsedMemoryPoolList = memoryPools->append(mUsedMemoryPoolList); |
| 249 |
return this; |
| 250 |
} |
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
}; // end namespace Memory |
| 255 |
|
| 256 |
}; // end namespace Utilities |
| 257 |
|
| 258 |
}; // end namespace Konclude |
| 259 |
|