-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmavalloc.c
executable file
·364 lines (312 loc) · 8.62 KB
/
mavalloc.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
359
360
361
362
363
364
/*
Patrick Arzoumanian
Justine Tran
*/
#include "mavalloc.h"
//P: Making these three variables "global" allows ease of access
Node *head;
Node *temp = NULL;
Node *previous = NULL;
void *arena;
enum ALGORITHM algorithm_g;
void *first_fit(size_t);
void *next_fit(size_t);
void *best_fit(size_t);
void *worst_fit(size_t);
//J: [size] - large memory pool allocation on application startup
//J: [ALGORITHM] - First Fit, Best Fit, Worst Fit, Next Fit
int mavalloc_init( size_t size, enum ALGORITHM algorithm )
{
//P: ALLOCATING ARENA
size_t requested_size = ALIGN4(size);
arena = malloc(requested_size);
head = (Node *) malloc(sizeof(Node));
//P: If the allocation fails or the size is less than 0 the function returns -1
if((head == NULL) || (requested_size < 0))
{
return -1;
}
//P: Setting head characteristics
head->arena = arena;
head->type = HOLE;
head->size = requested_size;
head->next = NULL;
head->prev = NULL;
//P: Setting global algorithm
algorithm_g = algorithm;
previous = head;
return 0;
}
void mavalloc_destroy( )
{
//P: Freeing a DLL implementation
Node *temp = head;
Node *next_node = NULL;
while(temp != NULL)
{
next_node = temp->next;
free(temp);
temp = next_node;
}
head = NULL;
return;
}
//J: Allocates size bytes from preallocated memory arena
//J: [Hole/Process] [Start Number] [Length] [Prev] [Next]
void * mavalloc_alloc( size_t size )
{
size_t requested_size = ALIGN4(size);
if(head == NULL)
{
// printf("\nNo arena allocated. mavalloc_alloc() cannot be called\n");
return NULL;
}
if(algorithm_g == FIRST_FIT)
{
return first_fit(requested_size);
}
else if(algorithm_g == NEXT_FIT)
{
return next_fit(requested_size);
}
else if(algorithm_g == BEST_FIT)
{
return best_fit(requested_size);
}
else if(algorithm_g == WORST_FIT)
{
return worst_fit(requested_size);
}
else
{
// printf("The code should not print this... Something went wrong with the algorithm enum.\n");
}
//P: COULDN'T FIND SPOT, NO SPACE
// printf("\nCouldn't find a spot to fit the request in... Request cannot be met.\n");
return NULL;
}
//J: Frees the memory block pointed by pointer back to preallocated memory arena
//J: Two consecutive blocks free then combine (coalesce) them
void mavalloc_free( void * ptr )
{
Node * node = head;
//P: Loop through DLL until pointers match
while( node )
{
if( node -> arena == ptr )
{
if( node -> type == HOLE )
{
printf("Warning: Double free detected\n");
}
//P: Change node to hole
node -> type = HOLE;
break;
}
node = node -> next;
}
//P: Reset node back to head
node = head;
//P: "Coalesce" node's surrounding desired hole
while( node )
{
if( node -> next && node -> type == HOLE && node -> next -> type == HOLE )
{
Node * previous = node -> next;
node -> size = node -> size + node -> next -> size;
node -> next = node -> next -> next;
free( previous );
continue;
}
node = node -> next;
}
return;
}
//J: Function returns the number of nodes in the memory area
int mavalloc_size( )
{
int number_of_nodes = 0;
Node *temp = head;
while(temp)
{
number_of_nodes++;
temp = temp->next;
}
return number_of_nodes;
}
//P: We will be using this function when needed during any of the 4 algorithms
void *insert_node_after(Node *prev_node, size_t size, enum TYPE type)
{
//P: Create new node and set it's data
Node *new_node = (Node *)malloc(sizeof(Node));
//J: Placing leftover arena into new_node
new_node->arena = prev_node->arena+size;
new_node->size = size;
new_node->type = type;
//P: Setting up pointers for new node
new_node->next = prev_node->next;
prev_node->next = new_node;
new_node->prev = prev_node;
//P: Setting the node FOLLOWING the new_node's previous pointer to the new node to link them up
if(new_node->next != NULL)
{
new_node->next->prev = new_node;
}
return new_node;
}
//P: Made the function for debugging purposes so that we can see what the arena looks like
void print_dll()
{
Node *temp = head;
printf("\n\n DLL\n----------\n");
while(temp != NULL)
{
//P: If node is of type "HOLE"
if(temp->type == 0)
{
printf("[H | %ld] --> ", temp->size);
}
//P: If node is of type "PART"
else
{
printf("[P | %ld] --> ", temp->size);
}
temp = temp->next;
}
printf("NULL\n\n");
}
void *first_fit(size_t requested_size)
{
temp = head;
while(temp)
{
//P: If request is smaller than the size of a hole...
if(temp->size > requested_size && temp->type == HOLE)
{
//P: Make a HOLE after temp with a size of the remaining memory
Node *memory_left_over = insert_node_after(temp, temp->size - requested_size, HOLE);
//P: Make the original HOLE a PART with the size requested
temp->size = requested_size;
temp->type = PART;
previous = memory_left_over;
return (void *) temp->arena;
}
//P: If requested size is the exact same size as the hole, make the HOLE a PART
if(temp->size == requested_size)
{
temp->type = PART;
previous = temp;
return (void *) temp->arena;
}
temp = temp->next;
}
}
void *next_fit(size_t requested_size)
{
//J: Start from head if there is no next pointer left off
if(previous == NULL)
previous = head;
//J: Assuming there was a left of part
temp = previous;
while(temp != NULL)
{
//J: Similar to FIRST_FIT Excepts starts at PART left off
if(temp->size > requested_size && temp->type == HOLE)
{
Node *memory_left_over = insert_node_after(temp,temp->size - requested_size,HOLE);
temp->size = requested_size;
temp->type = PART;
previous = temp;
return (void *) temp->arena;
}
//J: If pointer allocator is the same size as the HOLE
if(temp->size == requested_size && temp->type == HOLE)
{
temp->type = PART;
previous = temp;
return (void *) temp->arena;
}
temp = temp->next;
//J: Gone through whole array
if(temp == previous)
break;
//J: We have reached the head, starting from the beginning
if(temp == NULL)
temp = head;
}
}
void *best_fit(size_t requested_size)
{
Node *smallest_hole;
size_t smallest_size = INT_MAX;
temp = head;
while(temp != NULL)
{
//P: If temp has a bigger size than the largest_hole we've seen so far, update largest_hole
if(temp->type == HOLE && temp->size <= smallest_size)
{
smallest_size = temp->size;
smallest_hole = temp;
}
temp = temp->next;
}
//P: Set temp's address to the largest_hole's address
temp = smallest_hole;
if(requested_size > smallest_size)
{
// printf("\nCouldn't find a spot to fit the request in... Request cannot be met.\n");
return NULL;
}
//P: If the requested size is the same exact size as the largest size, there's no need to make a new node, make the HOLE a PART
if(requested_size == smallest_size)
{
temp->type = PART;
previous = temp;
return (void *) temp->arena;
}
//P: Make a HOLE after temp with a size of the remaining memory
Node *memory_left_over = insert_node_after(temp, temp->size - requested_size, HOLE);
//P: Make the original HOLE a PART with the size requested
temp->size = requested_size;
temp->type = PART;
previous = temp;
return (void *) temp->arena;
}
void *worst_fit(size_t requested_size)
{
//P: Make another temp variable to hold the locatation of the biggest hole and the biggest size
Node *largest_hole;
size_t largest_size = 0;
temp = head;
while(temp != NULL)
{
//P: If temp has a bigger size than the largest_hole we've seen so far, update largest_hole
if(temp->type == HOLE && temp->size >= largest_size)
{
largest_size = temp->size;
largest_hole = temp;
}
temp = temp->next;
}
//P: Set temp's address to the largest_hole's address
temp = largest_hole;
if(requested_size > largest_size)
{
// printf("\nCouldn't find a spot to fit the request in... Request cannot be met.\n");
return NULL;
}
//P: If the requested size is the same exact size as the largest size, there's no need to make a new node, make the HOLE a PART
if(requested_size == largest_size)
{
temp->type = PART;
previous = temp;
return (void *) temp->arena;
}
//P: Make a HOLE after temp with a size of the remaining memory
Node *memory_left_over = insert_node_after(temp, temp->size - requested_size, HOLE);
//P: Make the original HOLE a PART with the size requested
temp->size = requested_size;
temp->type = PART;
previous = temp;
return (void *) temp->arena;
}