-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmm.c
115 lines (86 loc) · 2.17 KB
/
mm.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*
* =====================================================================================
*
* Filename: mm.c
*
* Description:
*
* Version: 1.0
* Created: Tuesday 28 October 2014 08:56:34 IST
* Revision: none
* Compiler: gcc
*
* Author: YOUR NAME (),
* Organization:
*
* =====================================================================================
*/
#include<system.h>
void *memcpy(void *dest, const void *src, size_t count)
{
const char *sp = (const char *)src;
char *dp = (char *)dest;
for(; count != 0; count--) *dp++ = *sp++;
return dest;
}
void *memset(void *dest, char val, size_t count)
{
char *temp = (char *)dest;
for( ; count != 0; count--) *temp++ = val;
return dest;
}
unsigned short *memsetw(unsigned short *dest, unsigned short val, size_t count)
{
unsigned short *temp = (unsigned short *)dest;
for( ; count != 0; count--) *temp++ = val;
return dest;
}
int number_of_blocks = (MEM_HIGH-MEM_LOW)/PAGE_SIZE;
int used_blocks = 0;
int max_free_blocks = 0;
char bit_map[1535];
int first_free_block(){
int i;
for(i=0;i<number_of_blocks;i++){
if(bit_map[i]=='0') return i;
}
return -1;
}
void mm_init(){
int i;
for(i=0;i<number_of_blocks;i++) bit_map[i]='0';
putint(number_of_blocks);
}
int* mm_alloc_block(){
int frame = first_free_block();
if(frame==-1) return -1;
bit_map[frame] = '1';
int* physical_address = (int*) (frame*PAGE_SIZE+MEM_LOW);
used_blocks++;
return physical_address;
}
void mm_free_block(int* p){
int frame = (int)p/PAGE_SIZE;
bit_map[frame] = '0';
used_blocks--;
}
void print_mm_things(){
puts("Just Memory things \n");
puts("Number of blocks : ");
putint(number_of_blocks);
putch('\n');
int i;
for(i=0;i<number_of_blocks;i++){
putch(bit_map[i]);
}
putch('\n');
int free_block = first_free_block();
int free_address = mm_alloc_block();
putint(free_block);
putch('\n');
putint(free_address);
putch('\n');
for(i=0;i<number_of_blocks;i++){
putch(bit_map[i]);
}
}