-
Notifications
You must be signed in to change notification settings - Fork 0
/
ram.c
55 lines (44 loc) · 987 Bytes
/
ram.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "constants.h"
char *ram[RAM_SIZE];
int k = 0;
int findFrameIdxInRAM(int frameNo){
return (frameNo) * PAGE_LENGTH;
}
int isFrameAvailable(int frameNo){
int _f = findFrameIdxInRAM(frameNo);
return (_f < RAM_SIZE && ram[_f] == NULL);
}
void setRAMCell(int cell, char *line){
ram[cell] = strdup(line);
}
void addToRAM(FILE *p, int *start, int *end){
char line[1000];
*start = k;
while(fgets(line, 999, p)){
ram[k++] = strdup(line);
if(k == RAM_SIZE) {
*start = -1;
*end = -1;
return;
}
}
*end = k-1;
}
void clearRAM(){
for(int i = 0; i<k; i++){
ram[i] = NULL;
}
k = 0;
}
char* getLineFromRAM(int *isValid, int k){
if(k < RAM_SIZE && ram[k] != NULL) {
*isValid = 1;
return ram[k];
} else {
*isValid = -1;
return "";
}
}