Debian Codegraph @

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