-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmy_malloc.c
454 lines (364 loc) · 11.1 KB
/
my_malloc.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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
#include "my_malloc.h"
#include <stdint.h>
/* You *MUST* use this macro when calling my_sbrk to allocate the
* appropriate size. Failure to do so may result in an incorrect
* grading!
*/
#define SBRK_SIZE 2048
#define MAXBUCKET 7
/* If you want to use debugging printouts, it is HIGHLY recommended
* to use this macro or something similar. If you produce output from
* your code then you will receive a 20 point deduction. You have been
* warned.
*/
#ifdef DEBUG
#define DEBUG_PRINT(x) printf x
#else
#define DEBUG_PRINT(x)
#endif
//#include <string.h>
/* make sure this always points to the beginning of your current
* heap space! if it does not, then the grader will not be able
* to run correctly and you will receive 0 credit. remember that
* only the _first_ call to my_malloc() returns the beginning of
* the heap. sequential calls will return a pointer to the newly
* added space!
* Technically this should be declared static because we do not
* want any program outside of this file to be able to access it
* however, DO NOT CHANGE the way this variable is declared or
* it will break the autograder.
*/
void* heap;
void* heapEnd;
/* our freelist structure - this is where the current freelist of
* blocks will be maintained. failure to maintain the list inside
* of this structure will result in no credit, as the grader will
* expect it to be maintained here.
* Technically this should be declared static for the same reasons
* as above, but DO NOT CHANGE the way this structure is declared
* or it will break the autograder.
*/
metadata_t* freelist[8];
/**** SIZES FOR THE FREE LIST ****
* freelist[0] -> 16
* freelist[1] -> 32
* freelist[2] -> 64
* freelist[3] -> 128
* freelist[4] -> 256
* freelist[5] -> 512
* freelist[6] -> 1024
* freelist[7] -> 2048
*/
// the first run gets us the 8kb got to check this so we dont repeat
int initialRun = 0;
void* my_malloc(size_t size)
{
/* FIX ME */
DEBUG_PRINT(("in malloc"));
int metaDataSize = sizeof(metadata_t);
//can't allocate negative size
if(size <= 0) { return NULL; }
if(size > (2048-metaDataSize)) { return NULL; }
int index = getIndex(metaDataSize+size);
if( index > MAXBUCKET) { return NULL; }
if(!freelist[index] && (splitBlock(index) == -1)) { return NULL; }
metadata_t *ptr = freelist[index];
if(ptr->next)
{
ptr->next->prev = NULL;
freelist[index] = ptr->next;
}
else
{
freelist[index] = NULL;
}
ptr->in_use = 1;
return ptr+1;
}
void* my_realloc(void* ptr, size_t new_size)
{
/* FIX ME */
DEBUG_PRINT(("in realloc\n"));
//printf("in realloc\n");
if(!ptr) return my_malloc(new_size);
else if(!new_size)
{
my_free(ptr);
return NULL;
}
else
{
metadata_t* ptr2 = (metadata_t*)((char*)ptr-sizeof(metadata_t));
metadata_t* ptr3 = ptr2;
if(ptr2->size-sizeof(metadata_t) >= new_size)
{
int indexOfCurr, indexOfRealloc;
indexOfRealloc = getIndex(sizeof(metadata_t)+new_size);
if( indexOfRealloc > MAXBUCKET) { return NULL; }
indexOfCurr = getIndex(sizeof(metadata_t)+ptr2->size);
//case where its in the same bucket already
//(ie not a large enough size increase)
if(indexOfCurr == indexOfRealloc) { return ptr; }
}
//if its not malloc more memory
ptr2 = my_malloc(new_size);
if(!ptr2) { return NULL; }
//write code that transfers block of memory
//from one address to another
if(ptr3->size-sizeof(metadata_t) >= new_size) {
my_memcpy(ptr2,ptr,new_size);
}
else {
my_memcpy(ptr2,ptr,ptr3->size-sizeof(metadata_t));
}
my_free(ptr);
return ptr2;
}
return NULL;
}
void my_free(void* ptr)
{
/* FIX ME */
DEBUG_PRINT(("in free"));
char *p;
metadata_t *mPtr;
int index;
//step 1 offset the block of memory back to the head of the metadata
//purposely not casting data as a metadata pointer
p = ((char*)ptr)-sizeof(metadata_t);
if(p == NULL || ((metadata_t*)p)->in_use != 1) { return; }
mPtr = ((metadata_t*)p);
index = getIndex(mPtr->size);
if( index > MAXBUCKET) { return; }
mPtr->in_use = 0;
mPtr->next = freelist[index];
mPtr->prev = NULL;
if(mPtr->next) mPtr->next->prev = mPtr;
freelist[index] = mPtr;
//TODO make Buddies function
merge();
}
void* my_memcpy(void* dest, const void* src, size_t num_bytes)
{
/* FIX ME */
//cast to a char
DEBUG_PRINT(("in memcpy"));
char *destPtr = dest;
char const *srcPtr = src;
while (num_bytes-- > 0) {
*destPtr++ = *srcPtr++;
}
return dest;
}
/*HELPER FUNCTIONS*/
//not recursive but justin said that was cool
void merge()
{
//need to get n
//int n = logBase2(SIZEOFDATA_INCLUDING_METADATA )
//compare bit n+1 make sure they match up A&B = A
//where A and B represent the 5th bit of numbers a and b
//to determine address check n bit if 1 second in pair if 0 then first
// subtract heap from current position of heapEnd
/************(int*)heapEnd -(int*)heap;*************/
//do bit manipulation
//add back heap
DEBUG_PRINT(("in merge"));
char* curr;
for(curr= (char*)heap;curr!=(char*)heapEnd && curr;)
{
if(curr+((metadata_t*)curr)->size > (char*)heapEnd) break;
if(((metadata_t*)curr)->size == SBRK_SIZE)
{
curr += ((metadata_t*)curr)->size;
continue;
}
if(
((metadata_t*)(curr+((metadata_t*)curr)->size)) &&
((metadata_t*)curr)->size ==
((metadata_t*)(curr+((metadata_t*)curr)->size))->size
&&
((metadata_t*)curr)->in_use == 0
&&
((metadata_t*)(curr+((metadata_t*)curr)->size))->in_use == 0)
{
/* FIND BUDDY CODE*/
int n = logBase2(((metadata_t*)curr)->size);
int step1, step2, index;
metadata_t* toMerge1 = (metadata_t*)curr;
metadata_t* toMerge2 = (metadata_t*)(curr+((metadata_t*)curr)->size);
step1 = (intptr_t)curr-(intptr_t)heap;
step2 =
((intptr_t)(curr+((metadata_t*)curr)->size))-(intptr_t)heap;
if((step1>>(n+1) & 0x1) != (step2>>(n+1) & 0x1)){
return;
}
//size without the metadata
index = getIndex(((metadata_t*)curr)->size);
if( index > MAXBUCKET) return;
removeNode(toMerge1,index);
removeNode(toMerge2,index);
/* Add Nodes to list */
toMerge1->size *= 2;
toMerge1->in_use = 0;
toMerge1->prev = NULL;
toMerge1->next = freelist[index+1];
if(freelist[index+1]){
freelist[index+1]->prev = toMerge1;
}
freelist[index+1] = toMerge1;
curr = heap;
}else{
curr += ((metadata_t*)curr)->size;
}
}
}
void removeNode(metadata_t* node, int index){
metadata_t* curr;
if(node == NULL){
return;
}
for(curr=freelist[index];curr!= NULL;curr=curr->next){
if(curr == node){
if(curr->prev){
curr->prev->next = curr->next;
if(curr->next){
curr->next->prev = curr->prev;
}
}else{
freelist[index] = curr->next;
if(curr->next){
curr->next->prev = NULL;
}
}
return;
}
}
}
int splitBlock(int index)
{
DEBUG_PRINT(("in split block"));
split:
// their might be a more efficent way to do this
// need a l
//printf("%d",index);
for(int i=index+1; i<8; i++)
{
if(freelist[i] != NULL)
{
while(freelist[index] == NULL)
{
// need to break memory block in half until it fills freelist
//at the index i want (see the pun?)
splitBlockHelper(i);
i--;
}
return 1;
}
}
// does not handle yet but if no blocks available to split ask for more meomory
char* temp_heap = my_sbrk(SBRK_SIZE);
if(temp_heap == (void*)-1) return -1;
/*if(initialRun)
{
(char*)heap += SBRK_SIZE;
}*/
if(initialRun == 0)
{
heapEnd = (void*)temp_heap;
heap = temp_heap;
initialRun=1;
}
// can't increment point because its a void pointer
//heapEnd += SBRK_SIZE;
heapEnd = (void*)(((char*)heapEnd) + SBRK_SIZE);
((metadata_t*)temp_heap)->next = freelist[MAXBUCKET];
((metadata_t*)temp_heap)->prev = NULL;
((metadata_t*)temp_heap)->in_use = 0;
((metadata_t*)temp_heap)->size = SBRK_SIZE;
freelist[MAXBUCKET] = ((metadata_t*)temp_heap);
if( index == MAXBUCKET)
return 1;
goto split;
}
void splitBlockHelper(int index)
{
char *headBlock, *newCutBlock;
//might be able to cast this to a metadata_t but this might be safer
// Especially since we don't want the whole memory block to be treated as the metadata
//check...
headBlock = (char*)freelist[index];
newCutBlock = (char*)freelist[index]+((freelist[index]->size)/2);
freelist[index] = freelist[index]->next;
if(freelist[index] != NULL){
freelist[index]->prev = freelist[index];
}
index--;
//doubly Linked list setup
((metadata_t*)headBlock)->next = (metadata_t*)newCutBlock;
((metadata_t*)headBlock)->prev = NULL;
// don't set to null might be a node already in freeList at that index
((metadata_t*)newCutBlock)->next = freelist[index];
((metadata_t*)newCutBlock)->prev = (metadata_t*)headBlock;
freelist[index] = (metadata_t*)headBlock;
freelist[index]->in_use = 0;
freelist[index]->size = (short)(16<<index);
freelist[index]->next->in_use = 0;
freelist[index]->next->size = (short)(16<<index);
if(((metadata_t*)newCutBlock)->next)
{
((metadata_t*)newCutBlock)->next->prev = (metadata_t*)newCutBlock;
}
}
int getIndex(int size)
{
int index = 0;
for(int i = 4; i <12; i++)
{
if (size > powFunc(i,2))
index++;
}
return index;
}
int powFunc(int power, int base)
{
int retVal = 1;
if (power == 0)
return 1;
else if(base == 2)
return base <<(power-1);
else if(power == 1)
return base;
else
{
for(int i = 0; i < power; i++)
{
retVal *= base;
}
}
return retVal;
}
int logBase2( int a)
{
int total = ((int)sizeof(int))*8;
if (a < 0)
{
DEBUG_PRINT(("Log of negative number error"));
return -2;
}
if (!a)
return -1;
int pos = 0;
//printf("total: %d\n",total);
for(int i = total>>1 /*total*/; i > 0;i = i>>1)
{
//printf("i: %d\t",i);
if (a >= 1<<i)
{
a >>= i;
pos +=i;
}
}
if (a >= 1<< 1) pos += 1;
return pos;
}