• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

C++ FTRP函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了C++中FTRP函数的典型用法代码示例。如果您正苦于以下问题:C++ FTRP函数的具体用法?C++ FTRP怎么用?C++ FTRP使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。



在下文中一共展示了FTRP函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的C++代码示例。

示例1: GET_ALLOC

/* $begin mmfree */
static void *coalesce(void *bp)
{
    size_t prev_alloc = GET_ALLOC(FTRP(PREV_BLKP(bp)));
    size_t next_alloc = GET_ALLOC(HDRP(NEXT_BLKP(bp)));
    size_t size = GET_SIZE(HDRP(bp));
    
    if (prev_alloc && next_alloc) {            /* Case 1 */
        return bp;
    }
    
    else if (prev_alloc && !next_alloc) {      /* Case 2 */
        size += GET_SIZE(HDRP(NEXT_BLKP(bp)));
        PUT(HDRP(bp), PACK(size, 0));
        PUT(FTRP(bp), PACK(size,0));
    }
    
    else if (!prev_alloc && next_alloc) {      /* Case 3 */
        size += GET_SIZE(HDRP(PREV_BLKP(bp)));
        PUT(FTRP(bp), PACK(size, 0));
        PUT(HDRP(PREV_BLKP(bp)), PACK(size, 0));
        bp = PREV_BLKP(bp);
    }
    
    else {                                     /* Case 4 */
        size += GET_SIZE(HDRP(PREV_BLKP(bp))) +
	    GET_SIZE(FTRP(NEXT_BLKP(bp)));
        PUT(HDRP(PREV_BLKP(bp)), PACK(size, 0));
        PUT(FTRP(NEXT_BLKP(bp)), PACK(size, 0));
        bp = PREV_BLKP(bp);
    }
    /* $end mmfree */
#ifdef NEXT_FIT
    /* Make sure the rover isn't pointing into the free block */
    /* that we just coalesced */
    if ((rover > (char *)bp) && (rover < NEXT_BLKP(bp)))
        rover = bp;
#endif
    /* $begin mmfree */
    return bp;
}
开发者ID:liopei19nn,项目名称:15213_LIP,代码行数:41,代码来源:mm.c


示例2: GET_ALLOC

/*
 * coalesce the previous block-curent block-next block
    IF handled corrected, each free will call this method, and no two consecutive freed block would exsist.
 */
static void *coalesce(void *bp){
    size_t prev_alloc = GET_ALLOC(FTRP(PREV_BLKP(bp)));
    size_t next_alloc = GET_ALLOC(HDRP(NEXT_BLKP(bp)));
    size_t size = GET_SIZE(HDRP(bp));

    if(prev_alloc && next_alloc)
        return bp;

    else if(prev_alloc && !next_alloc){
        size += GET_SIZE(HDRP(NEXT_BLKP(bp)));
        PUT(HDRP(bp), PACK(size, 0));
        PUT(FTRP(bp), PACK(size, 0));
    }else if(!prev_alloc && next_alloc){
        size += GET_SIZE(FTRP(PREV_BLKP(bp)));
        PUT(HDRP(PREV_BLKP(bp)), PACK(size, 0));
        PUT(FTRP(bp), PACK(size, 0));
        bp = PREV_BLKP(bp);
    }else{
        size += GET_SIZE(HDRP(NEXT_BLKP(bp))) + GET_SIZE(FTRP(PREV_BLKP(bp)));
        PUT(HDRP(PREV_BLKP(bp)), PACK(size, 0));
        PUT(FTRP(NEXT_BLKP(bp)), PACK(size, 0));
        bp = PREV_BLKP(bp);
    }

    if((rover > (char*)bp) && (rover < NEXT_BLKP(bp)))
        rover = bp;

    return bp;
}
开发者ID:cmxcn,项目名称:CSAPP_labs,代码行数:33,代码来源:mm_Impl_nf.c


示例3: GET_ALLOC

