Mercurial > lcfOS
comparison cos/kernel/mm.c @ 26:dcce92b1efbc
Added mm.c
author | windel |
---|---|
date | Tue, 27 Dec 2011 17:36:52 +0100 |
parents | |
children | 47b7df514243 |
comparison
equal
deleted
inserted
replaced
25:d3c4bf3720a3 | 26:dcce92b1efbc |
---|---|
1 | |
2 /* Memory manager */ | |
3 | |
4 #include "kernel.h" | |
5 | |
6 // BITMAP functions: | |
7 #define NFRAMES 1000 | |
8 // Bit mask to check what frames are in use and which are free: | |
9 uint64_t frames[NFRAMES]; // NFRAMES*64*4kB frames. | |
10 | |
11 void set_frame(uint64_t frame) | |
12 { | |
13 uint64_t idx = frame / 64; | |
14 uint64_t off = frame % 64; | |
15 frames[idx] |= (0x1 << off); | |
16 } | |
17 | |
18 void clear_frame(uint64_t frame) | |
19 { | |
20 uint64_t idx = frame / 64; | |
21 uint64_t off = frame % 64; | |
22 frames[idx] &= ~(0x1 << off); | |
23 } | |
24 | |
25 uint64_t test_frame(uint64_t frame) | |
26 { | |
27 uint64_t idx = frame / 64; | |
28 uint64_t off = frame % 64; | |
29 return (frames[idx] & (0x1 << off)); | |
30 } | |
31 | |
32 uint64_t first_frame() | |
33 { | |
34 uint64_t i, j; | |
35 for (i = 0; i < NFRAMES / 64; i++) | |
36 { | |
37 if (frames[i] != 0xFFFFFFFFFFFFFFFF) | |
38 { | |
39 for (j = 0; j < 64; j++) | |
40 { | |
41 uint64_t dut = 0x1 << j; | |
42 if ((frames[i] & dut) != dut) | |
43 { | |
44 return i*64+j; | |
45 } | |
46 } | |
47 } | |
48 } | |
49 return 0xFFFFFFFFFFFFFFFF; | |
50 } | |
51 | |
52 // Memory manager functions: | |
53 void alloc_frame() | |
54 { | |
55 uint64_t idx = first_frame(); | |
56 if (idx == (uint64_t) -1) | |
57 { | |
58 panic("No more memory!"); | |
59 } | |
60 set_frame(idx); | |
61 } | |
62 | |
63 |