You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// 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
+
intmm_check(void)
248
+
{
249
+
size_t*next_ptr;
250
+
size_t*search_ptr;
251
+
intflag=1;
252
+
inti;
253
+
intsize;
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
+
return1;
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
+
return2;
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
+
return3;
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
+
elseif (search_ptr==NULL){//indicates block not found in list
301
+
printf("ERROR: free block in heap not found in coresponding free list.");
302
+
return4;
303
+
}
304
+
search_ptr=*search_ptr;
305
+
}
306
+
next_ptr= ((char*)next_ptr+size);
307
+
}
308
+
elseif (*(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
+
return5;
315
+
}
316
+
}
317
+
return0;
318
+
}
319
+
319
320
320
321
/*
321
322
* 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) {
377
378
*((char*)location+5) = (size_t*)next_ptr;
378
379
}
379
380
} else { //size is not appropriate, need to try next link
0 commit comments