//
// coalesce - boundary tag coalescing. Return ptr to coalesced block
//
static void *coalesce(void *bp) 
{
  size_t prev_alloc = GET_ALLOC(FTRP(PREV_BLKP(bp)));
  size_t next_alloc = GET_ALLOC(FTRP(NEXT_BLKP(bp)));
  size_t size = GET_SIZE(HDRP(bp));

  if (prev_alloc && next_alloc) { 	// Case 1
    return bp;
  }

  else if (prev_alloc && !next_alloc) { // Case 2
    size += GET_SIZE(HDRP(NEXT_BLKP(bp)));
    PUT(HDRP(bp), PACK(size, 0));
    PUT(FTRP(bp), PACK(size, 0));
  }

  else if (!prev_alloc && next_alloc) { // Case 3
    size += GET_SIZE(HDRP(PREV_BLKP(bp)));
    PUT(FTRP(bp), PACK(size, 0));
    PUT(HDRP(PREV_BLKP(bp)), PACK(size, 0));
    bp = PREV_BLKP(bp);
  }

  else {
    size =+ GET_SIZE(HDRP(PREV_BLKP(bp))) + GET_SIZE(FTRP(NEXT_BLKP(bp)));
    PUT(HDRP(PREV_BLKP(bp)), PACK(size, 0));
    PUT(FTRP(NEXT_BLKP(bp)), PACK(size, 0));
    bp = PREV_BLKP(bp);
  }
  return bp;
}
开发者ID:kerns95,项目名称:malloc,代码行数:34,代码来源:mm.backup.c


示例4: PREV_BLKP

//
// coalesce - boundary tag coalescing. Return ptr to coalesced block
//
static void *coalesce(void *bp)
{
	FL_Pointer prev = PREV_BLKP(bp);
	FL_Pointer next = NEXT_BLKP(bp);
	
	size_t prev_alloc = GET_ALLOC(FTRP(prev));
	size_t next_alloc = GET_ALLOC(HDRP(next));
	size_t size = GET_SIZE(HDRP(bp));
	
	if (prev_alloc && next_alloc) {
		CL_tack(&free_list, bp); // case 1 neighbours = 1  add a return?
	}
	
	else if (prev_alloc && !next_alloc) { // case 2 next is free
		CL_unlink(next);
		CL_tack(&free_list, bp);
		
		size += GET_SIZE(HDRP(next));
		PUT(HDRP(bp), PACK(size, 0));
		PUT(FTRP(bp), PACK(size, 0));
	}
	
	else if (!prev_alloc && next_alloc) { // case 3 prev is free
		size += GET_SIZE(HDRP(prev));
		PUT(FTRP(bp), PACK(size, 0));
		PUT(HDRP(prev), PACK(size, 0));
		bp = prev;
	}

	else { // both next and prev free
		size += GET_SIZE(HDRP(prev)) + GET_SIZE(FTRP(next));
		CL_unlink(next);
		PUT(HDRP(prev), PACK(size, 0 ));
		PUT(FTRP(next), PACK(size, 0));
		bp = prev;
	}
	
	return bp;
}
开发者ID:Boraz,项目名称:CSCI-2400,代码行数:42,代码来源:mm.c


示例5: mm_free

/* 
 * 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));
  PUT(HDRP(bp), PACK(size, 0));
  PUT(FTRP(bp), PACK(size, 0));
  coalesce(bp);
}
开发者ID:nik-6947,项目名称:malloc,代码行数:20,代码来源:mm.c


示例6: mm_free

/*
 * mm_free - Freeing a block does nothing.
 *
 * Role : The mm_free routine frees the block pointed to by ptr
 *
 * Return value : returns nothing
 */
