forked from samarthmahendraneu/virtual-memory-kernel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathp-allocator.cc
More file actions
52 lines (45 loc) · 1.57 KB
/
p-allocator.cc
File metadata and controls
52 lines (45 loc) · 1.57 KB
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
#include "u-lib.hh"
#ifndef ALLOC_SLOWDOWN
#define ALLOC_SLOWDOWN 100
#endif
extern uint8_t end[];
// These global variables go on the data page.
uint8_t* heap_top;
uint8_t* stack_bottom;
void process_main() {
pid_t p = sys_getpid();
srand(p);
// The heap starts on the page right after the 'end' symbol,
// whose address is the first address not allocated to process code
// or data.
heap_top = (uint8_t*) round_up((uintptr_t) end, PAGESIZE);
// The bottom of the stack is the first address on the current
// stack page (this process never needs more than one stack page).
stack_bottom = (uint8_t*) round_down((uintptr_t) rdrsp() - 1, PAGESIZE);
// Allocate heap pages until (1) hit the stack (out of address space)
// or (2) allocation fails (out of physical memory).
while (heap_top != stack_bottom) {
if (rand(0, ALLOC_SLOWDOWN - 1) < p) {
if (sys_page_alloc(heap_top) < 0) {
break;
}
// check that the page starts out all zero
for (unsigned long* l = (unsigned long*) heap_top;
l != (unsigned long*) (heap_top + PAGESIZE);
++l) {
assert(*l == 0);
}
// check we can write to new page
*heap_top = p;
// check we can write to console
console[CPOS(24, 79)] = p;
// update `heap_top`
heap_top += PAGESIZE;
}
sys_yield();
}
// After running out of memory, do nothing forever
while (true) {
sys_yield();
}
}