-
Notifications
You must be signed in to change notification settings - Fork 0
/
page_table.h
100 lines (80 loc) · 2.29 KB
/
page_table.h
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
#ifndef PAGE_TABLE
#define PAGE_TABLE
#include <stdlib.h>
#include "address_processing.h"
#define PAGE_TABLE1_LEN 1024
#define PAGE_TABLE2_LEN 1024
#define USED '1'
#define UNUSED '0'
#define VALID '1'
#define INVALID '0'
//Definitions
struct page_table1_entry
{
char available;
struct page_table2_entry *second_level;
};
struct page_table2_entry
{
char available;
char validity;
int frame_number;
};
struct page_table1_entry *init_page_table();
void free_page_table(struct page_table1_entry *page_table);
void set_page_table_interval(struct page_table1_entry *page_table, char hex1[HEX_LENGTH], char hex2[HEX_LENGTH]);
// Implementation
struct page_table1_entry *init_page_table()
{
struct page_table1_entry *page_table = malloc(sizeof(struct page_table1_entry) * PAGE_TABLE1_LEN);
for (int i = 0; i < PAGE_TABLE1_LEN; i++)
{
page_table[i].available = UNUSED;
page_table[i].second_level = NULL;
}
return page_table;
}
void free_page_table(struct page_table1_entry *page_table)
{
for (int i = 0; i < PAGE_TABLE1_LEN; i++)
{
if (page_table[i].available == USED)
{
free(page_table[i].second_level);
}
}
free(page_table);
}
void set_page_table_interval(struct page_table1_entry *page_table, char hex1[HEX_LENGTH], char hex2[HEX_LENGTH])
{
int hex1_page1_num = get_page_part1(hex1);
int hex1_page2_num = get_page_part2(hex1);
int hex2_page1_num = get_page_part1(hex2);
int hex2_page2_num = get_page_part2(hex2);
for (int i = hex1_page1_num; i <= hex2_page1_num; i++)
{
if (page_table[i].available == UNUSED)
{
page_table[i].available = USED;
page_table[i].second_level = malloc(sizeof(struct page_table2_entry) * PAGE_TABLE2_LEN);
for (int j = 0; j < PAGE_TABLE2_LEN; j++)
{
page_table[i].second_level[j].available = UNUSED;
}
}
}
int p1 = hex1_page1_num;
int p2 = hex1_page2_num;
while (p1 != hex2_page1_num || p2 != hex2_page2_num)
{
page_table[p1].second_level[p2].available = USED;
page_table[p1].second_level[p2].validity = INVALID;
p2++;
if (p2 == PAGE_TABLE2_LEN)
{
p2 = 0;
p1++;
}
}
}
#endif