-
Notifications
You must be signed in to change notification settings - Fork 0
/
memsim.c
66 lines (53 loc) · 1.8 KB
/
memsim.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
// Starting code version v1.0
#include <assert.h>
#include <string.h>
#include "memsim.h"
/* Private Internals: */
// Currently 'boolean' array (bitmap would be better for scale and perf)
short freePages[NUM_PAGES];
// The simulated physical memory array (in bytes), aka physical R.A.M.
char physmem[PHYSICAL_SIZE];
/*
* Performs sanity checks based on the simulations defined constants.
* If these checks fail, the simulation is not valid and should not proceed.
* Asserts are used to halt the program immediately if these checks fail.
*/
void MemsimConfigSanityChecks() { // you should not modify this function
assert(PHYSICAL_SIZE % PAGE_SIZE == 0); // physical size in bytes must be a multiple of page size bytes
assert(VIRTUAL_SIZE == PHYSICAL_SIZE); // standard implementation gives whole PA space to each process (virtually)
assert(MAX_VA == MAX_PA);
assert(NUM_PAGES == NUM_FRAMES);
}
/*
* Public Interface:
*/
/*
* Zeroes out the simulated physical memory array.
*/
void Memsim_Init() { // zero free pages list
MemsimConfigSanityChecks();
memset(physmem, 0, sizeof(physmem)); // zero out physical memory
memset(freePages, 0, sizeof(freePages)); // zero implies free / FALSE / not used
}
/* Gets current shared reference to start of simulated physical memory. */
char* Memsim_GetPhysMem() {
return physmem;
}
/*
* Searches through the free page list to find the first free page in memory.
* It claims the page, marking it, and returns the physical address of the beginning
* of the page. If there are no free pages, returns -1;
*/
int Memsim_FirstFreePFN(){
for(int i = 0; i < NUM_PAGES; i++){
if (freePages[i] == 0){
freePages[i] = 1;
return i * PAGE_SIZE;
}
}
return -1;
}
int PFNFree(int PFN){
freePages[PFN] = 0;
return 0;
}