Debian Codegraph @

Source/Utilities/Memory/CCentralizedAllocationLimitator.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 "CCentralizedAllocationLimitator.h"
22
23
24 namespace Konclude {
25
26 namespace Utilities {
27
28 namespace Memory {
29
30
31 CCentralizedAllocationLimitator::CCentralizedAllocationLimitator(CCentralizedAllocationLimitation* limitation) : CLinkerBase<CCentralizedAllocationLimitator*,CCentralizedAllocationLimitator>(this) {
32 mMaxMemAllocSize = 0;
33 mMemAllocated = 0;
34 mReqUpdateFlag = true;
35 mAllocLimitation = limitation;
36 }
37
38 CCentralizedAllocationLimitator::~CCentralizedAllocationLimitator() {
39 }
40
41
42 cint64 CCentralizedAllocationLimitator::getRemainingMemoryAllocationSize() {
43 return mMaxMemAllocSize-mMemAllocated;
44 }
45
46 cint64 CCentralizedAllocationLimitator::getMaximumMemoryAllocationSize() {
47 return mMaxMemAllocSize;
48 }
49
50 cint64 CCentralizedAllocationLimitator::getMemoryAllocatedSize() {
51 return mMemAllocated;
52 }
53
54
55 bool CCentralizedAllocationLimitator::addAllocatingMemorySize(cint64 allocatedMemorySize) {
56 if (mReqUpdateFlag) {
57 mReqUpdateFlag = false;
58 mMaxMemAllocSize = mAllocLimitation->getUpdatedMemoryAllocationLimit(this);
59 }
60
61 if (getRemainingMemoryAllocationSize() >= allocatedMemorySize) {
62 mMemAllocated += allocatedMemorySize;
63 return true;
64 } else {
65 while (allocatedMemorySize > 0) {
66 mMaxMemAllocSize = mAllocLimitation->getUpdatedMemoryAllocationLimit(this);
67 if (getRemainingMemoryAllocationSize() >= allocatedMemorySize) {
68 mMemAllocated += allocatedMemorySize;
69 return true;
70 } else {
71 cint64 tmpMemAllocSize = getRemainingMemoryAllocationSize();
72 mMemAllocated += tmpMemAllocSize;
73 allocatedMemorySize -= tmpMemAllocSize;
74 }
75 }
76 }
77 return false;
78 }
79
80 CCentralizedAllocationLimitator* CCentralizedAllocationLimitator::addReleasedMemorySize(cint64 allocatedMemorySize) {
81 mMemAllocated -= allocatedMemorySize;
82 return this;
83 }
84
85 CCentralizedAllocationLimitator* CCentralizedAllocationLimitator::notifyRequireUpdateLimit() {
86 mReqUpdateFlag = true;
87 return this;
88 }
89
90
91
92 }; // end namespace Memory
93
94 }; // end namespace Utilities
95
96 }; // end namespace Konclude
97