blob: 42ea018727593ae86737a1f0c2541aacb6873e3a (
plain)
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
|
/*
* ========================================================
*
* HCore
* Copyright Mahrouss Logic, all rights reserved.
*
* ========================================================
*/
#include <NewKit/KernelHeap.hpp>
//! @file KernelHeap.cpp
//! @brief Kernel allocator.
#define kMaxWrappers (4096 * 8)
namespace HCore {
static Ref<PTEWrapper *> kWrapperList[kMaxWrappers];
static SizeT kWrapperCount = 0UL;
static Ref<PTEWrapper *> kLastWrapper;
static Pmm kPmm;
namespace Detail {
static voidPtr find_ptr(const SizeT &sz, const bool rw, const bool user) {
for (SizeT indexWrapper = 0; indexWrapper < kMaxWrappers; ++indexWrapper) {
if (!kWrapperList[indexWrapper]->Present()) {
kWrapperList[indexWrapper]
->Reclaim(); /* very straight-forward as you can see. */
return reinterpret_cast<voidPtr>(
kWrapperList[indexWrapper]->VirtualAddress());
}
}
return nullptr;
}
} // namespace Detail
/// @brief manual allocation
/// @param sz size of pointer
/// @param rw read write (true to enable it)
/// @param user is it accesible by user processes?
/// @return the pointer
VoidPtr ke_new_ke_heap(const SizeT &sz, const bool rw, const bool user) {
if (kWrapperCount < sz) return nullptr;
if (auto ptr = Detail::find_ptr(sz, rw, user); ptr) return ptr;
Ref<PTEWrapper *> wrapper = kPmm.RequestPage(user, rw);
if (wrapper) {
kLastWrapper = wrapper;
kWrapperList[kWrapperCount] = wrapper;
++kWrapperCount;
return reinterpret_cast<voidPtr>(wrapper->VirtualAddress());
}
return nullptr;
}
/// @brief Declare pointer as free.
/// @param ptr the pointer.
/// @return
Int32 ke_delete_ke_heap(voidPtr ptr) {
if (ptr) {
const UIntPtr virtualAddress = reinterpret_cast<UIntPtr>(ptr);
if (kLastWrapper && virtualAddress == kLastWrapper->VirtualAddress()) {
return kPmm.FreePage(kLastWrapper);
}
Ref<PTEWrapper *> wrapper;
for (SizeT indexWrapper = 0; indexWrapper < kWrapperCount; ++indexWrapper) {
if (kWrapperList[indexWrapper]->VirtualAddress() == virtualAddress) {
wrapper = kWrapperList[indexWrapper];
return kPmm.FreePage(wrapper);
}
}
}
return -1;
}
/// @brief find pointer in kernel heap
/// @param ptr the pointer
/// @return if it exists.
Boolean kernel_valid_ptr(voidPtr ptr) {
if (ptr) {
const UIntPtr virtualAddress = reinterpret_cast<UIntPtr>(ptr);
if (kLastWrapper && virtualAddress == kLastWrapper->VirtualAddress()) {
return true;
}
Ref<PTEWrapper *> wrapper;
for (SizeT indexWrapper = 0; indexWrapper < kWrapperCount; ++indexWrapper) {
if (kWrapperList[indexWrapper]->VirtualAddress() == virtualAddress) {
wrapper = kWrapperList[indexWrapper];
return true;
}
}
}
return false;
}
/// @brief The Kernel heap initializer function.
/// @return
Void ke_init_ke_heap() noexcept
{
kWrapperCount = 0UL;
Ref<PTEWrapper *> kLastWrapper = Ref<PTEWrapper*>(nullptr);
Pmm kPmm = Pmm();
}
} // namespace HCore
|