Debian Codegraph @

Source/Utilities/Memory/CObjectMemoryPoolAllocator.h

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 #ifndef KONCLUDE_UTILITIES_MEMORY_COBJECTMEMORYPOOLALLOCATOR_H
22 #define KONCLUDE_UTILITIES_MEMORY_COBJECTMEMORYPOOLALLOCATOR_H
23
24 // Library includes
25
26
27 // Namespace includes
28 #include "MemorySettings.h"
29 #include "CMemoryPoolProvider.h"
30 #include "CMemoryPool.h"
31
32 // Other includes
33
34
35 // Logger includes
36 #include "Logger/CLogger.h"
37
38 using namespace std;
39
40 namespace Konclude {
41
42 namespace Utilities {
43
44 namespace Memory {
45
46
47 /*!
48 *
49 * \class CObjectMemoryPoolAllocator
50 * \author Andreas Steigmiller
51 * \version 0.1
52 * \brief TODO
53 *
54 */
55 template<class T>
56 class CObjectMemoryPoolAllocator {
57 // public methods
58 public:
59 //! Constructor
60 CObjectMemoryPoolAllocator() {
61 }
62
63 //! Destructor
64 virtual ~CObjectMemoryPoolAllocator() {
65 }
66
67 static inline T* allocateAndConstructWithMemroyPool(CMemoryPoolProvider* memoryPoolProvider = nullptr) {
68 T* object = 0;
69 CMemoryPool* memoryPool = nullptr;
70 if (memoryPoolProvider) {
71 memoryPool = memoryPoolProvider->acquireMemoryPool(sizeof(T));
72 } else {
73 memoryPool = new CMemoryPool();
74 cint64 memorySize = qMax((cint64)sizeof(T),DEFAULTMEMORYPOOLSIZE);
75 char* memoryPoolMemory = new char[memorySize];
76 memoryPool->setMemoryBlockData(memoryPoolMemory,memorySize);
77 }
78 if (memoryPool) {
79 cint64 memorySize = (cint64)sizeof(T);
80 object = (T*)(memoryPool->getMemoryBlockPointer());
81 memoryPool->incMemoryBlockPointer(sizeof(T));
82 new (object) T(memoryPool);
83 }
84 return object;
85 }
86
87
88 static inline T* allocateAndConstructWithMemroyPool(CMemoryPool* memoryPool) {
89 T* object = 0;
90 if (memoryPool) {
91 cint64 memorySize = (cint64)sizeof(T);
92 object = (T*)(memoryPool->getMemoryBlockPointer());
93 memoryPool->incMemoryBlockPointer(sizeof(T));
94 new (object) T(memoryPool);
95 }
96 return object;
97 }
98
99
100 // protected methods
101 protected:
102
103 // protected variables
104 protected:
105
106 // private methods
107 private:
108
109 // private variables
110 private:
111 };
112
113
114 }; // end namespace Memory
115
116 }; // end namespace Utilities
117
118 }; // end namespace Konclude
119
120 #include "CObjectMemoryPoolAllocator.cpp"
121
122 #endif // KONCLUDE_UTILITIES_MEMORY_COBJECTMEMORYPOOLALLOCATOR_H