void mm_free(void *ptr)
{
    size_t size = GET_SIZE(HDRP(ptr));
 
    REMOVE_RATAG(HDRP(NEXT_BLKP(ptr)));
    PUT(HDRP(ptr), PACK(size, 0));
    PUT(FTRP(ptr), PACK(size, 0));
    
    insert_node(ptr, size);
    coalesce(ptr);
    
    return;
}
开发者ID:gowtamvamsi,项目名称:Malloc-Lab,代码行数:20,代码来源:mm.c


示例7: add_to_group

static void add_to_group(char* bp) {
    int size = GET_SIZE(HDRP(bp));
    int group = get_group(size);
    void* heap_listp_new = heap_listp - (GROUPSIZE + (2*WSIZE));
    int** grpp = heap_listp_new + (group * WSIZE);    
    if (*grpp) {
        int* tmpptr = *grpp;
        PUT(HDRP(bp) + 4, (int)tmpptr);
        PUT(FTRP(bp) + 4, (int)tmpptr);
    }    
    //printf("group: %d", group);
    PUTPTR(grpp, bp);
}
开发者ID:drewmacmac,项目名称:old_class,代码行数:13,代码来源:mm_backup_11_21.c


示例8: GET_ALLOC

/* Looks to the right and to the left to combine with nearby free blocks
in order to minimize fragmentation. */
static void *coalesce(void *bp) {
    size_t prev_alloc = GET_ALLOC(FTRP(PREV_BLKP(bp))) || PREV_BLKP(bp) == bp;
    size_t next_alloc = GET_ALLOC(HDRP(NEXT_BLKP(bp)));
    size_t size = GET_SIZE(HDRP(bp));

    /* Coalesce with block to the right */
    if (prev_alloc && !next_alloc) {
        size += GET_SIZE(HDRP(NEXT_BLKP(bp)));
        /* Now remove coalesced block to make room for actual coalesce */
        deleteBlk(NEXT_BLKP(bp));
        PUT(HDRP(bp), PACK(size, 0));
        PUT(FTRP(bp), PACK(size, 0));
    }
    /* Coalesce with block to the left */
    else if (!prev_alloc && next_alloc) {
        size += GET_SIZE(HDRP(PREV_BLKP(bp)));
        bp = PREV_BLKP(bp);
        deleteBlk(bp); // Delete previous blck to make room
        PUT(HDRP(bp), PACK(size, 0));
        PUT(FTRP(bp), PACK(size, 0));
    }
    /* Coalesce with both left and right blocks */
    else if (!prev_alloc && !next_alloc) {
        size += GET_SIZE(HDRP(PREV_BLKP(bp))) +
                GET_SIZE(HDRP(NEXT_BLKP(bp)));
        /* Deletes next and previous blocks to make more room,
        then reassigns bp to reflect coalescing */
        deleteBlk(PREV_BLKP(bp));
        deleteBlk(NEXT_BLKP(bp));
        bp = PREV_BLKP(bp);
        PUT(HDRP(bp), PACK(size, 0));
        PUT(FTRP(bp), PACK(size, 0));
    }

    /* Insert newly freed block at the front of free list */
    reinsert(bp);

    return bp;
}
开发者ID:NikhilCMU,项目名称:Coding_Projects,代码行数:41,代码来源:mm.c


示例9: place

/* place - 
 * Places the requested block at the beginning of the freeblock, and splitting * only if the size of the remainder block would equal or exceed the minimum  * block size */
static void place(void *bp, size_t asize) {
    size_t csize = GET_SIZE(HDRP(bp));   
    size_t remainder = csize - asize;

    if (remainder >= (MINSIZE * WSIZE)) {          
        remove_from_list(find_list(GET_SIZE(HDRP(bp))), bp);
        SET_SIZE(HDRP(bp), asize);
        SET_ALLOC(HDRP(bp));
        bp = NEXT_BLKP(bp);
        SET_SIZE(HDRP(bp), remainder);
        SET_SIZE(FTRP(bp), remainder);
        UNSET_ALLOC(HDRP(bp));
        UNSET_ALLOC(FTRP(bp));        
        SET_TAG(HDRP(bp));
        SET_TAG(FTRP(bp));        
        add_to_list(find_list(GET_SIZE(HDRP(bp))), bp);
    }else { 
        remove_from_list(find_list(GET_SIZE(HDRP(bp))), bp);
        SET_ALLOC(HDRP(bp));
        SET_TAG(HDRP(NEXT_BLKP(bp)));
    }
}
开发者ID:csukuangfj,项目名称:15-213-Introduction-to-Computer-Systems,代码行数:24,代码来源:mm.c


