annotate cos/kernel/mm.c @ 26:dcce92b1efbc

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