void place(char *bp, size_t size) //allocate to block and update free list,split block if necessary //place called after find_fit in MALLOC //size is block including header and footer { ASSERT(GETALLOC(HDRP(bp)) == 0) ; ASSERT(size <= GETSIZE(HDRP(bp))) ; ASSERT(size % ALIGNMENT == 0) ; //bp is empty block size_t bSize = GETSIZE(HDRP(bp)) ; if(bSize - size < MINBLOCKSIZE) { FL_remove(bp) ; PUT(HDRP(bp), PACK(bSize, 1)) ; PUT(FTRP(bp), PACK(bSize, 1)) ; //change header and footer alloc bit } else if(bSize - size >= MINBLOCKSIZE) { ASSERT((bSize-size) % ALIGNMENT == 0) ; // check if new block size will mess up alignment FL_remove(bp) ;//remove bp from free list PUT(HDRP(bp) ,PACK(size, 1)) ; PUT(FTRP(bp), PACK(size,1)) ; ASSERT(isValidBlock(bp)) ; bp = NEXT_BLKP(bp) ; PUT(HDRP(bp) ,PACK(bSize - size, 0)); PUT(FTRP(bp), PACK(bSize-size, 0)) ; ASSERT(isValidBlock(bp)) ; coalesce(bp) ; //insert split block back to free list } dmm_checkheap(278) ; //CHECKHEAP!!!!!!!!!!!!!!!!!!!!! return ; }
int validFreeList(char *bp) { #ifdef DEBUG char *hdr ; int prevOffset ; // int offset = (int)(((long)(HDRP(firstFBP))) - (long)(heapStart)) ; // offset between first block and heap start char * curr = firstFBP ; if(curr == NULL) return 1 ; //int nextOffset = DEREF_NEXT(curr) ; while(curr != heapStart) //curr == heapStart if next field is NULL/0 { hdr = HDRP(curr) ; if(GETALLOC(hdr)) return 0 ; // block is not free if(!isValidBlock(curr)) return 0 ; prevOffset = DEREF_PREV(curr) ; if((prevOffset != 0 && GETALLOC(HDRP(GET_PREV(curr)))) || (DEREF_NEXT(curr) != 0 && GETALLOC(HDRP(GET_NEXT(curr))))) { // if prev or next is not free return 0 ; } ASSERT((GET_NEXT(curr)==heapStart) || GET_PREV(GET_NEXT(curr)) == curr) ; curr = GET_NEXT(curr) ; // dbg_printf("Curr->next = %d \n", DEREF_NEXT(curr)) ; // nextOffset = DEREF_NEXT(curr) ; //no effect if curr == heapStart } return 1 ; #else return 1 ; #endif }
static void FL_insert(char *bp) //insert at start of free list { ASSERT(bp != NULL) ; ASSERT(GETALLOC(HDRP(bp)) == 0) ; ASSERT(isValidBlock(bp)) ; if(firstFBP == NULL) { // free list is empty firstFBP = bp ; SET_PREV(bp, 0) ; SET_NEXT(bp, 0) ; return ; } else { unsigned int offset = firstFBP - heapStart ; SET_NEXT(bp, offset) ; //bp->next = firstFBP offset = (unsigned int)(bp - heapStart) ; // ASSERT(DEREF_PREV(firstFBP) == 0) ; SET_PREV(firstFBP, offset) ; // firstFBP->prev = bp //ASSERT(DEREF_PREV(firstFBP) == offset) ; SET_PREV(bp, 0) ; //bp->prev = NULL firstFBP = bp ; //set bp as start of list return ; } return ; }
bool isReadable() const { #ifdef R_UM_OPT_ENFORCE_LOWLEV_VALIDITY assert(isValidRefcnt()); assert(isValidBlock()); #endif return true; }
~RawBlock() { #ifdef R_UM_OPT_ENFORCE_LOWLEV_VALIDITY assert(isValidBlock()); assert(! isValidRefcnt()); #endif delete[] block_; }
RawBlock(Word blockSize): refcnt_(1), size_(blockSize), block_(new Word[size_]) { #ifdef R_UM_OPT_ENFORCE_LOWLEV_VALIDITY assert(isValidSize()); assert(isValidBlock()); #endif std::memset(block_, 0, size_ * sizeof(Word)); }
RawBlock(const RawBlock &sourceBlock): refcnt_(1), size_(sourceBlock.size_), block_(new Word[size_]) { #ifdef R_UM_OPT_ENFORCE_LOWLEV_VALIDITY assert(isValidSize()); assert(isValidBlock()); assert(sourceBlock.isValidBlock()); assert(size_ == sourceBlock.size_); #endif std::memcpy(block_, sourceBlock.block_, size_ * sizeof(Word)); }
bool isValidSudoku(vector<vector<char>>& board) { bool f = 1; for (int i = 0; i < board.size(); i++) { // row f = f && isValidBlock(board[i]); } for (int i = 0; i < SIZE; i++) { // column vector<char> tmp; for (int j = 0; j < SIZE; j++) { tmp.push_back(board[j][i]); } f = f && isValidBlock(tmp); } for (int i = 0; i < SIZE; i++) { // little 3x3 square vector<char> tmp; int r = i / 3, c = i % 3; r *= 3, c *= 3; for (int j = 0; j < SIZE; j++) { int dr = j / 3, dc = j % 3; tmp.push_back(board[r + dr][c + dc]); } f = f && isValidBlock(tmp); } return f; }
/* * realloc - you may want to look at mm-naive.c */ void *realloc(void *oldptr, size_t size) { size_t oldsize; char *newptr; /* If size == 0 then this is just free, and we return NULL. */ if(size == 0) { free(oldptr); return 0; } /* If oldptr is NULL, then this is just malloc. */ if(oldptr == NULL) { return malloc(size); } ASSERT(GETALLOC(HDRP(oldptr)) == 1) ; ASSERT(isValidBlock((char*)oldptr)); newptr = (char*)malloc(size); /* If realloc() fails the original block is left untouched */ if(!newptr) { return 0; } /* Copy the old data. */ oldsize = GETSIZE(HDRP(oldptr)) - ALIGNMENT; //remove size of hdr and ftr size = MIN(oldsize, size) ; //ASSERT(GETSIZE(HDRP(newptr)) >= GETSIZE(HDRP(oldptr))) ; char* Oldptr = (char*)oldptr; ASSERT(size < GETSIZE(HDRP(newptr)) ) ; for(size_t i = 0; i < size; i++) { *(newptr +i) = *(Oldptr + i) ; // copy data } /* Free the old block. */ free(oldptr); return newptr; }
void mm_checkheap(int lineno) { #ifdef DEBUG if(prologue == NULL || epilogue == NULL) { dbg_printf("Pro / Ei is NULL at line %d\n", lineno); exit(1) ; } if((GETSIZE(epilogue)) != 0 || (GETALLOC(epilogue)) != 1) { dbg_printf("EPILOGUE not set correctly at line %d\n",lineno) ; dbg_printf("Epilogue Size = %d , Alloc = %d \n",GETSIZE(epilogue),GETALLOC(epilogue)); exit(1) ; } //if epilogue size is not 0 or alloc is not 1 char *proFtr = prologue + (GETSIZE(prologue))/2 ; //prologue should be only hdr and ftr by text book convention if(GETSIZE(prologue) != GETSIZE(proFtr)) { dbg_printf("Prlogue not set correctly at line %d\n",lineno); exit(1); } if(GETALLOC(prologue) != GETALLOC(proFtr)) { dbg_printf("Prologue Alloc not set correctly at line %d\n",lineno); exit(1) ; } char *bp = heapStart + (WSIZE * 4) ; // set to first bp on heap while(1) { if(HDRP(bp) == epilogue) return ; //reached end of heap if(! isValidBlock(bp)) { dbg_printf("Not Valid Block at line %d\n",lineno); exit(1) ; } bp = NEXT_BLKP(bp) ; } return ; #else return ; #endif }