summaryrefslogtreecommitdiff
path: root/Source/Kernel/MemoryManager/Mem.ns.cpp
blob: 6c64a536cc9613974566677a3ac2269b81e94bee (plain) (blame)
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
#include <common.h>
#include <MemoryManager/PhysMem.ns.h>
#include <MemoryManager/Heap.class.h>

namespace Mem {

bool pagingEnabled = false;
u32int placementAddress;

Heap kheap;

//Placement malloc, used while heap is not initialized
void *kallocInternal(u32int sz, bool align) {
	if (kheap.usable()) return 0;
	if (align && (placementAddress & 0xFFFFF000)) {
		placementAddress &= 0xFFFFF000;
		placementAddress += 0x1000;
	}
	u32int temp = placementAddress;
	placementAddress += sz;
	for (u32int i = temp; i < placementAddress; i += 0x1000) {
		if (pagingEnabled) kernelPageDirectory->allocFrame(i, false, false);
	}
	return (void*)temp;
}

//***************************************************************************
//***                      MEMORY MANAGMENT FUNCTIONS                     ***
//***************************************************************************
void createHeap() {
	u32int heapIndexSize = PhysMem::total() * 64 + 0x10000;
	u32int heapStart = (placementAddress & 0xFFFFF000) + 0x10000; //Set initial heap start
	u32int heapSize = HEAP_MIN_SIZE + heapIndexSize;							//Calculate heap size

	for (u32int i = (placementAddress & 0xFFFFF000); i < heapStart; i += 0x1000) {
		kernelPageDirectory->allocFrame(i, false, false);
	}

	kheap.create(heapStart, heapSize, heapIndexSize, kernelPageDirectory, false, false);
}

void *kalloc(u32int sz, bool align) {
	if (!kheap.usable()) return kallocInternal(sz, align);
	if (align) return 0;

	return kheap.alloc(sz);
}

void kfree(void *ptr) {
	kheap.free(ptr);
}

u32int kheapSize() {
	return kheap.size();
}

u32int kheapFree() {
	return kheap.free();
}

}