示例10: print_free_block

/**
 * print_free_block - Print the contents of the given free block, including 
 *                    the pointers
 * @param bp Block to be printed
 */
static void print_free_block(void *bp) 
{
    size_t hsize = -1, halloc, fsize, falloc;

    if(bp != NULL) {
    hsize = GET_SIZE(HDRP(bp));
    halloc = GET_ALLOC(HDRP(bp));  
    fsize = GET_SIZE(FTRP(bp));
    falloc = GET_ALLOC(FTRP(bp));  
    dbg_printf("%p: header: [%lu:%c] footer: [%lu:%c]\n", bp,hsize, \
        (halloc ? 'a' : 'f'), fsize, (falloc ? 'a' : 'f')); 
    dbg_printf( "%p: next: [%p] prev: [%p]\n  ", bp, GET_NEXTP(bp), \
        GET_PREVP(bp) );
    } else {
        dbg_printf("bp is null\n");
    }

    if (hsize == 0) {
        dbg_printf("%p: EOL\n", bp);
        return;
    }
}
开发者ID:AceYuRanger,项目名称:MallocLab,代码行数:27,代码来源:mm.c


示例11: mm_free

/*
 * frees a block of memory, enabling it to be reused later
 * arguments: ptr: the allocated block to free
 * returns: nothing
 */
void mm_free(void *ptr) {
    size_t size;
    /* Ignore spurious requests. */
    if (ptr == NULL)
    {
	return;
    }
    /* Free and coalesce the block. */
    size = GET_SIZE(HDRP(ptr));
    PUT(HDRP(ptr), PACK(size, 0));
    PUT(FTRP(ptr), PACK(size, 0));
    coalesce(ptr);
}
开发者ID:dukelv,项目名称:csci033,代码行数:18,代码来源:mm.c


示例12: GET_PREV_ALLOC

/*
 * coalesce - Implements boundary-tag coalescing to merge the input block 
 * with any adjacent free blocks in constant time.
 */
static void *coalesce(void *bp) {
    size_t prev_alloc = GET_PREV_ALLOC(HDRP(bp));
    size_t next_alloc = GET_ALLOC(HDRP(NEXT_BLKP(bp)));
    size_t size = GET_SIZE(HDRP(bp));

    if (prev_alloc && next_alloc) {            /* Case 1 */
        UNSET_TAG(HDRP(NEXT_BLKP(bp)));    
        add_to_list(find_list(GET_SIZE(HDRP(bp))), bp);
        return bp;
    }
    else if (prev_alloc && !next_alloc) {      /* Case 2 */
        remove_from_list(find_list(GET_SIZE(HDRP(NEXT_BLKP(bp)))), NEXT_BLKP(bp));
        size += GET_SIZE(HDRP(NEXT_BLKP(bp)));
        PUT(HDRP(bp), PACK(size, 0));
        PUT(FTRP(bp), PACK(size,0));
        SET_TAG(HDRP(bp));
        SET_TAG(FTRP(bp));
    }
    else if (!prev_alloc && next_alloc) {      /* Case 3 */
        remove_from_list(find_list(GET_SIZE(HDRP(PREV_BLKP(bp)))), PREV_BLKP(bp));
        size += GET_SIZE(HDRP(PREV_BLKP(bp)));
        SET_SIZE(FTRP(bp), size);
        SET_SIZE(HDRP(PREV_BLKP(bp)), size);
        bp = PREV_BLKP(bp);
    }
    else {                                     /* Case 4 */
        remove_from_list(find_list(GET_SIZE(HDRP(PREV_BLKP(bp)))), PREV_BLKP(bp));
        remove_from_list(find_list(GET_SIZE(HDRP(NEXT_BLKP(bp)))), NEXT_BLKP(bp));
        size += GET_SIZE(HDRP(PREV_BLKP(bp))) +  GET_SIZE(FTRP(NEXT_BLKP(bp)));
        SET_SIZE(HDRP(PREV_BLKP(bp)), size);
        SET_SIZE(FTRP(NEXT_BLKP(bp)), size);
        bp = PREV_BLKP(bp);
    }
    
    UNSET_TAG(HDRP(NEXT_BLKP(bp)));    
    add_to_list(find_list(GET_SIZE(HDRP(bp))), bp);

    return bp;
}
开发者ID:csukuangfj,项目名称:15-213-Introduction-to-Computer-Systems,代码行数:43,代码来源:mm.c


