Debian Codegraph @

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