Divide Framework 0.1
A free and open-source 3D Framework under heavy development
Loading...
Searching...
No Matches
ECSMM.h
Go to the documentation of this file.
1/*
2 Author : Tobias Stein
3 Date : 6th September, 2017
4 File : ECSMM.h
5
6 Internal used memory manager.
7
8 All Rights Reserved. (c) Copyright 2016 - 2017.
9*/
10
11#pragma once
12#ifndef ECS_ECSSMM_H__
13#define ECS_ECSSMM_H__
14
15#define ECS_GLOBAL_MEMORY_CAPACITY 134217728 // 128 MB
16
17#include "API.h"
18
20
21namespace ECS { namespace Memory {
22
23namespace Allocator {
24 class StackAllocator;
25}
26
27namespace Internal {
28
30 {
32
34
36
37 public:
38
40
41 private:
42
43 // Pointer to global allocated memory
45
46 // Allocator used to manager memory allocation from global memory
48
49 std::vector<std::pair<const char*, void*>> m_PendingMemory;
50
51 std::list<void*> m_FreedMemory;
52
53 MemoryManager(const MemoryManager&) = delete;
55
56 public:
57
60
61
62 inline void* Allocate(size_t memSize, const char* user = nullptr)
63 {
64 LOG_DEBUG("{} allocated {} bytes of global memory.", user != nullptr ? user : "Unknown", memSize);
65 void* pMemory = m_MemoryAllocator->allocate(memSize, alignof(u8));
66
67 this->m_PendingMemory.push_back(std::pair<const char*, void*>(user, pMemory));
68
69 return pMemory;
70 }
71
72 inline void Free(void* pMem)
73 {
74 if (pMem == this->m_PendingMemory.back().second)
75 {
76 this->m_MemoryAllocator->free(pMem);
77 this->m_PendingMemory.pop_back();
78
79 bool bCheck = true;
80 while(bCheck == true)
81 {
82 bCheck = false;
83
84 // do not report already freed memory blocks.
85 for (auto it : this->m_FreedMemory)
86 {
87 if (it == this->m_PendingMemory.back().second)
88 {
89 this->m_MemoryAllocator->free(pMem);
90 this->m_PendingMemory.pop_back();
91 this->m_FreedMemory.remove(it);
92
93 bCheck = true;
94 break;
95 }
96 }
97 }
98
99 }
100 else
101 {
102 this->m_FreedMemory.push_back(pMem);
103 }
104 }
105
106 void CheckMemoryLeaks();
107
108 }; // class MemoryManager
109
110}}} // namespace ECS::Memory::Internal
111
112#endif // ECS_ECSSMM_H__
#define ECS_GLOBAL_MEMORY_CAPACITY
Definition: ECSMM.h:15
#define DECLARE_LOGGER
Definition: LoggerMacro.h:15
#define LOG_DEBUG(format,...)
Definition: LoggerMacro.h:24
virtual void free(void *p) override
virtual void * allocate(size_t size, u8 alignment) override
void * Allocate(size_t memSize, const char *user=nullptr)
Definition: ECSMM.h:62
MemoryManager(const MemoryManager &)=delete
static constexpr size_t MEMORY_CAPACITY
Definition: ECSMM.h:39
std::vector< std::pair< const char *, void * > > m_PendingMemory
Definition: ECSMM.h:49
std::list< void * > m_FreedMemory
Definition: ECSMM.h:51
StackAllocator * m_MemoryAllocator
Definition: ECSMM.h:47
MemoryManager & operator=(MemoryManager &)=delete
uint8_t u8
Definition: Platform.h:49