示例13: GET_SIZE

/* $begin mmplace-proto */
static void *place(void *bp, size_t asize)
// $end mmplace-proto 
{
    size_t csize = GET_SIZE(HDRP(bp));
	void *alloBlock = bp;

    if ((csize - asize) >= (DSIZE + OVERHEAD)) { 
		PUT(HDRP(bp), PACK(asize, 1));
        PUT(FTRP(bp), PACK(asize, 1));
        bp = NEXT_BLKP(bp);
        PUT(HDRP(bp), PACK(csize-asize, 0));
        PUT(FTRP(bp), PACK(csize-asize, 0));
		// setjum free blokkina sem kom út úr splitti í free lista
		insert_block(bp);
    }
    else {
        PUT(HDRP(bp), PACK(csize, 1));
        PUT(FTRP(bp), PACK(csize, 1));
    }
	// skilum pointer á allocated blokkina
	return alloBlock;
}
开发者ID:olikari,项目名称:malloclab,代码行数:23,代码来源:mm.c


示例14: ALIGN

/*
 * extend_heap - Extends the heap when the desired memory is not available
 *
 */
static void *extend_heap(size_t words) {
    char *bp;
    size_t size=words*WSIZE;
    size = ALIGN(size+SIZE_T_SIZE); /* Set the size of the block to follow double word Allignment */
 \
    if ((int)(bp = mem_sbrk(size)) == -1)
        return NULL;

    PUT(HDRP(bp), PACK(size, 0)); /* Sets the Header of the Block */
    PUT(FTRP(bp), PACK(size, 0)); /* Sets the Footer of the Block */
    PUT(HDRP(NEXT_BLKP(bp)), PACK(0, 1)); /* Epilogue has to be created at the end */
    return coalesce(bp); /* Coalescing the Free blocks (Immediate Coalescing) */
}
开发者ID:punit9462,项目名称:IT-Projects,代码行数:17,代码来源:mm.c


示例15: place

static void place (void*bp, size_t asize){
    size_t csize = GET_SIZE(HDRP(bp));

    offlist(bp);
    if((csize - asize) >= (2*DSIZE)) {
        PUT(HDRP(bp),PACK(asize,1));
        PUT(FTRP(bp),PACK(asize,1));
        bp = NEXT_BLKP(bp);
        PUT(HDRP(bp),PACK(csize-asize,0));
        PUT(FTRP(bp),PACK(csize-asize,0));
        
        PUT(bp, endfree);
        PUT(SUC(bp), NULL);
        if (endfree)
            PUT(SUC(endfree), bp);
        endfree = bp;
    }
    else{
        PUT(HDRP(bp),PACK(csize,1));
        PUT(FTRP(bp),PACK(csize,1));
    }
}
开发者ID:RayneZhang,项目名称:csapp-lab,代码行数:22,代码来源:mm.c


