Skip to content

Commit 75aefd5

Browse files
committed
merged and bugfixes
2 parents 4527d6a + 64c1e00 commit 75aefd5

File tree

1 file changed

+77
-76
lines changed

1 file changed

+77
-76
lines changed

mm.c

Lines changed: 77 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ void *mm_malloc(size_t size)
130130
*((int*)(LL_ptr) - 1) = size; //mark size metadata with size
131131
*LL_ptr_last = *LL_ptr; //repaired linked list, even if null.
132132

133-
mm_insert((void*)((char*)LL_ptr + size + 8), (oldsize - newsize));
133+
mm_insert((void*)((char*)LL_ptr + size + 8), (oldsize - newsize));
134134

135135
returnPointer = ((char*)(LL_ptr) + 3);
136136
break;
@@ -182,7 +182,7 @@ void mm_free(void *argptr)
182182
{
183183
int size, csize;
184184
char* ptr = argptr;
185-
185+
186186
ptr = (char*)ptr - 3; //Puts ptr at start of pointer position (no 3byte buffer in free blocks)
187187
size = *((int*)ptr - 1);
188188

@@ -201,7 +201,7 @@ void mm_free(void *argptr)
201201
if(*(ptr + size + 8) == 0) //the block AFTER is a free block
202202
{
203203
csize = *((int*)((char*)ptr + size + 9)); //size of the next block
204-
if (csize > 0) {
204+
if (csize > 0) {
205205
size = size + csize + 16;
206206
}
207207
}
@@ -244,78 +244,79 @@ void *mm_realloc(void *ptr, size_t size)
244244
* if a block is allocated:
245245
* ???
246246
*/
247-
// int mm_check(void)
248-
// {
249-
// void* next_ptr;
250-
// void* search_ptr*;
251-
// int flag = 1;
252-
// int i;
253-
// int size;
254-
// void* top_heap = mem_heap_hi();
255-
//
256-
// for(int i = 0; i < NUM_CLASSES; i++){//traverse free lists for integrity/*{{{*/
257-
// next_ptr = CLASSES[i];
258-
// if (next_ptr == NULL)
259-
// break;
260-
// while(flag){
261-
// if(*((char*)next_ptr - 5) != 0){//check if block is listed as free
262-
// printf("ERROR: block in free list not marked as free (possibly some other n00b mistake making it look like this)");
263-
// flag = !flag;
264-
// return 1;
265-
// }
266-
// size = *((int*)next_ptr-1);
267-
// if ( (*((char*)next_ptr -6) == 0) || (*((char*)next_ptr + size + 11) == 0) ){//checks if coalesce should have happened but didn't
268-
// printf("ERROR: block immediately before or after free block was free but not coalesced. COALESCEFAIL (possibly some other n00b mistake making it look like this)");
269-
// flag = !flag;
270-
// return 2;
271-
// }
272-
// next_ptr = *(size_t*)next_ptr;
273-
// if (next_ptr == NULL)
274-
// flag = !flag;
275-
// }
276-
// }/*}}}*/
277-
//
278-
// next_ptr = mem_heap_lo();//start of heap
279-
//
280-
// while(1){//traverse entire heap for integrity
281-
// if (&next_ptr > *top_heap){
282-
// printf("ERROR: Top of heap exceeded by pointer");
283-
// return 3;
284-
// }
285-
//
286-
// if (*(char*)next_ptr == 0){//is a free block
287-
// size = *(int*)((char*)next_ptr + 1);
288-
//
289-
// while(i < NUM_CLASSES){//find appropriate size class
290-
// if (size <= CLASS_SIZE[i])
291-
// break;
292-
// i++;
293-
// }
294-
// //now need to search for next_ptr in CLASSES[i]
295-
// search_ptr = CLASSES[i];
296-
// while(1){
297-
// if(next_ptr == search_ptr){//has ben found
298-
// break;
299-
// }
300-
// else if (search_ptr == NULL){//indicates block not found in list
301-
// printf("ERROR: free block in heap not found in coresponding free list.");
302-
// return 4;
303-
// }
304-
// search_ptr = *search_ptr;
305-
// }
306-
// next_ptr = ((char*)next_ptr + size);
307-
// }
308-
// else if (*(char*)next_ptr == 1){//is an allocated block
309-
// size = *(char*)((char*)next_ptr + 1);
310-
// next_ptr = ((char*)next_ptr + size);
311-
// }
312-
// else{ //data is corrupted
313-
// printf("free/allocated marker is other than 1/0. DATA CORRUPTED");
314-
// return 5;
315-
// }
316-
// }
317-
// return 0;
318-
// }
247+
int mm_check(void)
248+
{
249+
size_t* next_ptr;
250+
size_t* search_ptr;
251+
int flag = 1;
252+
int i;
253+
int size;
254+
size_t* top_heap = mem_heap_hi();
255+
256+
for(i = 0; i < NUM_CLASSES; i++){//traverse free lists for integrity/*{{{*/
257+
next_ptr = CLASSES[i];
258+
if (next_ptr == NULL)
259+
break;
260+
while(flag){
261+
if(*((char*)next_ptr - 5) != 0){//check if block is listed as free
262+
printf("ERROR: block in free list not marked as free (possibly some other n00b mistake making it look like this)");
263+
flag = !flag;
264+
return 1;
265+
}
266+
size = *((int*)next_ptr-1);
267+
if ( (*((char*)next_ptr -6) == 0) || (*((char*)next_ptr + size + 11) == 0) ){//checks if coalesce should have happened but didn't
268+
printf("ERROR: block immediately before or after free block was free but not coalesced. COALESCEFAIL (possibly some other n00b mistake making it look like this)");
269+
flag = !flag;
270+
return 2;
271+
}
272+
next_ptr = *(size_t*)next_ptr;
273+
if (next_ptr == NULL)
274+
flag = !flag;
275+
}
276+
}/*}}}*/
277+
278+
next_ptr = mem_heap_lo();//start of heap
279+
280+
while(1){//traverse entire heap for integrity
281+
if (&next_ptr > *top_heap){
282+
printf("ERROR: Top of heap exceeded by pointer");
283+
return 3;
284+
}
285+
286+
if (*(char*)next_ptr == 0){//is a free block
287+
size = *(int*)((char*)next_ptr + 1);
288+
289+
while(i < NUM_CLASSES){//find appropriate size class
290+
if (size <= CLASS_SIZE[i])
291+
break;
292+
i++;
293+
}
294+
//now need to search for next_ptr in CLASSES[i]
295+
search_ptr = CLASSES[i];
296+
while(1){
297+
if(next_ptr == search_ptr){//has ben found
298+
break;
299+
}
300+
else if (search_ptr == NULL){//indicates block not found in list
301+
printf("ERROR: free block in heap not found in coresponding free list.");
302+
return 4;
303+
}
304+
search_ptr = *search_ptr;
305+
}
306+
next_ptr = ((char*)next_ptr + size);
307+
}
308+
else if (*(char*)next_ptr == 1){//is an allocated block
309+
size = *(char*)((char*)next_ptr + 1);
310+
next_ptr = ((char*)next_ptr + size);
311+
}
312+
else{ //data is corrupted
313+
printf("free/allocated marker is other than 1/0. DATA CORRUPTED");
314+
return 5;
315+
}
316+
}
317+
return 0;
318+
}
319+
319320

320321
/*
321322
* mm_insert - given a pointer to the beginning of a free section of data, and a total size,
@@ -377,7 +378,7 @@ int mm_insert(void* location, int size) {
377378
*((char*)location + 5) = (size_t*)next_ptr;
378379
}
379380
} else { //size is not appropriate, need to try next link
380-
last_ptr = next_ptr;
381+
last_ptr = next_ptr;
381382
next_ptr = *(size_t*)next_ptr;
382383
}
383384
}

0 commit comments

Comments
 (0)