Debian Codegraph @

Source/Utilities/Memory/CNewCentralizedLimitedAllocationMemoryPoolProvider.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 "CNewCentralizedLimitedAllocationMemoryPoolProvider.h"
22
23
24 namespace Konclude {
25
26 namespace Utilities {
27
28 namespace Memory {
29
30
31 CNewCentralizedLimitedAllocationMemoryPoolProvider::CNewCentralizedLimitedAllocationMemoryPoolProvider(CAllocationLimitator* allocLimitator, cint64 defaultPoolSize) {
32 mDefaultPoolSize = defaultPoolSize;
33 mStatAllocatedPoolSize = 0;
34 mStatAllocatedPoolCount = 0;
35 mStatReleasedPoolSize = 0;
36 mStatReleasedPoolCount = 0;
37 mStatDiffPoolSize = 0;
38 mStatDiffPoolCount = 0;
39 mAllocLimitator = allocLimitator;
40 }
41
42 CNewCentralizedLimitedAllocationMemoryPoolProvider::~CNewCentralizedLimitedAllocationMemoryPoolProvider() {
43 }
44
45
46 CMemoryPool* CNewCentralizedLimitedAllocationMemoryPoolProvider::acquireMemoryPool(cint64 minPoolSize) {
47 cint64 memoryBlockSize = qMax(mDefaultPoolSize,minPoolSize);
48 mAllocLimitator->addAllocatingMemorySize(memoryBlockSize+sizeof(CMemoryPool));
49 CMemoryPool* memoryPool = new CMemoryPool();
50 char* memoryBlock = new char[memoryBlockSize];
51 mStatAllocatedPoolSize += memoryBlockSize;
52 mStatDiffPoolSize += memoryBlockSize;
53 ++mStatAllocatedPoolCount;
54 ++mStatDiffPoolCount;
55 memoryPool->setMemoryBlockData(memoryBlock,memoryBlockSize);
56 return memoryPool;
57 }
58
59
60 CMemoryPool* CNewCentralizedLimitedAllocationMemoryPoolProvider::acquireMemoryPoolConsiderated(cint64 minPoolSize) {
61 cint64 memoryBlockSize = qMax(mDefaultPoolSize,minPoolSize);
62 if (mAllocLimitator->addAllocatingMemorySize(memoryBlockSize+sizeof(CMemoryPool))) {
63 CMemoryPool* memoryPool = new CMemoryPool();
64 char* memoryBlock = new char[memoryBlockSize];
65 mStatAllocatedPoolSize += memoryBlockSize;
66 mStatDiffPoolSize += memoryBlockSize;
67 ++mStatAllocatedPoolCount;
68 ++mStatDiffPoolCount;
69 memoryPool->setMemoryBlockData(memoryBlock,memoryBlockSize);
70 return memoryPool;
71 } else {
72 throw CMemoryAllocationException();
73 }
74 return nullptr;
75 }
76
77
78
79 CMemoryPoolProvider* CNewCentralizedLimitedAllocationMemoryPoolProvider::releaseMemoryPool(CMemoryPool* memoryPool) {
80 while (memoryPool) {
81 CMemoryPool* tmpMemoryPool = memoryPool;
82 memoryPool = memoryPool->getNext();
83 mStatReleasedPoolSize += tmpMemoryPool->getMemoryBlockSize();
84 mStatDiffPoolSize -= tmpMemoryPool->getMemoryBlockSize();
85 mAllocLimitator->addReleasedMemorySize(tmpMemoryPool->getMemoryBlockSize()+sizeof(CMemoryPool));
86 ++mStatReleasedPoolCount;
87 --mStatDiffPoolCount;
88 char* memoryBlock = tmpMemoryPool->getMemoryBlockData();
89 delete[] memoryBlock;
90 delete tmpMemoryPool;
91 }
92 return this;
93 }
94
95
96 cint64 CNewCentralizedLimitedAllocationMemoryPoolProvider::getAllocatedReleaseDifferencePoolCount() {
97 return mStatDiffPoolCount;
98 }
99
100 cint64 CNewCentralizedLimitedAllocationMemoryPoolProvider::getAllocatedReleaseDifferencePoolSize() {
101 return mStatDiffPoolSize;
102 }
103
104
105
106
107 }; // end namespace Memory
108
109 }; // end namespace Utilities
110
111 }; // end namespace Konclude
112