-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpages.c
82 lines (67 loc) · 1.39 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
#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 "pages.h"
#include "slist.h"
#include "util.h"
const int NUFS_SIZE = 1024 * 1024; // 1MB
const int PAGE_COUNT = 256;
static int pages_fd = -1;
static void* pages_base = 0;
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
pages_free()
{
int rv = munmap(pages_base, NUFS_SIZE);
assert(rv == 0);
}
void*
pages_get_page(int pnum)
{
return pages_base + 4096 * pnum;
}
inode*
pages_get_node(int node_id)
{
inode* idx = (inode*) pages_get_page(0);
return &(idx[node_id]);
}
int
pages_find_empty()
{
int pnum = -1;
for (int ii = 2; ii < PAGE_COUNT; ++ii) {
if (0) { // if page is empty
pnum = ii;
break;
}
}
return pnum;
}
void
print_node(inode* node)
{
if (node) {
printf("node{refs: %d, mode: %04o, size: %d, xtra: %d}\n",
node->refs, node->mode, node->size, node->xtra);
}
else {
printf("node{null}\n");
}
}