-
Notifications
You must be signed in to change notification settings - Fork 0
/
memory_allocator.c
358 lines (327 loc) · 8.75 KB
/
memory_allocator.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
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
/* ICS 53
* Project: A Memory Allocator
* Instructor: Ian Harris
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
// global variable
int arg_count;
unsigned char * heap;
unsigned char * start;
unsigned char * end;
int track = 0;
unsigned char *find_free_blk(int realsize);
void addblock(unsigned char* p, int len);
void keep_track(unsigned char * newblock);
void init_mem()
{
heap = (unsigned char *)malloc(127); // The heap is 127 bytes long
start = heap; // start ptr
end = heap + 127; // end ptr
heap[0] = 127*2;
}
void alloc_mem(int intsize)
{
unsigned int wholesize = intsize + 2; // header + payload + footer
// if(wholesize > 127) // check if entire block exceeds 127
// {
// return;
// }
unsigned char * newblock = find_free_blk(wholesize);
if(newblock == NULL) // all blocks are allocated or no suitable block is found
{
return;
}
addblock(newblock, wholesize);
keep_track(newblock);
if (start==newblock)
{
track = 1;
}
printf("%d\n", track);
}
// First-fit allocation
unsigned char *find_free_blk(int realsize)
{
unsigned char * p = start;
while (p<end)
{
// printf("p[0]&1 = %d\n", p[0]&1);
// printf("p[0] = %d\n", p[0]);
// printf("realsize*2)|1 = %d\n", (realsize*2)|1);
if ( ((p[0]&1)==0) && (p[0] >= ((realsize*2))))
{
return p;
}
p += (p[0]&-2)/2; // go to next block
}
return NULL;
}
// controller of track variable (for alloc_mem print)
void keep_track(unsigned char * newblock)
{
track = 1;
unsigned char * p = start;
while (p<end && p< newblock)
{
track += (p[0]&-2) /2;
p += (p[0]&-2)/2; // go to next block
}
}
void addblock(unsigned char* p, int len)
{
int newsize = len; // 6
int oldsize = (p[0]&-2)/2; // 10
// printf("newsize: %d/ oldsize: %d\n", newsize, oldsize);
// splitting
if (newsize < oldsize)
{
// printf("split\n");
(p + ((p[0]/2)-1))[0] = (oldsize - newsize)*2; // change the rightest of heap
p[0] = (newsize*2) | 1; // set the header
(p + newsize -1)[0] = p[0]; // set the footer
p += (p[0]&-2)/2;
p[0] = (oldsize - newsize)*2;
}
else // no splitting needed
{
// printf("NO split\n");
p[0] = (newsize*2) | 1; // store size and mark allocation bit 1
(p + newsize -1)[0] = p[0];
}
}
// unsigned char * find_block(int intindex)
// {
// unsigned char * temp = start;
// while(temp < end)
// {
// if(temp[0] == intindex)
// {
// return temp;
// }
// temp += (temp[0]&-2)/2;
// }
// return NULL; // assume the input is correct, won't return NULL
// }
void free_mem(int intindex)
{
unsigned char * target = start + intindex - 1; // -1 to find the header of target block
target[0] = target[0] & -2; // clear allocation bit (24--> realsize = 12) //
unsigned char * next = target + ((target[0]&-2)/2); // next_block = target + 12
unsigned char * set_zero = start + intindex;
unsigned char zero_count = ((target[0] & -2)/2)-2; // number of space needed to be reser to 0 // 5
int i;
for (i=0; i<zero_count; i++)
{
set_zero[i] = 0;
}
// not the first block
if (target!=start)
{
if ((next[0] & 1)==1) // ~~~~~~~~~|| allocated
{
/* free || allocated || allocated (coalesing previous block) */
if (((target-1)[0]&1) == 0)
{
unsigned char * prev = target - (( (target-1)[0] & -2 )/2); // get previous block ptr
prev[0] = (target[0] + ( (target-1)[0] & -2) ); // combine prev blk size and the free size & allocation bit 0
( target + (target[0]/2)-1 )[0] = prev[0]; // update the free blk footer to be n+m1
target = prev; // let p point to the whole coalescing block
}
/* allocated || allocated || allocated (No coalesing) */
else
{
target[0] = target[0] & -2; // clear header allocation flag
(target + (((target[0] & -2)/2)-1))[0] = target[0]; // clear footer allocation flag
}
}
else // ~~~~~~~~~|| free
{
/* free || allocated || free (coalesing previous and next block) */
if (((target-1)[0]&1) == 0)
{
target[0] = target[0] + next[0];
(target + (target[0]/2)-1 )[0] = target[0] ; // set the footer of next block to be same as the entire current + next
unsigned char * prev = target - (( (target-1)[0] & -2 )/2 );
prev[0] = (target[0] + ( (target-1)[0] & -2) ); // update the free blk footer to be n+m1
( target + (target[0]/2)-1 )[0] = prev[0];
target = prev;
}
/* allocated || allocated || free (coalesing next block) */
else
{
target[0] = target[0] + next[0];
(target + (target[0]&-2)/2-1)[0] = target[0]; // update the footer to be n+m2
}
}
}
// if target == start (first block)
else
{
/* allocated (start) || allocated ~~~~~~ */
if ((next[0] & 1)==1 )
{
target[0] = target[0] & -2; // clear header allocation flag
(target + (((target[0] & -2)/2)-1))[0] = target[0]; // clear footer allocation flag
}
/* allocated (start) || free ~~~~~~ */
else
{
target[0] = target[0] + next[0];
(target + (target[0]&-2)/2-1)[0] = target[0];
}
}
}
void blocklist()
{
unsigned char * temp = start;
unsigned char st_of_payload = 0;
unsigned char payload_size;
char status[10];
while(temp < end)
{
// free
if ((temp[0] & 1) == 0)
{
payload_size = ((temp[0]&-2)/2)-2;
strcpy(status, "free");
}
// allocated
else
{
payload_size = ((temp[0]&-2)/2)-2;
strcpy(status, "allocated");
}
printf("%d, %d, %s.\n", st_of_payload+1, payload_size, status);
st_of_payload += ((temp[0]&-2)/2);
temp += ((temp[0]&-2)/2);
}
}
void write_mem(int index, unsigned char*str)
{
unsigned char * target = start + index;
int char_count = strlen(str);
int i;
for (i=0; i < char_count; i++)
{
target[i] = str[i];
}
}
void print_mem(int idx, int num_to_print)
{
unsigned char * target = start + idx;
int i;
for (i=0; i < num_to_print; i++)
{
printf("%02X ",target[i]);
}
printf("\n");
}
int eval_cmd(char* str)
{
if (strcmp(str, "malloc")==0)
{
return 1;
}
if (strcmp(str, "free")==0)
{
return 2;
}
if (strcmp(str, "blocklist")==0)
{
return 3;
}
if (strcmp(str, "writemem")==0)
{
return 4;
}
if (strcmp(str, "printmem")==0)
{
return 5;
}
if (strcmp(str, "quit")==0)
{
return 0;
}
else
{
return -1;
}
}
void parse_cmd(char *cmd, char **argv)
{
// the cmd will be parsed and store in argv list
cmd[strlen(cmd)-1] = ' ';
char *token = strtok(cmd, " ");
while(token != NULL)
{
argv[arg_count++] = token;
token = strtok(NULL," ");
}
return;
}
void run_mem_alloc()
{
char cmdline[80];
char *argv[80];
arg_count = 0;
int cmd_type = 0;
init_mem();
do
{
arg_count = 0;
printf("> ");
fgets(cmdline, 80, stdin);
if (feof(stdin))
{
return;
}
parse_cmd(cmdline, argv);
cmd_type = eval_cmd(argv[0]);
switch (cmd_type)
{
// MALLOC
case (1):
{
int intsize = atoi(argv[1]);
alloc_mem(intsize);
break;
}
// FREE
case (2):
{
int intindex = atoi(argv[1]);
free_mem(intindex);
break;
}
// BLOCKLIST
case (3):
{
blocklist();
break;
}
// WRITE MEM
case (4):
{
int index = atoi(argv[1]);
write_mem(index, argv[2]);
break;
}
// PRINT MEM
case (5):
{
int idx = atoi(argv[1]);
int num_to_print = atoi(argv[2]);
print_mem(idx, num_to_print);
break;
}
}
}while (cmd_type != 0);
}
int main()
{
run_mem_alloc();
return 0;
}