Debian Codegraph @

Source/Utilities/Memory/CTempMemoryPoolContainerAllocationManager.cpp

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 #include "CTempMemoryPoolContainerAllocationManager.h"
22
23
24 namespace Konclude {
25
26 namespace Utilities {
27
28 namespace Memory {
29
30
31 CTempMemoryPoolContainerAllocationManager::CTempMemoryPoolContainerAllocationManager(CMemoryPoolContainer* memoryPoolContainer, CMemoryPoolProvider* memoryPoolProvider) {
32 initTempMemoryPoolContainerAllocationManager(memoryPoolContainer,memoryPoolProvider);
33 }
34
35 CTempMemoryPoolContainerAllocationManager::CTempMemoryPoolContainerAllocationManager(CMemoryPoolProvider* memoryPoolProvider) {
36 initTempMemoryPoolContainerAllocationManager(nullptr,memoryPoolProvider);
37 }
38
39 CTempMemoryPoolContainerAllocationManager::CTempMemoryPoolContainerAllocationManager() {
40 initTempMemoryPoolContainerAllocationManager(nullptr,nullptr);
41 }
42
43
44 CTempMemoryPoolContainerAllocationManager::~CTempMemoryPoolContainerAllocationManager() {
45 }
46
47 CMemoryPool* CTempMemoryPoolContainerAllocationManager::getMemoryPools() {
48 return mMemoryPoolContainer->getMemoryPools();
49 }
50
51 CMemoryPool* CTempMemoryPoolContainerAllocationManager::takeMemoryPools() {
52 return mMemoryPoolContainer->takeMemoryPools();
53 }
54
55
56 CTempMemoryPoolContainerAllocationManager* CTempMemoryPoolContainerAllocationManager::initTempMemoryPoolContainerAllocationManager(CMemoryPoolContainer* memoryPoolContainer, CMemoryPoolProvider* memoryPoolProvider) {
57 mMemoryPoolContainer = memoryPoolContainer;
58 mMemoryPoolProvider = memoryPoolProvider;
59 mStatPoolAllocCount = 0;
60 mStatAllocPoolSize = 0;
61 if (!mMemoryPoolContainer) {
62 mMemoryPoolContainer = &mTmpMemoryPoolContainer;
63 }
64 return this;
65 }
66
67
68 void* CTempMemoryPoolContainerAllocationManager::allocate(size_t size) {
69 void* memPtr = 0;
70 char* memBlock = 0;
71 if (size >= 8) {
72 memBlock = getMemoryBlock(size*sizeof(char),8);
73 } else if (size >= 4) {
74 memBlock = getMemoryBlock(size*sizeof(char),4);
75 } else if (size >= 2) {
76 memBlock = getMemoryBlock(size*sizeof(char),2);
77 } else {
78 memBlock = getMemoryBlock(size*sizeof(char));
79 }
80 memPtr = memBlock;
81 return memPtr;
82 }
83
84 CMemoryAllocationManager* CTempMemoryPoolContainerAllocationManager::release(void* address) {
85 // not supported by this pool memory manager, stored all memory in memory pool container
86 return this;
87 }
88
89 void* CTempMemoryPoolContainerAllocationManager::allocateAligned(size_t size, int alignment) {
90 void* memPtr = 0;
91 char* memBlock = getMemoryBlock(size*sizeof(char),alignment);
92 memPtr = memBlock;
93 return memPtr;
94 }
95
96 char* CTempMemoryPoolContainerAllocationManager::getMemoryBlock(cint64 memoryBlockSize) {
97 char* memBlock = 0;
98 bool allocated = false;
99 CMemoryPool* memoryPool = mMemoryPoolContainer->getMemoryPools();
100 while (!allocated) {
101 if (!memoryPool) {
102 memoryPool = mMemoryPoolProvider->acquireMemoryPool(memoryBlockSize);
103 ++mStatPoolAllocCount;
104 if (memoryPool) {
105 mStatAllocPoolSize += memoryPool->getMemoryBlockSize();
106 mMemoryPoolContainer->appendMemoryPool(memoryPool);
107 }
108 }
109 if (memoryPool) {
110 char* memBlockEnd = memoryPool->getMemoryBlockEnd();
111 char* memBlockPointer = memoryPool->getMemoryBlockPointer();
112 if (memBlockPointer+memoryBlockSize <= memBlockEnd) {
113 memBlock = memBlockPointer;
114 memoryPool->setMemoryBlockPointer(memBlockPointer+memoryBlockSize);
115 allocated = true;
116 } else {
117 memoryPool = nullptr;
118 }
119 }
120 }
121 return memBlock;
122 }
123
124
125 cint64 CTempMemoryPoolContainerAllocationManager::getOptimizedMoreAllocationSize(cint64 size) {
126 CMemoryPool* memoryPool = mMemoryPoolContainer->getMemoryPools();
127 if (memoryPool) {
128 cint64 availableSizeThisPool = memoryPool->getMemoryBlockEnd() - memoryPool->getMemoryBlockData();
129 if (availableSizeThisPool >= size) {
130 return qMax(availableSizeThisPool,size<<2);
131 } else if (memoryPool->getMemoryBlockSize() >= size) {
132 return qMax(memoryPool->getMemoryBlockSize(),size<<2);
133 }
134 }
135 return size;
136 }
137
138
139
140 char* CTempMemoryPoolContainerAllocationManager::getMemoryBlock(cint64 memoryBlockSize, cint64 alignment) {
141 char* memBlock = 0;
142 bool allocated = false;
143 CMemoryPool* memoryPool = mMemoryPoolContainer->getMemoryPools();
144 while (!allocated) {
145 if (!memoryPool) {
146 memoryPool = mMemoryPoolProvider->acquireMemoryPool(memoryBlockSize+alignment);
147 ++mStatPoolAllocCount;
148 if (memoryPool) {
149 mStatAllocPoolSize += memoryPool->getMemoryBlockSize();
150 mMemoryPoolContainer->appendMemoryPool(memoryPool);
151 }
152 }
153 if (memoryPool) {
154 char* memBlockEnd = memoryPool->getMemoryBlockEnd();
155 char* memBlockPointer = memoryPool->getMemoryBlockPointer();
156 cint64 maxBlockAlloc = (cint64)(memBlockPointer)+(cint64)(memoryBlockSize+alignment);
157 if (maxBlockAlloc <= (cint64)memBlockEnd) {
158 // alignment is 2^N
159 cint64 alignmentMask = alignment-1;
160 memBlock = memBlockPointer;
161 if (alignmentMask & cint64(memBlock)) {
162 memBlock = (char*)((cint64(memBlock) & ~alignmentMask) + alignment);
163 }
164 memoryPool->setMemoryBlockPointer(memBlock+memoryBlockSize);
165 allocated = true;
166 } else {
167 memoryPool = nullptr;
168 }
169 }
170 }
171 return memBlock;
172 }
173
174
175 void* CTempMemoryPoolContainerAllocationManager::allocateMemoryToContainer(size_t size, CMemoryPoolContainer* memoryPoolContainer, CMemoryPoolProvider* memoryPoolProvider) {
176 cint64 memoryBlockSize = size;
177 char* memBlock = 0;
178 bool allocated = false;
179 CMemoryPool* memoryPool = memoryPoolContainer->getMemoryPools();
180 while (!allocated) {
181 if (!memoryPool) {
182 memoryPool = memoryPoolProvider->acquireMemoryPool(memoryBlockSize);
183 memoryPoolContainer->appendMemoryPool(memoryPool);
184 }
185 if (memoryPool) {
186 char* memBlockEnd = memoryPool->getMemoryBlockEnd();
187 char* memBlockPointer = memoryPool->getMemoryBlockPointer();
188 if (memBlockPointer+memoryBlockSize <= memBlockEnd) {
189 memBlock = memBlockPointer;
190 memoryPool->setMemoryBlockPointer(memBlockPointer+memoryBlockSize);
191 allocated = true;
192 } else {
193 memoryPool = nullptr;
194 }
195 }
196 }
197 return memBlock;
198 }
199
200 }; // end namespace Memory
201
202 }; // end namespace Utilities
203
204 }; // end namespace Konclude