-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpages.c
102 lines (83 loc) · 1.82 KB
/
pages.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// based on cs3650 starter code/hints
#define _GNU_SOURCE
#include <string.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <assert.h>
#include <fcntl.h>
#include <errno.h>
#include <stdio.h>
#include <stdint.h>
#include "pages.h"
#include "util.h"
#include "bitmap.h"
const int PAGE_COUNT = 256;
const int NUFS_SIZE = 4096 * 256; // 1MB
static int pages_fd = -1;
static void* pages_base = 0; // where the pages start
// page 0: bitmaps (first half is pages_bitmap, second half is inode_bitmap)
void
pages_init(const char* path)
{
pages_fd = open(path, O_CREAT | O_RDWR, 0644);
assert(pages_fd != -1);
int rv = ftruncate(pages_fd, NUFS_SIZE);
assert(rv == 0);
pages_base = mmap(0, NUFS_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, pages_fd, 0);
assert(pages_base != MAP_FAILED);
void* pbm = get_pages_bitmap();
bitmap_put(pbm, 0, 1);
}
void
pages_free()
{
int rv = munmap(pages_base, NUFS_SIZE);
assert(rv == 0);
}
void*
pages_get_page(int pnum)
{
return pages_base + 4096 * pnum;
}
void*
get_pages_bitmap()
{
return pages_get_page(0);
}
void*
get_inode_bitmap()
{
uint8_t* page = pages_get_page(0);
return (void*)(page + 32);
}
void* get_pages_rootlist()
{
uint8_t* page = pages_get_page(1);
return (void*)(page);
}
int
alloc_page()
{
void* pbm = get_pages_bitmap();
for (int ii = 1; ii < PAGE_COUNT; ++ii) {
if (!bitmap_get(pbm, ii)) {
bitmap_put(pbm, ii, 1);
printf("+ alloc_page() -> %d\n", ii);
return ii;
}
}
bitmap_print(get_pages_bitmap(), 256);
puts("");
sleep(1);
assert(0);
return -1;
}
void
free_page(int pnum)
{
printf("+ free_page(%d)\n\n\n\n\n", pnum);
void* pbm = get_pages_bitmap();
bitmap_put(pbm, pnum, 0);
}