void freef(char *p) { printf("freef called with p=%p\n", (void *) p); if(p!=0){ /* you write the code for the freef function here */ struct blockl *temp = (struct blockl *)p; temp->tag = FREETAG; char *temp2 = p; temp2 += temp->size; temp2 += (TAGSIZE*2); struct blockr *temp3 = (struct blockr *)temp2; temp3->tag = FREETAG; struct blockr *temp4 = (struct blockr *)(p - TAGSIZE); //temp4 -= 1; if (temp4->tag == FREETAG) { struct blockl *temp5 = (struct blockl *)(temp4 - temp4->size-(TAGSIZE)); coalesce(temp5, temp); } else{ enchain(temp); } temp2 += TAGSIZE; struct blockl *temp6 = (struct blockl *)temp2; if (temp6->tag == FREETAG){ unchain(temp6); coalesce(temp, temp6); } } }
int main() { int *p1, *p2; printf("initial allocation\n"); initmemory(56); printallocation(); printf("malloc 20\n"); p1 = (int *)myalloc(20); printresult(p1); printf("malloc 10\n"); p2 = (int *)myalloc(10); printresult(p2); printf("free (malloc 20)\n"); myfree(p1); p1 = NULL; printallocation(); printf("malloc 4\n"); p1 = (int *)myalloc(4); printresult(p1); printf("free (malloc 10)\n"); myfree(p2); p2 = NULL; printallocation(); printf("malloc 30: should fail\n"); // this will fail because we need a block of at least 40 p2 = (int *)myalloc(30); printresult(p2); printf("coalesce\n"); coalesce(); printallocation(); printf("malloc 30 \n"); // now this succeeds p2 = (int *) myalloc(30); printresult(p2); printf("free everything\n"); myfree(p1); p1 = NULL; myfree(p2); p2 = NULL; printallocation(); printf("malloc 56: should fail\n"); // this will fail p1 = (int *)myalloc(56); printresult(p1); printf("coalesce\n"); coalesce(); printallocation(); printf("malloc 56\n"); p1 = (int *)myalloc(56); printresult(p1); }
/** * @brief Coalesce the free list into larger blocks. * @param prev The previous block in the free list. * @param current The block to coalesce. * @param next The next block in the free list. */ static void coalesceBlocks(long *prev, long *current, long *next) { if (next != NULL) { coalesce(current, next); } if (prev != NULL) { coalesce(prev, current); } return; }
void mm_free(void *ptr) { if (ptr != NULL) { struct metadata *curr = get_block_ptr(ptr); curr->free = 1; if (curr->prev && curr->prev->free) { curr = coalesce(curr->prev); } if (curr->next && curr->next->free) { coalesce(curr); } } }
/** * @param data full multipart content * @param filesize sum of all parts * @param parts number of parts */ void RFC1867Base::testSimple(unique_ptr<IOBuf> data, size_t fileSize, size_t splitSize, size_t parts) { size_t fileLength = 0; IOBufQueue parsedData{IOBufQueue::cacheChainLength()}; EXPECT_CALL(callback_, onFieldStartImpl(_, _, _, _)) .Times(parts) .WillRepeatedly(Return(0)); EXPECT_CALL(callback_, onFieldData(_, _)) .WillRepeatedly(Invoke([&] (std::shared_ptr<IOBuf> data, uint64_t) { fileLength += data->computeChainDataLength(); parsedData.append(data->clone()); return 0; })); EXPECT_CALL(callback_, onFieldEnd(true, _)) .Times(parts) .WillRepeatedly(Return()); parse(data->clone(), splitSize); auto parsedDataBuf = parsedData.move(); if (fileLength > 0) { // isChained() called from coalesce below asserts if no data has // been added parsedDataBuf->coalesce(); } CHECK_EQ(fileLength, fileSize); }
/********************************************************** * extend_heap * Extend the heap by "words" words, maintaining alignment * requirements of course. Free the former epilogue block * and reallocate its new header **********************************************************/ void *extend_heap(size_t words) { char *bp; size_t size; /* Allocate an even number of words to maintain alignments */ size = (words % 2) ? (words+1) * WSIZE : words * WSIZE; if ( (bp = mem_sbrk(size)) == (void *)-1 ) return NULL; /* Initialize free block header/footer and the epilogue header */ PUT(HDRP(bp), PACK(size, 0)); // free block header PUT(FTRP(bp), PACK(size, 0)); // free block footer PUT(HDRP(NEXT_BLKP(bp)), PACK(0, 1)); // new epilogue header /* Place at top of free list */ add_to_free_list(bp); printf("add_to_free being called in extend()\n"); printf("extend - bp: %p\n", bp); printf("extend - addr of prev: %p\n", PREV_FR_BP(bp)); printf("extend - addr of next: %p\n", NEXT_FR_BP(bp)); /* Coalesce if the previous block was free */ return coalesce(bp); }
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 ; }
/** * allocate - Place block, i.e. write header and footer. */ static void allocate(void *bp, size_t adjusted_size) { size_t csize = GET_THISSIZE(bp); size_t is_prev_alloc = GET_PREVALLOC(bp); TRACE(">>>Entering allocate(bp=0x%X, adjusted_size=%u)\n", (unsigned int)bp, adjusted_size); /* We will always need to remove tshi block from the free list */ remove_from_list(bp, calc_list_index(csize)); /* See if there's room to split this block into two */ if ((csize - adjusted_size) >= (MIN_SIZE)) { PUTW(GET_BLOCKHDR(bp), PACK(adjusted_size, THISALLOC | is_prev_alloc)); PUTW(GET_BLOCKFTR(bp), PACK(adjusted_size, THISALLOC | is_prev_alloc)); /* Using the new header info, mark the newly created block as free */ bp = GET_NEXTBLOCK(bp); PUTW(GET_BLOCKHDR(bp), PACK(csize - adjusted_size, PREVALLOC)); PUTW(GET_BLOCKFTR(bp), PACK(csize - adjusted_size, PREVALLOC)); /* And add it to the appropriate free list */ coalesce(bp); } else {/* If there's not room to create split the block, just extend the amount to allocated */ PUTW(GET_BLOCKHDR(bp), PACK(csize, THISALLOC | is_prev_alloc)); PUTW(GET_BLOCKFTR(bp), PACK(csize, THISALLOC | is_prev_alloc)); /* Make sure the next block's header has the prevalloc field marked */ bp = GET_BLOCKHDR(GET_NEXTBLOCK(bp)); PUTW(bp, GETW(bp) | PREVALLOC); } TRACE("<<<---Leaving allocate()\n"); }
static void *extend_heap(size_t words) { char *bp; size_t size; /* Allocate an even number of words to maintain alignment */ size = (words % 2) ? (words+1) * WSIZE : words * WSIZE; size = MAX(size, BSIZE); /* call for more memory space */ if ((int)(bp = mem_sbrk(size)) == -1) return NULL; /* Initialize free block header/footer and the epilogue header */ PUT(HDRP(bp), PACK(size, 0)); /* free block header */ PUT(FTRP(bp), PACK(size, 0)); /* free block footer */ PUT(HDRP(NEXT_BLKP(bp)), PACK(0, 1)); /* new epilogue header */ /* coalesce bp with next and previous blocks */ return coalesce(bp); }
void free_single_page(region r, struct page *p) /* Assumes freepages_lock held */ { #ifndef NMEMDEBUG ASSERT_INUSE(p, r); set_page_region(PAGENB(p), FREEPAGE); #endif // fprintf(stderr, "## free_single_page: p=%p, p->pagecount=%d\n", p, p->pagecount); // assert(p->pagecount > 0); assert(p->pagecount == 1); /* Don't keep too many single pages (a small fraction of total allocated pages) */ if (single_page_count > PAGE_GROUP_SIZE * 2) { // quarl 2006-05-13: we now enforce the invariant that p->pagecount is // correct, so it should already be 1 at this point. // p->pagecount = 1; // XXX coalesce(p); } else { p->next = single_pages; single_pages = p; single_page_count++; } }
/* * free - Frees the block and coalesces * (implementation issue: coalescing) * */ void free(void *bp) { size_t size; size_t prev_alloc; size_t temp_value; dbg1("[IN ] : free()\n"); if (!bp) { dbg1("[OUT] : free() - NULL pointer\n"); return; } if (!mm_initialized) mm_init(); size = GET_SIZE(HDRP(bp)); prev_alloc = GET_PREV_ALLOC(HDRP(bp)); /* update header and footer pointers of this block */ PUT(HDRP(bp), PACK(size, prev_alloc)); PUT(FTRP(bp), PACK(size, prev_alloc)); /* tell next block that this block is now free */ temp_value = GET(HDRP(NEXT_BLKP(bp))) & ~0x2; PUT(HDRP(NEXT_BLKP(bp)), temp_value); coalesce(bp); dbg1("[OUT] : free()\n"); }
void git_blame__like_git(git_blame *blame, uint32_t opt) { while (true) { git_blame__entry *ent; git_blame__origin *suspect = NULL; /* Find a suspect to break down */ for (ent = blame->ent; !suspect && ent; ent = ent->next) if (!ent->guilty) suspect = ent->suspect; if (!suspect) return; /* all done */ /* We'll use this suspect later in the loop, so hold on to it for now. */ origin_incref(suspect); pass_blame(blame, suspect, opt); /* Take responsibility for the remaining entries */ for (ent = blame->ent; ent; ent = ent->next) { if (same_suspect(ent->suspect, suspect)) { ent->guilty = true; ent->is_boundary = !git_oid_cmp( git_commit_id(suspect->commit), &blame->options.oldest_commit); } } origin_decref(suspect); } coalesce(blame); }
//Called by mm_alloc, places void place (void *bp, size_t asize){ size_t size = GET_SIZE(HDRP(bp)); size_t frag = size - asize; //internal fragment if (frag < MINBLKSIZE){ PUT(HDRP(bp), PACK(size, 1)); PUT(FTRP(bp), PACK(size, 1)); delete_free_block(bp); //remove allocated block from free list // fwdPointers(bp, frag); //Reassigns pointers } //external fragment else{ PUT(HDRP(bp), PACK(asize, 1)); PUT(FTRP(bp), PACK(asize, 1)); //fwdPointers(bp, frag); delete_free_block(bp); bp = NEXT_BLKP(bp); //Move pointer to next block PUT(HDRP(bp), PACK(frag, 0)); PUT(FTRP(bp), PACK(frag, 0)); coalesce(bp); // moveStart(bp); } }
int list_free(void* ptr) { header_t* hptr = (header_t*) (ptr - sizeof(header_t)); #ifdef DEBUG printf("\n------------------------------MEM-FREE--------------------------------\n"); printf("Entering Mem_Free with PTR: %p, HPTR: %p, Magic: %d, Size: %d\n", ptr, hptr, hptr->magic, hptr->size); #endif if(hptr->magic != MAGIC) //Check if its the valid pointer return (-1); if(head == NULL) { #ifdef DEBUG printf("NULL header - nothing to coalesce\n"); //Nothing to coalesce #endif head = (node_t*) hptr; head->size = hptr->size; head->next= NULL; return 0; } //Attach the new incoming ptr as the new head node_t* temp = head; head = (node_t*) hptr; head->size = hptr->size; head->next = temp; //Calling Coalescer coalesce(); return 0; }
void freeRegion(void *r) { if (r != 0) { BlockPrefix_t *p = regionToPrefix(r); /* convert to block */ p->allocated = 0; /* mark as free */ coalesce(p); } }
static void place(void *bp, size_t asize){ size_t csize = HDRSIZE(bp); if ((csize - asize) >= BSIZE) { PUT(HDRP(bp), PACK(asize, 1)); PUT(FTRP(bp), PACK(asize, 1)); del(bp); // deleting current block bp = NEXT_BLKP(bp); PUT(HDRP(bp), PACK(csize-asize, 0)); PUT(FTRP(bp), PACK(csize-asize, 0)); coalesce(bp); } else { PUT(HDRP(bp), PACK(csize, 1)); PUT(FTRP(bp), PACK(csize, 1)); del(bp); } }
/* * mm_free - Freeing a block does nothing. */ void mm_free(void *ptr) //LIFO policy of adding to free list { blockHdr *bp = ptr-SIZE_T_SIZE; blockHdr *nu_bp = coalesce((size_t *) bp); nu_bp->size &= ~1; }
/* * free */ void free (void *ptr) { if (ptr == NULL) { return; } block_mark(get_header(ptr), 0); coalesce(ptr); }
/* * mm_free - Free a block by adding it to the appropriate list and coalescing * it. */ void mm_free(void *ptr) { size_t size = GET_SIZE(HEAD(ptr)); /* Size of block */ /* Unset the reallocation tag on the next block */ UNSET_TAG(HEAD(NEXT(ptr))); /* Adjust the allocation status in boundary tags */ PUT(HEAD(ptr), PACK(size, 0)); PUT(FOOT(ptr), PACK(size, 0)); /* Insert new block into appropriate list */ insert_node(ptr, size); /* Coalesce free block */ coalesce(ptr); /* // Check heap for consistency line_count++; if (CHECK && CHECK_FREE) { mm_check('f', ptr, size); } */ return; }
/********************************************************** * extend_heap * Extend the heap by "words" words, maintaining alignment * requirements of course. Free the former epilogue block * and reallocate its new header **********************************************************/ void *extend_heap(size_t words) { #if DEBUG >= 3 printf(" %zu words ", words); #endif char *bp; size_t size; /* Allocate an even number of words to maintain alignments */ size = (words % 2) ? (words+1) * WSIZE : words * WSIZE; if ( (bp = mem_sbrk(size)) == (void *)-1 ) return NULL; /* Initialize free block header/footer and the epilogue header */ PUT(HDRP(bp), PACK(size, 0)); // free block header PUT(FTRP(bp), PACK(size, 0)); // free block footer PUT(HDRP(NEXT_BLKP(bp)), PACK(0, 1)); // new epilogue header getNext(bp) = NULL; getPrev(bp) = NULL; /* Coalesce if the previous block was free */ return coalesce(bp); }
inline static void* extend_heap(size_t words) { #else static void* extend_heap(size_t words) { #endif char *bp; size_t size; size_t prev_alloc; dbg1("[IN ] : extend_heap()\n"); /* Allocate an even number of words to maintain alignment */ size = (words % 2) ? (words+1) * WSIZE : words * WSIZE; if ((long)(bp = mem_sbrk(size)) < 0) return NULL; /* remember if previous block is allocated */ prev_alloc = GET_PREV_ALLOC(HDRP(bp)); /* Initialize free block header/footer and the epliogue header */ PUT(HDRP(bp), PACK(size, prev_alloc)); /* free block header */ PUT(FTRP(bp), PACK(size, prev_alloc)); /* free block footer */ PUT(HDRP(NEXT_BLKP(bp)), PACK(0, 1)); /* new epilogue header */ epilogue = (unsigned *)HDRP(NEXT_BLKP(bp)); dbg1("[OUT] : extend_heap()\n"); /* Coalesce if the previous block was free */ return coalesce(bp); }
/* * place - Places a block of the given size in the free block pointed to by the given * pointer bp. * * This placement is done using a split strategy. If the difference between the size of block * being allocated (asize) and the total size of the free block (fsize) is greater than or equal * to the mimimum block size, then the block is split into two parts. The first block is the * allocated block of size asize, and the second block is the remaining free block with a size * corresponding to the difference between the two block sizes. */ static void place(void *bp, size_t asize) { // Gets the total size of the free block size_t fsize = GET_SIZE(HDRP(bp)); // Case 1: Splitting is performed if((fsize - asize) >= (MINBLOCKSIZE)) { PUT(HDRP(bp), PACK(asize, 1)); PUT(FTRP(bp), PACK(asize, 1)); remove_freeblock(bp); bp = NEXT_BLKP(bp); PUT(HDRP(bp), PACK(fsize-asize, 0)); PUT(FTRP(bp), PACK(fsize-asize, 0)); coalesce(bp); } // Case 2: Splitting not possible. Use the full free block else { PUT(HDRP(bp), PACK(fsize, 1)); PUT(FTRP(bp), PACK(fsize, 1)); remove_freeblock(bp); } }
void mm_free(void* bp) { size_t size = GET_SIZE(HDRP(bp)); PUT(HDRP(bp), PACK(size, 0)); PUT(FTRP(bp), PACK(size, 0)); coalesce(bp); }
void mm_free(void *ptr) { size_t size = GET_SIZE(HDRP(ptr)); PUT(HDRP(ptr), PACK(size, 0)); PUT(FTRP(ptr), PACK(size, 0)); coalesce(ptr); }
/* * place - Place block of asize bytes at start of free block bp * * This function places the block by comparing asize with the total * block size, csize. If the difference is at least the minimum block * size, we can split the block into an allocated block and a free block. * If not, we declare the whole block as allocated to avoid excessive * external fragmentation. * * This function takes a block pointer to a free block and the size of the * block we wish to place there. */ static void place(void *bp, size_t asize) { /* Gets the size of the whole free block */ size_t csize = GET_SIZE(HDRP(bp)); /* If the difference is at least 24 bytes, change the header and footer * info for the allocated block (size = asize, allocated = 1) and * remove the block from the free list. * Then, split the block by: * (1) Changing the header and footer info for the free block created from the * remaining space (size = csize-asize, allocated = 0) * (2) Coalescing the new free block with adjacent free blocks */ if ((csize - asize) >= MINIMUM) { PUT(HDRP(bp), PACK(asize, 1)); PUT(FTRP(bp), PACK(asize, 1)); removeBlock(bp); bp = NEXT_BLKP(bp); PUT(HDRP(bp), PACK(csize-asize, 0)); PUT(FTRP(bp), PACK(csize-asize, 0)); coalesce(bp); } /* If the remaining space is not enough for a free block, don't split the block */ else { PUT(HDRP(bp), PACK(csize, 1)); PUT(FTRP(bp), PACK(csize, 1)); removeBlock(bp); } }
/********************************************************** * extend_heap * Extend the heap by "words" words, maintaining alignment * requirements of course. Free the former epilogue block * and reallocate its new header **********************************************************/ void *extend_heap(size_t words) { // printf("Entered extend heap\n"); char *bp; size_t size; /* Allocate an even number of words to maintain alignments */ size = (words % 2) ? (words+1) * WSIZE : words * WSIZE; /* Find the size of the last block in the heap (if free) */ void* p = heap_listp + (heap_length * DSIZE - DSIZE); if (GET_ALLOC(p) == 0) { size -= GET_SIZE(p); } if ( (bp = mem_sbrk(size)) == (void *)-1 ) return NULL; /* Initialize free block header/footer and the epilogue header */ PUT(HDRP(bp), PACK(size, 0)); // free block header PUT(FTRP(bp), PACK(size, 0)); // free block footer PUT(HDRP(NEXT_BLKP(bp)), PACK(0, 1)); // new epilogue header heap_length += (size >> 4); // printf("Exiting extend heap, new heap_length: %d\n", heap_length); /* Coalesce if the previous block was free */ return coalesce(bp); }
/** * extend_heap - Extend the heap by number of bytes adjusted_size. * * This differs from the example extend_heap function in that the parameter * passed is in BYTES rather than WORDS. Constantly converting between the two * is confusing and unnecessary. * * Furthermore, it should be a size already adjusted to fit byte and header * alignment. This function merely sets header/footer/successor as needed. */ static void *extend_heap(size_t adjusted_size) { char *bp; size_t prev_alloc; TRACE("Entering extend_heap(adjusted_size=%u)\n", adjusted_size); if ((long)(bp = mem_sbrk(adjusted_size)) == -1) return NULL; /* Initialize free block header/footer and the epilogue header. heap_end points to one byte before the next payload, so reading the PREVALLOC field of heap_end + 1 will yield the actual prev-alloc for the block just before the end of the heap. */ prev_alloc = GET_PREVALLOC(heap_end + 1); /* Free block header */ PUTW(GET_BLOCKHDR(bp), PACK(adjusted_size, prev_alloc)); /* Free block header */ PUTW(GET_BLOCKFTR(bp), PACK(adjusted_size, prev_alloc)); /* New epilogue header */ PUTW(GET_BLOCKHDR(GET_NEXTBLOCK(bp)), PACK(0xEA7F00D0, THISALLOC)); heap_end = mem_heap_hi(); TRACE("<<<---Leaving extend_heap() with a call to coalesce()\n"); /* Coalesce if the previous block was free */ return coalesce(bp); /* coalesce handles adding block to free list */ }
/* * Requires: * "bp" is either the address of an allocated block or NULL. * * Effects: * Free a block. */ void mm_free(void *bp) { size_t size; /* Ignore spurious requests. */ if (bp == NULL) return; /* Free and coalesce the block. */ size = GET_SIZE(HDRP(bp)); bool prev_alloc = GET_PREV_ALLOC(HDRP(bp)); if(!prev_alloc) { PUT(HDRP(bp), PACK(size, 0)); PUT(FTRP(bp), PACK(size, 0)); } else { PUT(HDRP(bp), PACK(size, 2)); PUT(FTRP(bp), PACK(size, 2)); } int *next = NEXT_BLKP(bp); size_t size_next = GET_SIZE(HDRP(next)); bool next_alloc = GET_ALLOC(HDRP(next)); if(!next_alloc) { PUT(HDRP(next), PACK(size_next, 0)); PUT(FTRP(next), PACK(size_next, 0)); } else { PUT(HDRP(next), PACK(size_next, 1)); } coalesce(bp); }
static void *extend_heap(size_t words) { char *bp; size_t size=words;//=words*WSIZE; dbg_printf("extend heap\n"); if ((long)(bp = mem_sbrk(size)) == -1) return NULL; /* Initialize free block header/footer and the epilogue header */ PUT(HDRP(bp), PACK(size, 0)); /* Free block header */ PUT(FTRP(bp), PACK(size, 0)); /* Free block footer */ PUT(HDRP(NEXT_BLKP(bp)), PACK(0, 1)); /* New epilogue header */ heap_top=HDRP(NEXT_BLKP(bp)); /* Coalesce if the previous block was free */ /*size_t prev_alloc=1&(*((unsigned int *)bp-2)); if (prev_alloc) add_free_block(bp); else { void *prev=PREV_BLKP(bp); size+=GET_SIZE(HDRP(prev)); delete_free_block(prev); PUT(FTRP(bp),PACK(size,0)); PUT(HDRP(prev),PACK(size,0)); bp=prev; add_free_block(bp); } return bp; */ dbg_printf("extend%p\n",bp); return coalesce(bp); }
/* * Extends the heap with a new free block * Return: the pointer to the new free block * NULL on error. */ static void *extend_heap(unsigned int words) { REQUIRES(words > 4); uint32_t *block; uint32_t *next; /* Ask for 2 more words for header and footer */ words = (words % 2) ? (words + 1) : words; if (VERBOSE) printf("Extend Words = %d bytes\n", words * 4); if ((long)(block = mem_sbrk(words * WSIZE)) == -1) return NULL; block--; // back step 1 since the last one is the epi block set_size(block, words - 2); block_mark(block, FREE); ENSURES(block != NULL); // New eqilogue block next = block_next(block); set_size(next, 0); *next |= 0x40000000; //block_mark(block_next(block), ALLOCATED); ENSURES(!block_free(next)); ENSURES(block_size(next) == 0); block = coalesce(block); // Coalesce if necessary ENSURES(in_list(block)); return block; }