-
Notifications
You must be signed in to change notification settings - Fork 0
/
memory.c
65 lines (51 loc) · 1.5 KB
/
memory.c
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
#include "memory.h"
#include "debug.h"
#include "vmm.h"
static heap_t kernel_heap;
void printf (const char *format, ...);
void init_kernel_heap() {
extern const void KernelStart, KernelEnd;
UNUSED(KernelStart);
uint32_t KernelEndAddr = (uint32_t)&KernelEnd;
kernel_heap.pos = kernel_heap.start = KernelEndAddr;
kernel_heap.allocated_end = KernelEndAddr + MB(4); //Since the first 4 MB are mapped from the beginning we can just use that space
kernel_heap.max_end = KernelEndAddr + MB(512);
}
void* kmalloc(uint32_t space, uint32_t alignment) {
if(alignment > 1)
kernel_heap.pos = ALIGN(kernel_heap.pos, alignment);
while((kernel_heap.pos + space) >= kernel_heap.allocated_end) {
alloc_frame(get_page(kernel_heap.allocated_end, false, kernel_directory), 0, 0);
kernel_heap.allocated_end += 0x1000;
}
uint32_t tmp;
tmp = kernel_heap.pos;
kernel_heap.pos += space;
return (void*)tmp;
}
void memset(void* ptr, uint8_t b, uint32_t size) {
for(uint32_t i = 0; i < size; i++)
*((uint8_t*)ptr+i) = b;
}
/*
* Simple Memcpy, copies char-by-char
* TODO implement better algorythm
* at the moment only fixes overlapping problem
*/
void memcpy(void* dest, void* src, uint32_t size) {
if(dest == src) return;
if(dest < src)
while(size-- > 0)
*(char*) dest++ = *(char*) src++;
else {
src += size;
dest += size;
while(size-- > 0)
*(char*) --dest = *(char*) --src;
}
}
void disable_pse() {
asm volatile ( "mov %%cr4, %%eax\n"
"and $~0x00000010, %%eax\n"
"mov %%eax, %%cr4":::"eax");
}