-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGCMemoryManager.h
68 lines (47 loc) · 1.34 KB
/
GCMemoryManager.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#ifndef CPPGCPTR_GCMEMORYMANAGER_H
#define CPPGCPTR_GCMEMORYMANAGER_H
#include <iostream>
#include <vector>
#include <queue>
#include <stack>
#include <deque>
#include <list>
#include <map>
#include <mutex>
#include "IAllocatable.h"
#include "GCParameter.h"
class MemoryBlock {
private:
void* address;
public:
size_t size;
MemoryBlock(void* start_address, size_t size_) : address(start_address), size(size_) {
}
MemoryBlock() = delete;
void* getEndAddress() {
return reinterpret_cast<void*>(reinterpret_cast<char*>(address) + size);
}
void* getStartAddress() {
return address;
}
void shrink_from_head(size_t);
void shrink_from_back(size_t);
void grow_from_head(size_t);
void grow_from_back(size_t);
};
class GCMemoryManager : public IAllocatable {
private:
std::list<MemoryBlock> freeList;
std::map<void*, size_t> new_mem_map;
std::recursive_mutex allocate_mutex_;
void free_new_mem(const decltype(new_mem_map)::iterator&);
public:
GCMemoryManager() = default;
GCMemoryManager(const GCMemoryManager&) = delete;
GCMemoryManager(GCMemoryManager&&) noexcept;
void* allocate(size_t size) override;
void free(void* address, size_t size) override;
void add_memory(size_t size);
void return_reserved();
};
#endif //CPPGCPTR_GCMEMORYMANAGER_H