-
Notifications
You must be signed in to change notification settings - Fork 0
/
memorymgr.c
173 lines (121 loc) · 3.07 KB
/
memorymgr.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#include <stdio.h>
#include <stdlib.h>
#include "memorymgr.h"
// global variables
int *firstblock;
int *lastblock;
int *firstheader;
int next8(int size);
// initialize a simulated heap
void initmemory(int size) {
// 39 as original request
// need 4 bytes for header of heap
// 4 bytes for sentinal block
// 4 bytes for alingment
// 39 + 4 + 4 + 4
int heaplentgh = next8(size + 4);
int *heap = malloc(heaplentgh);
firstblock = heap;
firstheader = heap + 1;
lastblock = firstblock + (heaplentgh/4) - 1;
*firstheader = heaplentgh - 8;
*lastblock = 1;
}
// allocate memory from the heap
void *myalloc(int length) {
int lastVal = 0;
int *header = firstBlock();
int *ptr;
int size = next8(length);
// Look for unallocated block
while (*header < size || *header % 2 !=0) {
// we've reached the sentinel block, so no valid block
if (*header == 1) {
return NULL;
}
header = nextBlock(header);
}
lastVal = *header;
// +1 to show that block is allocated
*header = size + 1;
// to return the block (not header), we want the header+1
ptr = header + 1;
// adjust rest of heap
header = nextBlock(header);
if (*header != 1) {
*header = lastVal - size;
}
return ptr;
}
// free a block
void myfree(void *ptr) {
int *header = ptr - 4;
if(isAllocated(header)) {
*header = *header - 1;
}
}
// coalesce all free blocks
void coalesce() {
int *header = firstBlock();
int *nextHeader = nextBlock(header);
while (*nextHeader != 1) {
if (*header % 2 == 0 && *nextHeader % 2 == 0) {
*header = *header + *nextHeader;
}
else {
header = nextHeader;
}
nextHeader = nextBlock(header);
}
}
void printallocation() {
int *header = firstBlock();
int num = 0;
while (*header != 1) {
if (isAllocated(header)) {
printf("Block %d: size %d \t allocated \n", num, *header - 1);
}
else {
printf("Block %d: size %d \t unallocated \n", num, *header);
}
header = nextBlock(header);
num++;
}
printf("\n\n");
}
// The following functions are needed for HW6.
// You should implement them now, as they will also be useful to help you implement HW5.
int next8(int size) {
size += 4;
int num = size % 8;
if (num != 0) {
size = size + (8 - num);
}
return size;
}
// check if block is allocated
int isAllocated(int *p) {
if(*p % 8 == 0) {
return 0;
}
return 1;
}
// go the the next block
int *nextBlock(int *p) {
int *block = p;
if (*block % 2 != 0) {
block = block + (*block - 1)/4;
}
else {
block = block + *block/4;
}
return block;
}
// retun the first header in the heap
int *firstBlock() {
return firstheader;
}
// return sentinel block
int *lastBlock() {
return lastblock;
}