-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmmanager_model.c
More file actions
264 lines (246 loc) · 9.18 KB
/
Copy pathmmanager_model.c
File metadata and controls
264 lines (246 loc) · 9.18 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
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "mmanager.h"
int runModel(FILE *outputfp, FILE *inputfp, long totalMemorySize,
int fitStrategy, int verbosity) {
char *memoryBlock = NULL;
int nSuccessfulActions = 0;
mmgr_action action;
fprintf(outputfp, "Running a %s-fit model in %ld (0x%lx) bytes of memory.\n",
(fitStrategy == STRAT_FIRST ? "first"
: fitStrategy == STRAT_BEST ? "best"
: fitStrategy == STRAT_WORST ? "worst"
: "unknown"),
totalMemorySize, totalMemorySize);
/**
* this is the only allocation you should need -- all requests
* from your model should come from this allocated block
*/
memoryBlock = (char *)malloc(totalMemorySize);
if (memoryBlock == NULL) {
perror("allocation failed!");
return -1;
}
/**
* +++ Set up anything else you will need for your memory management
*/
// internal strucutre to track the memory chunks
// this struct itself needs 24 bytes
typedef struct Chunk {
int id; // id of the allocation chunk
int size; // size of the chunk
int isFree; // 1-> free, 0 -> allocated
struct Chunk *next; // next chunk pointer
} Chunk;
// counting the number of allocations
int numAlocations = 1;
// track number of bytes allocated
int allocated = 0;
Chunk *head = (Chunk *)memoryBlock;
// printf("%p", head);
head->size = totalMemorySize - sizeof(Chunk);
head->isFree = 1;
head->next = NULL;
while (getAction(&action, inputfp, outputfp, verbosity) > 0) {
if (action.type == ACTION_ALLOCATE) {
/* +++ do an allocation */
Chunk *current = head;
// iterate through the linked list to find suitable chunk
while (current != NULL) {
// for 1st starategy
if (fitStrategy == STRAT_FIRST) {
// is current chunk free and large enough for the request
if (current->isFree && current->size >= action.size) {
// allocate memory in the current chunk
int requested = action.size;
// calculating relative location
int offset = (int)((char *)current - memoryBlock);
// divide the chunk if there's enough space for another chunk
if ((current->size - requested) >= sizeof(Chunk)) {
// calcuting the address and size of the chunk that will be marked
// as free
Chunk *next =
(Chunk *)((char *)current + sizeof(Chunk) + requested);
next->size = current->size - requested - sizeof(Chunk);
next->isFree = 1;
next->next = current->next;
current->next = next;
// increment the allocation tracker
numAlocations++;
}
// update current chunk
current->size = requested;
current->isFree = 0;
current->id = action.id;
// track allocated memory
allocated += action.size;
// paint the memory with paint character
memset((char *)current + sizeof(Chunk), action.paint, requested);
// output message
fprintf(outputfp, "alloc %d bytes : SUCCESS - return location %d\n",
requested, offset);
break;
}
}
// best strategy
if (fitStrategy == STRAT_BEST) {
// pointer to find the best fit chunk for request
Chunk *ptr = head;
Chunk *bestFit = NULL;
int min = totalMemorySize;
// traverse the linked list
while (ptr != NULL) {
// current chunk is free, large enough size and has less useless
// space
if (ptr->size - action.size < min && ptr->isFree &&
ptr->size >= action.size) {
// store current chunk into pointer
min = ptr->size;
bestFit = ptr;
}
ptr = ptr->next;
}
// allocate if a suitable chunk was found
if (bestFit != NULL) {
int requested = action.size;
// calculate relative location
int offset = (int)((char *)bestFit - memoryBlock);
// need memory for the tracking strut as well so check if left over
// space is enough
if ((bestFit->size - requested) >= sizeof(Chunk)) {
// calcuting the address and size of the chunk that will be marked
// as free
Chunk *next =
(Chunk *)((char *)bestFit + sizeof(Chunk) + requested);
next->size = bestFit->size - requested - sizeof(Chunk);
next->isFree = 1;
next->next = bestFit->next;
bestFit->next = next;
// increment the allocation tracker
numAlocations++;
}
// update the best fit chunk
bestFit->size = requested;
bestFit->isFree = 0;
bestFit->id = action.id;
// tracking the memory allocated
allocated += action.size;
// paint the memory with paint character
memset((char *)bestFit + sizeof(Chunk), action.paint, requested);
// output message
fprintf(outputfp, "alloc %d bytes : SUCCESS - return location %d\n",
requested, offset);
} else {
fprintf(outputfp, "alloc %d bytes : FAIL\n", action.size);
}
break;
}
if (fitStrategy == STRAT_WORST) {
Chunk *ptr = head;
Chunk *worstFit = NULL;
int max = 0;
while (ptr != NULL) {
if (ptr->size - action.size > max && ptr->isFree &&
ptr->size >= action.size) {
max = ptr->size;
worstFit = ptr;
}
ptr = ptr->next;
}
if (worstFit != NULL) {
int requested = action.size;
// calculate relative location
int offset = (int)((char *)worstFit - memoryBlock);
// need memory for the tracking strut as well so check if left over
// space is enough
if ((worstFit->size - requested) >= sizeof(Chunk)) {
// calcuting the address and size of the chunk that will be marked
// as free
Chunk *next =
(Chunk *)((char *)worstFit + sizeof(Chunk) + requested);
next->size = worstFit->size - requested - sizeof(Chunk);
next->isFree = 1;
next->next = worstFit->next;
worstFit->next = next;
// increment the allocation tracker
numAlocations++;
}
// update the best fit chunk
worstFit->size = requested;
worstFit->isFree = 0;
worstFit->id = action.id;
// tracking the memory allocated
allocated += action.size;
// paint the memory with paint character
memset((char *)worstFit + sizeof(Chunk), action.paint, requested);
// output message
fprintf(outputfp, "alloc %d bytes : SUCCESS - return location %d\n",
requested, offset);
} else {
fprintf(outputfp, "alloc %d bytes : FAIL\n", action.size);
}
break;
}
current = current->next;
}
// no allocation done, error message printout
if (current == NULL) {
fprintf(outputfp, "alloc %d bytes : FAIL\n", action.size);
}
} else {
/* +++ do a release */
Chunk *current = head;
// traverse the linked list, looking for id
while (current != NULL) {
if (action.id == current->id) {
// mark it as free
current->isFree = 1;
allocated -= current->size;
// output message with relative addressing
fprintf(outputfp, "free location %ld\n",
(char *)current - (char *)memoryBlock);
break;
}
current = current->next;
}
// merging adjacent free blocks
Chunk *ptr = head;
while (ptr != NULL && ptr->next != NULL) {
if (ptr->isFree && ptr->next->isFree) {
// merge the current and next chunks
ptr->size += ptr->next->size + sizeof(Chunk);
// skip the next chunk
ptr->next = ptr->next->next;
numAlocations--;
} else {
ptr = ptr->next;
}
}
}
/** increment our count of work we did */
nSuccessfulActions++;
}
/** +++ Clean up your memory management */
fprintf(outputfp, "SUMMARY:\n");
fprintf(outputfp, "%d bytes allocated\n", allocated);
fprintf(outputfp, "%ld bytes free\n", totalMemorySize - allocated);
fprintf(outputfp, "%d allocation chunks:\n", numAlocations);
Chunk *ptr = head;
int i = 0;
while (ptr != NULL) {
fprintf(outputfp, "chunk %d location %ld:%d bytes - ", i,
(char *)ptr - (char *)memoryBlock, ptr->size);
if (ptr->isFree) {
fprintf(outputfp, "free");
} else {
fprintf(outputfp, "allocated");
}
fprintf(outputfp, "\n");
ptr = ptr->next;
i++;
}
free(memoryBlock);
return nSuccessfulActions;
}