-
Notifications
You must be signed in to change notification settings - Fork 0
/
disk.c
92 lines (79 loc) · 2.5 KB
/
disk.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
#include "disk.h"
//initializes the disk data-type
//return true on success, false otherwise
bool DISK_Init() {
ASSERT_PRINT("Entering:DISK_Init\n");
Disk = calloc(NumOfPagesInDisk, sizeof (Page));
int i = 0;
int j = 0;
for (i = 0; i < NumOfPagesInDisk; i++)
Disk[i] = calloc(PageSize, sizeof (char));
/*
for (i = 0; i < NumOfPagesInDisk; i++)
for (j = 0; j < PageSize; j++) {
Disk[i][j] = 48 + (((i + 1) * j) % 42);
}
*/
//ASSERT(DISK_PrintContent());
if (Disk == NULL)
return FALSE;
return TRUE;
}
void DISK_PrintContent() {
int i = 0;
int j = 0;
ASSERT_PRINT("Disk content:\n");
for (i = 0; i < NumOfPagesInDisk; i++)
{
for (j = 0; j < PageSize; j++)
fprintf(outFile,"%c|", Disk[i][j]);
fprintf(outFile,"\n");
if((i+1)%NumOfProcessPages==0)
fprintf(outFile,"\n");
}
ASSERT_PRINT("End disk content\n");
}
bool DISK_AllocateSpace(unsigned int start, unsigned int end) {
ASSERT_PRINT("Entering:DISK_AllocateSpace(%d,%d)\n", start, end);
int i = start;
for (i; i < end; i++) {
if (FreeList[i].isFree == FALSE) {
fprintf(outFile,"Error allocating space for process\n");
return FALSE;
}
//Disk[i] = calloc(PageSize, sizeof (char));
if (Disk[i] == NULL)
return FALSE;
}
FREELIST_SetTaken(start);
ASSERT_PRINT("Exiting:DISK_AllocateSpace(%d,%d)\n", start, end);
return TRUE;
}
bool DISK_DeAllocateSpace(unsigned int start, unsigned int end) {
ASSERT_PRINT("Entering:DISK_DeAllocateSpace(%d,%d)\n", start, end);
int i = start;
for (i; i < end; i++)
free(Disk[i]);
ASSERT_PRINT("Exiting:DISK_DeAllocateSpace(%d,%d)\n", start, end);
}
void DISK_ReadPage(int pageNum, OUT Page* pageToReturn) {
ASSERT_PRINT("Entering:DISK_ReadPage(pageNum:%d)\n", pageNum);
int i=0;
for(i=0; i<PageSize; i++)
(*pageToReturn)[i] = Disk[pageNum][i];
ASSERT_PRINT("Exiting:DISK_ReadPage(pageNum:%d)\n", pageNum);
}
//Writes the data to the pageNumber returns true if all went well, false otherwise.
bool DISK_WritePage(Page data, int pageNum) {
ASSERT_PRINT("Entering:DISK_WritePage(pageNum:%d)\n", pageNum);
ASSERT(strlen(data) == PageSize);
int i=0;
for(i; i<PageSize; i++)
Disk[pageNum][i] = data[i];
ASSERT_PRINT("Exiting:DISK_WritePage(pageNum:%d)\n", pageNum);
}
void DISK_DeInit()
{
DISK_DeAllocateSpace(0,NumOfPagesInDisk);
free(Disk);
}