void *_safeFree(char *dataAddress, int lineNumber, char *fileName){ MemoryDescription *freePtr; ErrorObject *err; int iHeader=-1,iFooter=-1; Try{ freePtr=moveBetweenList(dataAddress, fileName, lineNumber); // no error, will do below designation freePtr->freeLine =lineNumber; freePtr->freeFile =fileName; patternRepeat(freePtr->dataSize, "#", freePtr->memoryAddress); iHeader = patternCheck(freePtr->headerAddress, HEADER_SIZE, CODE_PATTERN); iFooter = patternCheck(freePtr->footerAddress, FOOTER_SIZE, CODE_PATTERN); if(iHeader != PASS_TICKET){ printf("File:%p:%d: note: Memory's header integrity violated when freed@column:%d",fileName,lineNumber,iHeader); } if(iFooter != PASS_TICKET){ printf("File:%p:%d: note: Memory's footer integrity violated when freed@column:%d",fileName,lineNumber,iFooter); } }Catch(err){ printf("Error: %s\n", err->errorMsg); freeError(err); } }
int roamingChecker(MemoryDescription *nomadPtr){ int resultOfHeader,resultOfSpace,resultOfFooter; int counter=0; printf("==========START OF LIST===========\n"); while(nomadPtr->next != NULL){ resultOfHeader = patternCheck( nomadPtr->headerAddress, HEADER_SIZE, CODE_PATTERN); resultOfFooter = patternCheck( nomadPtr->footerAddress, FOOTER_SIZE, CODE_PATTERN); if(nomadPtr->freeLine == 0){ printf("File:%p:%d: note: Memory malloc-ed pending to be free.\n",nomadPtr->mallocFile,nomadPtr->mallocLine); } else{ resultOfSpace = patternCheck( nomadPtr->memoryAddress, nomadPtr->dataSize, "#"); if(resultOfSpace != PASS_TICKET){ printf("File:%p:%d: note: Memory's datapad integrity violated @column:%d\n",nomadPtr->mallocFile,nomadPtr->mallocLine,resultOfSpace); } } if(resultOfHeader != PASS_TICKET){ printf("File:%p:%d: note: Memory's header integrity violated @column:%d\n",nomadPtr->mallocFile,nomadPtr->mallocLine,resultOfHeader); } if(resultOfFooter != PASS_TICKET){ printf("File:%p:%d: note: Memory's footer integrity violated @column:%d\n",nomadPtr->mallocFile,nomadPtr->mallocLine,resultOfFooter); } counter++; nomadPtr=nomadPtr->next; } printf("==========END OF REPORT==========\n\n"); return counter; }
void ourFree(void* p1, char* file, size_t line){ /*check to make sure it's an existing pointer*/ if(!p1){ printf("ERROR: You cannot free a null pointer! \n"); printf("This ERROR was encountered in file %s, at line %d \n", file, (int)line); return; } /*first, check if it is even a valid pointer to something that was malloc-ed*/ char* ptrCheck = (char*)p1 - SIZE_OF_MEMENTRY; if(ptrCheck > memory + BLOCK_SIZE - SIZE_OF_MEMENTRY || ptrCheck < memory) { printf("ERROR: The pointer you are trying to free is not in the heap. \n"); printf("This ERROR was encountered in file %s, at line %d \n", file, (int)line); return; } /*otherwise, find what you're trying to free*/ memEntry* p1ptr = (memEntry*) ptrCheck; if(!patternCheck(p1ptr->recPattern)){ printf("ERROR: Pointer was never allocated! \n"); printf("This ERROR was encountered in file %s, at line %d \n", file, (int)line); return; } /*if you've already freed the pointer*/ if(p1ptr -> isFree) { printf("ERROR: Redundant pointer freeing! \n"); printf("This ERROR was encountered in file %s, at line %d \n", file, (int)line); printf("The object was malloc-ed in file %s, at line %d \n", p1ptr->file, (int)p1ptr->lineNumber); return; } /*now that you know it's a valid pointer, check what preceds and follows it*/ memEntry* pre; memEntry* next; /* set pre equal to the previous of the first pointer and make sure it's already freed so you can merge */ if((pre = p1ptr->prev) != 0 && pre->isFree){ pre->size = pre->size + p1ptr->size + SIZE_OF_MEMENTRY; pre->succ = p1ptr->succ; if(p1ptr->succ != NULL) { p1ptr->succ->prev = pre; } p1ptr->recPattern = 1; if(p1ptr == shared_start) shared_start = pre; } else{ p1ptr->isFree = 1; pre = p1ptr; } if((next = p1ptr->succ) != 0 && next->isFree){ pre->size = pre->size + next->size + SIZE_OF_MEMENTRY; pre->succ = next->succ; if(next->succ != NULL){ next->succ->prev = pre; } next->recPattern = 1; } return; }