示例16: printblock

static void printblock(void *bp)
{
  REQUIRES (bp!=NULL);
  REQUIRES ((size_t)(bp)%8 == 0);
  size_t hsize, halloc, fsize, falloc;

  // checkheap(0);
  hsize = GET_SIZE(HDRP((bp)));
  halloc = GET_ALLOC(HDRP((bp)));
  fsize = GET_SIZE(FTRP((bp)));
  falloc = GET_ALLOC(FTRP((bp)));

  printf("%p: header: [%d:%c] footer: [%d:%c]\n", bp,
          (int)hsize, (halloc ? 'a' : 'f'),
          (int)fsize, (falloc ? 'a' : 'f'));

  if (hsize == 0) {
    printf("%p: EOL\n", bp);
    return;
  }

}
开发者ID:msr23trini,项目名称:malloclab,代码行数:22,代码来源:mm.c


示例17: GET_ALLOC

void *coalesce(void *bp) {
	size_t previous_alloc = GET_ALLOC(FTRP(PREV_BLKP(bp)));
	size_t next_alloc = GET_ALLOC(HDRP(NEXT_BLKP(bp)));
	size_t size = GET_SIZE(HDRP(bp));
	/* Previous & Next both are allocated. DO NOT COALESCE */
	if (previous_alloc && next_alloc) {
		addFreeBlock(bp);
		return bp;
	}
	/* Previous is allocated, Next is free. */
	else if (previous_alloc && !next_alloc) {
		delFreeBlock(NEXT_BLKP(bp));
		size += GET_SIZE(HDRP(NEXT_BLKP(bp)));
		PUT(HDRP(bp), PACK(size, 0));
		PUT(FTRP(bp), PACK(size, 0));
		addFreeBlock(bp);
	}
	/* Previous is free, Next is Allocated */
	else if (!previous_alloc && next_alloc) {
		delFreeBlock(PREV_BLKP(bp));
		size += GET_SIZE(HDRP(PREV_BLKP(bp)));
		PUT(FTRP(bp), PACK(size, 0));
		PUT(HDRP(PREV_BLKP(bp)), PACK(size, 0));
		bp = PREV_BLKP(bp);
		addFreeBlock(bp);
	}
	/* Both previousious & next are free */
	else {
		delFreeBlock(PREV_BLKP(bp));
		delFreeBlock(NEXT_BLKP(bp));
		size+=GET_SIZE(HDRP(PREV_BLKP(bp)));
		size+=GET_SIZE(HDRP(NEXT_BLKP(bp)));
		PUT(HDRP(PREV_BLKP(bp)), PACK(size, 0));
		PUT(FTRP(NEXT_BLKP(bp)), PACK(size, 0));
		bp = PREV_BLKP(bp);
		addFreeBlock(bp);
	}
	return bp; /* return the void pointer ... anyway we are not getting to catch it */
}
开发者ID:tushar7795,项目名称:malloc,代码行数:39,代码来源:mm.c


示例18: GET_ALLOC

static void *coalesce(void *bp)
{    
    size_t prev_alloc = GET_ALLOC(FTRP(PREV_BLKP(bp))); 
    size_t next_alloc = GET_ALLOC(HDRP(NEXT_BLKP(bp)));
    size_t size = GET_SIZE(HDRP(bp));

    if (prev_alloc && next_alloc) /* Case 1 */
    {
        return bp;
    }

    else if (prev_alloc && !next_alloc) /* Case 2 */
    {
        size += GET_SIZE(HDRP(NEXT_BLKP(bp)));
        PUT(HDRP(bp), PACK(size, 0));
        PUT(FTRP(bp), PACK(size,0));
    }

    else if (!prev_alloc && next_alloc) /* Case 3 */
    {
        char * pbp = HDRP(PREV_BLKP(bp));
        size += GET_SIZE(pbp);
        PUT(FTRP(bp), PACK(size, 0));
        PUT(pbp, PACK(size, 0));
        bp = PREV_BLKP(bp);
    }

    else /* Case 4 */ 
    {
        char * pbp = HDRP(PREV_BLKP(bp));
        char * nbp = FTRP(NEXT_BLKP(bp));
        size += GET_SIZE(pbp) + GET_SIZE(nbp);
        PUT(pbp, PACK(size, 0)); 
        PUT(nbp, PACK(size, 0));
        bp = PREV_BLKP(bp);
    }
    
    return bp;
}
开发者ID:nmank,项目名称:comp-organization,代码行数:39,代码来源:mm.c


