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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
|
/*
* ========================================================
*
* HCore
* Copyright Mahrouss Logic, all rights reserved.
*
* ========================================================
*/
#include <NewKit/PageManager.hpp>
#include <NewKit/UserHeap.hpp>
/// @file Heap.cxx
/// @brief Heap Manager, user mode allocator.
/// @note if you want to look at kernel allocs, please look for KHeap.cxx
/// bugs: 0
namespace HCore {
class HeapManager final {
public:
static SizeT& GetCount() { return s_NumPools; }
static Ref<Pmm>& GetPmm() { return s_Pmm; }
static Boolean& IsEnabled() { return s_PoolsAreEnabled; }
static Array<Ref<PTEWrapper*>, kPoolMaxSz>& The() { return s_Pool; }
private:
static Size s_NumPools;
static Ref<Pmm> s_Pmm;
private:
static Boolean s_PoolsAreEnabled;
static Array<Ref<PTEWrapper*>, kPoolMaxSz> s_Pool;
};
//! declare fields
SizeT HeapManager::s_NumPools = 0UL;
Ref<Pmm> HeapManager::s_Pmm;
Boolean HeapManager::s_PoolsAreEnabled = true;
Array<Ref<PTEWrapper*>, kPoolMaxSz> HeapManager::s_Pool;
static voidPtr ke_find_unused_heap(Int flags);
static void ke_free_heap_internal(voidPtr vaddr);
static voidPtr ke_make_heap(voidPtr vaddr, Int flags);
static bool ke_check_and_free_heap(const SizeT& index, voidPtr ptr);
static voidPtr ke_find_unused_heap(Int flags) {
for (SizeT index = 0; index < kPoolMaxSz; ++index) {
if (HeapManager::The()[index] &&
!HeapManager::The()[index].Leak().Leak().Leak()->Present()) {
HeapManager::GetPmm().Leak().TogglePresent(
HeapManager::The()[index].Leak().Leak(), true);
kcout << "[ke_find_unused_heap] Done, trying now to make a pool\r\n";
return ke_make_heap((voidPtr)HeapManager::The()[index]
.Leak()
.Leak()
.Leak()
->VirtualAddress(),
flags);
}
}
return nullptr;
}
static voidPtr ke_make_heap(voidPtr virtualAddress, Int flags) {
if (virtualAddress) {
HeapHeader* pool_hdr = reinterpret_cast<HeapHeader*>(virtualAddress);
if (!pool_hdr->Free) {
kcout << "[ke_make_heap] pool_hdr->Free, Pool already exists\n";
return nullptr;
}
pool_hdr->Flags = flags;
pool_hdr->Magic = kPoolMag;
pool_hdr->Free = false;
kcout << "[ke_make_heap] New allocation has been done.\n";
return reinterpret_cast<voidPtr>(
(reinterpret_cast<UIntPtr>(virtualAddress) + sizeof(HeapHeader)));
}
kcout << "[ke_make_heap] Address is invalid";
return nullptr;
}
static void ke_free_heap_internal(voidPtr virtualAddress) {
HeapHeader* pool_hdr = reinterpret_cast<HeapHeader*>(
reinterpret_cast<UIntPtr>(virtualAddress) - sizeof(HeapHeader));
if (pool_hdr->Magic == kPoolMag) {
pool_hdr->Free = false;
pool_hdr->Flags = 0;
kcout << "[ke_free_heap_internal] Successfully marked header as free!\r\n";
}
}
static bool ke_check_and_free_heap(const SizeT& index, voidPtr ptr) {
if (HeapManager::The()[index]) {
// ErrorOr<>::operator bool
if (!HeapManager::The()[index].Leak().Leak().IsStrong()) {
// we want them to be weak
// because we allocated it.
if (HeapManager::The()[index].Leak().Leak().Leak()->VirtualAddress() ==
(UIntPtr)ptr) {
HeapManager::GetPmm().Leak().FreePage(
HeapManager::The()[index].Leak().Leak());
--HeapManager::GetCount();
ke_free_heap_internal(ptr);
ptr = nullptr;
return true;
}
}
}
return false;
}
/// @brief Creates a new pool pointer.
/// @param flags the flags attached to it.
/// @return a pool pointer with selected permissions.
voidPtr ke_new_heap(Int32 flags) {
if (!HeapManager::IsEnabled()) return nullptr;
if (HeapManager::GetCount() > kPoolMaxSz) return nullptr;
if (voidPtr ret = ke_find_unused_heap(flags)) return ret;
// this wasn't set to true
auto ref_page = HeapManager::GetPmm().Leak().RequestPage(
((flags & kPoolUser)), (flags & kPoolRw));
if (ref_page) {
///! reserve page.
HeapManager::The()[HeapManager::GetCount()].Leak() = ref_page;
auto& ref = HeapManager::GetCount();
++ref; // increment the number of addresses we have now.
kcout << "[ke_new_heap] New Address found!\r\n";
// finally make the pool address.
return ke_make_heap(
reinterpret_cast<voidPtr>(ref_page.Leak()->VirtualAddress()), flags);
}
return nullptr;
}
/// @brief free a pool pointer.
/// @param ptr The pool pointer to free.
/// @return status code
Int32 ke_free_heap(voidPtr ptr) {
if (!HeapManager::IsEnabled()) return -1;
if (ptr) {
SizeT base = HeapManager::GetCount();
if (ke_check_and_free_heap(base, ptr)) return 0;
for (SizeT index = 0; index < kPoolMaxSz; ++index) {
if (ke_check_and_free_heap(index, ptr)) return 0;
--base;
}
}
return -1;
}
/// @brief Init HeapManager, set GetCount to zero and IsEnabled to true.
/// @return
Void ke_init_heap() {
HeapManager::GetCount() = 0UL;
HeapManager::IsEnabled() = true;
}
} // namespace HCore
|