-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMalloc.c
More file actions
326 lines (269 loc) · 9.56 KB
/
Malloc.c
File metadata and controls
326 lines (269 loc) · 9.56 KB
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <inttypes.h>
//GLOBAL VARIABLES
int blockNum = 0;
int allocate(int bytes, unsigned char* myHeap) {
int successful = 0;
if(bytes >= 127) {
printf("Invalid byte entered!\n");
return successful;
}
else if (blockNum == 256) {
printf("Heap is full!\n");
return successful;
}
unsigned char index = 0;
for (;index < 128; index = index + (((myHeap[index+1]) & -2) >> 1) + 1) {
//"and" the size with 1 and shift right 1 to get the original size back
// if the orignal size is greater than the needed byte than do splitting
if (((((myHeap[index+1]) & -2) >> 1) - (((unsigned char)bytes) + 2)) == 1 && (myHeap[index+1] & 1) == 0) {
++blockNum;
successful = 1;
//change the header (first byte) to the incremented block number
myHeap[index] = blockNum;
unsigned char blockSize = (unsigned char)bytes + 3;
blockSize = blockSize << 1;
blockSize = blockSize | 1;
myHeap[index+1] = blockSize;
break;
}
/* if ((((((myHeap[index+1]) & -2) >> 1)+1) - (((unsigned char)bytes) + 2)) == 1 && (myHeap[index+1] & 1) == 0) {
printf("HERE\n");
++blockNum;
successful = 1;
//change the header (first byte) to the incremented block number
myHeap[index] = blockNum;
unsigned char blockSize = (unsigned char)bytes + 3;
blockSize = blockSize << 1;
blockSize = blockSize | 1;
myHeap[index+1] = blockSize;
break;
}*/
else if (((((myHeap[index+1]) & -2) >> 1) - (((unsigned char)bytes) + 2)) == 2 && (myHeap[index+1] & 1) == 0) {
++blockNum;
successful = 1;
//change the header (first byte) to the incremented block number
myHeap[index] = blockNum;
unsigned char blockSize = (unsigned char)bytes + 4;
blockSize = blockSize << 1;
blockSize = blockSize | 1;
myHeap[index+1] = blockSize;
break;
}
/*else if ((((((myHeap[index+1]) & -2) >> 1)+1) - (((unsigned char)bytes) + 2)) == 2 && (myHeap[index+1] & 1) == 0) {
++blockNum;
successful = 1;
//change the header (first byte) to the incremented block number
myHeap[index] = blockNum;
unsigned char blockSize = (unsigned char)bytes + 4;
blockSize = blockSize << 1;
blockSize = blockSize | 1;
myHeap[index+1] = blockSize;
break;
}*/
else if ((((myHeap[index+1]) & -2) >> 1) >( (unsigned char)bytes + 2) && (myHeap[index+1] & 1) == 0) {
++blockNum;
successful = 1;
unsigned char oldBlockSize = ((myHeap[index+1]) & -2) >> 1;
//change the header (first byte) to the incremented block number
myHeap[index] = blockNum;
unsigned char blockSize = (unsigned char)bytes + 2; //check if have to minus 1
blockSize = blockSize << 1;
blockSize = blockSize | 1;
//stores the new size into index + 1 and mark the last bit as "allocated" = 1
myHeap[index+1] = blockSize;
//have to set the size of the "ending" byte (in the 2nd byte)
unsigned char getSize = ((blockSize & -2) >> 1);
unsigned char calcIndexNewSize = ((oldBlockSize - getSize) << 1);
myHeap[index + ((myHeap[index + 1] & -2) >> 1) + 2] = calcIndexNewSize;
break;
}
/* else if (((((myHeap[index+1]) & -2) >> 1)+1) >( (unsigned char)bytes + 2) && (myHeap[index+1] & 1) == 0) {
++blockNum;
successful = 1;
unsigned char oldBlockSize = ((myHeap[index+1]) & -2) >> 1;
//change the header (first byte) to the incremented block number
myHeap[index] = blockNum;
unsigned char blockSize = (unsigned char)bytes + 2; //check if have to minus 1
blockSize = blockSize << 1;
blockSize = blockSize | 1;
//stores the new size into index + 1 and mark the last bit as "allocated" = 1
myHeap[index+1] = blockSize;
//have to set the size of the "ending" byte (in the 2nd byte)
unsigned char getSize = ((blockSize & -2) >> 1);
unsigned char calcIndexNewSize = ((oldBlockSize - getSize + 1) << 1);
myHeap[index + ((myHeap[index + 1] & -2) >> 1) + 2] = calcIndexNewSize;
break;
}*/
else if ((((myHeap[index+1]) & -2) >> 1) == ((unsigned char)bytes +2) && (myHeap[index+1] & 1) == 0) {
++blockNum;
successful = 1;
//change the header (first byte) to the incremented block number
myHeap[index] = blockNum;
unsigned char blockSize = (unsigned char)bytes + 2; //check if have to minus 1
blockSize = blockSize << 1;
blockSize = blockSize | 1;
//stores the new size into index + 1 and mark the last bit as "allocated" = 1
myHeap[index+1] = blockSize;
break;
}
/* else if (((((myHeap[index+1]) & -2) >> 1)+1) == ((unsigned char)bytes +2) && (myHeap[index+1] & 1) == 0) {
++blockNum;
successful = 1;
//change the header (first byte) to the incremented block number
myHeap[index] = blockNum;
unsigned char blockSize = (unsigned char)bytes + 2; //check if have to minus 1
blockSize = blockSize << 1;
blockSize = blockSize | 1;
//stores the new size into index + 1 and mark the last bit as "allocated" = 1
myHeap[index+1] = blockSize;
break;
}*/
}
if (successful == 0) {
printf("Heap cannot allocate that much space!\n");
return;
}
printf("%d\n", blockNum);
}
void freeBlock(int prevBlockNum, unsigned char* myHeap) {
if (prevBlockNum != blockNum) {
printf("This is not the previous block!\n");
}
unsigned char index = 0;
for (;index < 128; index = index + (((myHeap[index+1]) & -2) >> 1) + 1) {
if(prevBlockNum == myHeap[index]) {
myHeap[index + 1] = myHeap[index + 1] & -2;
break;
}
}
}
void blocklist(unsigned char* myHeap) {
printf("Size Allocated Start End\n");
unsigned char index = 0;
int check = 0;
int count = 0;
for (;index < 128; index = index + (((myHeap[index+1]) & -2) >> 1) + 1) {
count += myHeap[index+1] >> 1;
if ( count == 127 || (myHeap[index+1] >> 1 ) == 127 ){
check = 1;
}
printf("%-5u", (myHeap[index+1] >> 1) + check );
check = 0;
if ( (myHeap[index+1] & 1) == 0 ) {
printf("no ");
}
else {
printf("yes ");
}
printf("%-15p", (void* )&myHeap[index]);
printf("%p\n", (void* )&myHeap[(index + (((myHeap[index+1]) & -2) >> 1) + 1)-1]);
}
}
void writeHeap(int bnum, char* copyChar, int numCopies, unsigned char* myHeap) {
if (bnum > blockNum || bnum <= 0) {
printf("Block number does not exist!\n");
return;
}
unsigned char index = 0;
for (;index < 128; index = index + (((myHeap[index+1]) & -2) >> 1) + 1) {
if (myHeap[index] == bnum && ((myHeap[index + 1] >> 1) >= (numCopies + 2)) && (myHeap[index+1] & 1) == 1) {//delete the minus 1
unsigned char i = 0;
for (; i < numCopies; ++i) {
myHeap[index + i + 2] = *copyChar;
}
return;
}
}
printf("Too many copies or that block is free!\n");
}
void printHeap(int bnum, int numBytes, unsigned char* myHeap) {
if (bnum > blockNum || bnum <= 0) {
printf("Block number does not exist!\n");
return;
}
if (numBytes > 126) {
printf("Too many bytes!\n");
return;
}
unsigned char index = 0;
for (;index < 128; index = index + ((((myHeap[index+1]) & -2) >> 1) + 1)) {
if (myHeap[index] == bnum && (myHeap[index + 1] & 1) == 1) {
unsigned char i = 0;
for (; i <numBytes; ++i) {
printf("%c", myHeap[index + i + 2]);
}
printf("\n");
return;
}
}
printf("That block is free!\n");
}
void printHeader(int bnum, unsigned char* myHeap) {
if (bnum > blockNum || bnum <= 0) {
printf("Block number does not exist!\n");
return;
}
unsigned char index = 0;
for (;index < 128; index = index + (((myHeap[index+1]) & -2) >> 1) + 1) {
if (myHeap[index] == bnum && (myHeap[index+1] & 1) == 1) {
printf("%02x%02x\n",myHeap[index], ((((myHeap[index + 1] >> 1) - 1) << 1) + 1));
return;
}
}
printf("That block is free!\n");
}
int main() {
char user_command[BUFSIZ];
char *command, *command1, *command2, *command3, *command4;
unsigned char *myHeap;
myHeap = (unsigned char*) malloc (128 * sizeof(unsigned char*));
unsigned char tempBlockNum = 0;
unsigned char tempBlockSize = 127;
tempBlockSize = tempBlockSize << 1;
myHeap[0] = tempBlockNum;
myHeap[1] = tempBlockSize;
while (1) {
printf("> ");
fgets(user_command, 100, stdin);
command = strtok(strtok(user_command, "\n"), " ");
command1 = strtok(NULL, " ");
command2 = strtok(NULL, " ");
command3 = strtok(NULL, " ");
command4 = strtok(NULL, " ");
int temp;
if (strlen(user_command) <= 1) { //user just press enter
printf("Invalid command, please try again!\n");
}
else if (strcmp(command, "allocate") == 0 && command1 != NULL && sscanf(command1, "%d", &temp) == 1 && command2 == NULL) {
allocate(atoi(command1), myHeap);
}
else if (strcmp(command, "free") == 0 && command1 != NULL && sscanf(command1, "%d", &temp) == 1 && command2 == NULL) {
freeBlock(atoi(command1), myHeap);
}
else if (strcmp(command, "blocklist") == 0 && command1 == NULL) {
blocklist(myHeap);
}
else if (strcmp(command, "writeheap") == 0 && command1 != NULL && sscanf(command1, "%d", &temp) == 1
&& command2 != NULL && sscanf(command2, "%c ", &temp) == 1 &&
command3 != NULL && sscanf(command3, "%d", &temp) == 1 && command4 == NULL) { //fix the character reading problem
writeHeap(atoi(command1), command2, atoi(command3), myHeap);
}
else if (strcmp(command, "printheap") == 0 && command1 != NULL && sscanf(command1, "%d", &temp) == 1 && command2 != NULL && sscanf(command2, "%d", &temp) == 1 && command3 == NULL ) {
printHeap(atoi(command1), atoi(command2), myHeap);
}
else if (strcmp(command, "printheader") == 0 && command1 != NULL && sscanf(command1, "%d", &temp) == 1 && command2 == NULL) {
printHeader(atoi(command1), myHeap);
}
else if (strcmp(command, "quit") == 0) {
break;
}
else {
printf("Invalid command, please try again!\n");
}
}
return 0;
}