示例19: coalesce_next

/*
 * coalesce next block with current block
 * this method only called by coalesce,
 * pre-condistion is bp block is free and its next block in heap is free too,
 * so we merge them
 */
void coalesce_next(void *bp){
	size_t size = GET_SIZE(bp);
	void *next_bp = NEXT_BLKP(bp);	
	
	/* delete next_bp from free list */
	SET_NEXT(GET_PREV(next_bp), GET_NEXT(next_bp));
	SET_PREV(GET_NEXT(next_bp), GET_PREV(next_bp));
	
	/* reset hdr/ftr of the new free block */
	size += GET_SIZE(NEXT_BLKP(bp));
	PUT(HDRP(bp), PACK(size, 0));
	PUT(FTRP(bp), PACK(size, 0));
}
开发者ID:yihanwan,项目名称:15513,代码行数:19,代码来源:mm.c


示例20: GET_ALLOC

/**
 * coalesce - Coalesce the blocks to avoid fragmentation.
 *            Need to be done after every block free.
 * @param bp Block pointer to block to be coalesced
 */
static inline void *coalesce_block(void *bp) 
{   
    size_t prev_alloc = GET_ALLOC(FTRP(PREV_BLKP(bp)));
    size_t next_alloc = GET_ALLOC(HDRP(NEXT_BLKP(bp)));
    size_t size = GET_SIZE(HDRP(bp));
    /*The line kept, so that compiler dont complain.*/
    if(size == 0) {
        checkheap(9);
    }

    if ( prev_alloc && next_alloc ) {               /* Case 1 */
        return bp;               
    } else if ( prev_alloc && !next_alloc ) {       /* Case 2 */
        size += GET_SIZE( HDRP( NEXT_BLKP( bp )));
        /* Next is not allocated-- remove from free list */
        delete_free_list( NEXT_BLKP( bp ));
        PUT( HDRP( bp ), PACK( size, 0 ));
        PUT( FTRP( bp ), PACK( size, 0 ));
    } else if ( !prev_alloc && next_alloc ) {       /* Case 3 */
        bp = PREV_BLKP(bp);
        size += GET_SIZE(HDRP(bp));
        /* Previous is not allocated-- remove from free list */
        delete_free_list(bp);
        PUT(HDRP(bp), PACK(size,0));
        PUT(FTRP(bp), PACK(size,0));
    } else {                                        /* Case 4 */
        /* Both Previous and Next Block are not allocated */
        size += GET_SIZE(HDRP(PREV_BLKP(bp))) + 
            GET_SIZE(FTRP(NEXT_BLKP(bp)));
        /* Remove both previous and next */
        delete_free_list(PREV_BLKP(bp));
        delete_free_list(NEXT_BLKP(bp));
        PUT(HDRP(PREV_BLKP(bp)), PACK(size, 0));
        PUT(FTRP(NEXT_BLKP(bp)), PACK(size, 0));
        bp = PREV_BLKP(bp);
    }
    return bp;
}
开发者ID:AceYuRanger,项目名称:MallocLab,代码行数:43,代码来源:mm.c



注:本文中的FTRP函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C++ FT_ALLOC函数代码示例发布时间:2022-05-30
下一篇:
C++ FTRACE函数代码示例发布时间:2022-05-30
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap