Debian Codegraph @

Source/Utilities/Memory/CMemoryAllocator.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_CMEMORYALLOCATOR_H
22 #define KONCLUDE_UTILITIES_MEMORY_CMEMORYALLOCATOR_H
23
24 // Library includes
25 #include <QtGlobal>
26
27 // Namespace includes
28 #include "MemorySettings.h"
29 #include "CMemoryAllocationManager.h"
30
31 // Other includes
32
33
34 // Logger includes
35 #include "Logger/CLogger.h"
36
37 using namespace std;
38
39 namespace Konclude {
40
41 namespace Utilities {
42
43 namespace Memory {
44
45
46 /*!
47 *
48 * \class CMemoryAllocator
49 * \author Andreas Steigmiller
50 * \version 0.1
51 * \brief TODO
52 *
53 */
54 class CMemoryAllocator {
55 // public methods
56 public:
57 //! Constructor
58 CMemoryAllocator() {
59 }
60
61 //! Destructor
62 virtual ~CMemoryAllocator() {
63 }
64
65 static inline void* allocateMemory(CMemoryAllocationManager* memoryAllocatorManager, size_t memorySize) {
66 void* memAddr = 0;
67 if (memoryAllocatorManager) {
68 memAddr = memoryAllocatorManager->allocate(memorySize);
69 } else {
70 memAddr = qMallocAligned(memorySize,0);
71 }
72 return memAddr;
73 }
74
75 static inline void* reallocateMemory(void* memAddr, CMemoryAllocationManager* memoryAllocatorManager, size_t prevMemorySize, size_t newMemorySize) {
76 void* newMemAddr = 0;
77 if (memoryAllocatorManager) {
78 newMemAddr = memoryAllocatorManager->allocate(newMemorySize);
79 ::memcpy(newMemAddr,memAddr,prevMemorySize);
80 memoryAllocatorManager->release(memAddr);
81 } else {
82 newMemAddr = qReallocAligned(memAddr,newMemorySize,prevMemorySize,0);
83 }
84 return newMemAddr;
85 }
86
87
88 static inline void* allocateAlignedMemory(CMemoryAllocationManager* memoryAllocatorManager, size_t memorySize, cint alignment) {
89 void* memAddr = 0;
90 if (memoryAllocatorManager) {
91 memAddr = memoryAllocatorManager->allocateAligned(memorySize,alignment);
92 } else {
93 memAddr = qMallocAligned(memorySize,alignment);
94 }
95 return memAddr;
96 }
97
98 static inline void releaseMemory(void* memAddr, CMemoryAllocationManager* memoryAllocatorManager) {
99 if (memoryAllocatorManager) {
100 memoryAllocatorManager->release(memAddr);
101 } else {
102 qFreeAligned(memAddr);
103 }
104 }
105
106 static inline void releaseAlignedMemory(void* memAddr, CMemoryAllocationManager* memoryAllocatorManager) {
107 if (memoryAllocatorManager) {
108 memoryAllocatorManager->release(memAddr);
109 } else {
110 qFreeAligned(memAddr);
111 }
112 }
113 // protected methods
114 protected:
115
116 // protected variables
117 protected:
118
119 // private methods
120 private:
121
122 // private variables
123 private:
124 };
125
126
127 }; // end namespace Memory
128
129 }; // end namespace Utilities
130
131 }; // end namespace Konclude
132
133
134 #endif // KONCLUDE_UTILITIES_MEMORY_CMEMORYALLOCATOR_H