/* Free the block referenced by ptr. */
void mm_free (void *ptr) {
  size_t payloadSize;
  BlockInfo * blockInfo;
  BlockInfo * followingBlock;

  // Implement mm_free.  You can change or remove the declaraions
  // above.  They are included as minor hints.
  blockInfo = (BlockInfo*)POINTER_SUB(ptr, WORD_SIZE);
  payloadSize = SIZE(blockInfo->sizeAndTags);
  followingBlock = (BlockInfo*)POINTER_ADD(blockInfo, payloadSize);

  // update the status in the current block: reset TAG_USED in both head and boundary tag
  size_t precedingBlockUseTag = blockInfo->sizeAndTags & TAG_PRECEDING_USED;
  blockInfo->sizeAndTags = payloadSize | precedingBlockUseTag;
  *((size_t*)POINTER_ADD(blockInfo, payloadSize - WORD_SIZE)) = payloadSize | precedingBlockUseTag;

  // update the status in the following block: reset TAG_PRECEDING_USED
  size_t followingBlockSize = SIZE(followingBlock->sizeAndTags);
  size_t followingBlockUsed = followingBlock->sizeAndTags & TAG_USED;
  followingBlock->sizeAndTags = followingBlockSize | followingBlockUsed;
  // if the following block is free, also update its boundary tag
  if ((followingBlock->sizeAndTags & TAG_USED) != TAG_USED) {
    *((size_t*)POINTER_ADD(followingBlock, followingBlockSize - WORD_SIZE)) = followingBlock->sizeAndTags;
  }

  insertFreeBlock(blockInfo);
  coalesceFreeBlock(blockInfo);
  
}
示例#2
0
文件: mm.c 项目: YurieCo/hsi
/* Free the block referenced by ptr. */
void mm_free (void *ptr) {
  size_t payloadSize;
  BlockInfo * blockInfo;
  BlockInfo * followingBlock;  
  
	// Implement mm_free.  You can change or remove the declaraions
  // above.  They are included as minor hints.
    
  // Casts void pointer into BlockInfo, subtracts pointer by Wsize
	blockInfo = (BlockInfo*)POINTER_SUB(ptr, WORD_SIZE);
	
	// Checks if block wasn't freed
	if(((blockInfo->sizeAndTags) & TAG_USED)==0){
		return;
	}
	
	// Marks it as free, adds to free list and coalesces the free blocks together.
	blockInfo->sizeAndTags = blockInfo->sizeAndTags & (~TAG_USED);
	insertFreeBlock(blockInfo);
	coalesceFreeBlock(blockInfo);
	
	// unmarks the next block's preced used tag
	payloadSize = SIZE(blockInfo->sizeAndTags);
	followingBlock = (BlockInfo*)POINTER_ADD(blockInfo, payloadSize);
	followingBlock->sizeAndTags = followingBlock->sizeAndTags &	(~TAG_PRECEDING_USED);

}
示例#3
0
文件: mm.c 项目: YurieCo/hsi
/* Initialize the allocator. */
int mm_init () {
  // Head of the free list.
  BlockInfo *firstFreeBlock;
  // Initial heap size: WORD_SIZE byte header (stores pointer to head
  // of free list), MIN_BLOCK_SIZE bytes of space, WORD_SIZE byte footer.
  int initsize = WORD_SIZE+MIN_BLOCK_SIZE+WORD_SIZE;

  int totalSize;

  void* mem_sbrk_result = mem_sbrk(initsize);
  //  printf("mem_sbrk returned %p\n", mem_sbrk_result);
  if ((int)mem_sbrk_result == -1) {
		printf("ERROR: mem_sbrk failed in mm_init, returning %p\n", 
		         mem_sbrk_result);
		exit(1);
  }

  firstFreeBlock = (BlockInfo*)POINTER_ADD(mem_heap_lo(), WORD_SIZE);

  // total usable size is full size minus header and footer words.
  totalSize = initsize - WORD_SIZE - WORD_SIZE;
  // initialize the free block
  firstFreeBlock->sizeAndTags = totalSize | TAG_PRECEDING_USED;
  firstFreeBlock->next = NULL;
  firstFreeBlock->prev = NULL;
  // boundary tag
  *((int*)POINTER_ADD(firstFreeBlock, totalSize - WORD_SIZE)) = totalSize | TAG_PRECEDING_USED;
  
  // Tag "useless" word at end of heap as used.
  *((int*)POINTER_SUB(mem_heap_hi(), 3)) = TAG_USED;

  // set the head of the free list to this new free block.
  FREE_LIST_HEAD = firstFreeBlock;
  return 0;
}
示例#4
0
/* Free the block referenced by ptr. */
void mm_free (void *ptr) {
  size_t payloadSize;
  BlockInfo * blockInfo;
  BlockInfo * followingBlock;
  size_t bitMask;

  // Implement mm_free.  You can change or remove the declaraions
  // above.  They are included as minor hints.
 
  // set BlockInfo pointer to include header
  blockInfo = (BlockInfo*) POINTER_SUB(ptr, WORD_SIZE);
  payloadSize = SIZE(blockInfo->sizeAndTags) - WORD_SIZE;
  followingBlock = (BlockInfo*) POINTER_ADD(ptr, payloadSize + WORD_SIZE);
  
  // set header (first tags, then size)
  bitMask = ~0 << 1;
  blockInfo->sizeAndTags &= bitMask; /* preserves all bits, except sets lowest
				      to 0 (unsetting used tag)  */
  // copy header into footer
  *((size_t*) POINTER_ADD(blockInfo, payloadSize)) = blockInfo->sizeAndTags;
  
  // set preceding use tag for following block
  bitMask = (~0 << 2) | 1;
  followingBlock->sizeAndTags &= bitMask; /* preserves all bits except 2nd
						lowest bit */

  // insert into free list and coalesce
  insertFreeBlock(blockInfo);
  coalesceFreeBlock(blockInfo);
}
示例#5
0
/* Free the block referenced by ptr. */
void mm_free (void *ptr) {
  size_t payloadSize;
  BlockInfo * blockInfo;
  BlockInfo * nextBlock;
  
  // Implement mm_free.  You can change or remove the declaraions
  // above.  They are included as minor hints.
  blockInfo = (BlockInfo* )POINTER_SUB(ptr, WORD_SIZE);
  payloadSize = SIZE(blockInfo->sizeAndTags);
  blockInfo->sizeAndTags = blockInfo->sizeAndTags & ~TAG_USED;
  *((size_t*)POINTER_ADD(blockInfo, payloadSize- WORD_SIZE)) = blockInfo->sizeAndTags;
  nextBlock = (BlockInfo* )POINTER_ADD(blockInfo, payloadSize);
  nextBlock->sizeAndTags = nextBlock->sizeAndTags & ~TAG_PRECEDING_USED;
  insertFreeBlock(blockInfo);
  coalesceFreeBlock(blockInfo);
}
/* Initialize the allocator. */
int mm_init () {
  // Head of the free list.
  BlockInfo *firstFreeBlock;

  // Initial heap size: WORD_SIZE byte heap-header (stores pointer to head
  // of free list), MIN_BLOCK_SIZE bytes of space, WORD_SIZE byte heap-footer.
  size_t initSize = WORD_SIZE+MIN_BLOCK_SIZE+WORD_SIZE;
  size_t totalSize;

  void* mem_sbrk_result = mem_sbrk(initSize);
  //  printf("mem_sbrk returned %p\n", mem_sbrk_result);
  if ((ssize_t)mem_sbrk_result == -1) {
    printf("ERROR: mem_sbrk failed in mm_init, returning %p\n",
           mem_sbrk_result);
    exit(1);
  }

  firstFreeBlock = (BlockInfo*)POINTER_ADD(mem_heap_lo(), WORD_SIZE);

  // Total usable size is full size minus heap-header and heap-footer words
  // NOTE: These are different than the "header" and "footer" of a block!
  // The heap-header is a pointer to the first free block in the free list.
  // The heap-footer is used to keep the data structures consistent (see
  // requestMoreSpace() for more info, but you should be able to ignore it).
  totalSize = initSize - WORD_SIZE - WORD_SIZE;

  // The heap starts with one free block, which we initialize now.
  firstFreeBlock->sizeAndTags = totalSize | TAG_PRECEDING_USED;
  firstFreeBlock->next = NULL;
  firstFreeBlock->prev = NULL;
  // boundary tag
  *((size_t*)POINTER_ADD(firstFreeBlock, totalSize - WORD_SIZE)) = totalSize | TAG_PRECEDING_USED;

  // Tag "useless" word at end of heap as used.
  // This is the is the heap-footer.
  *((size_t*)POINTER_SUB(mem_heap_hi(), WORD_SIZE - 1)) = TAG_USED;

  // set the head of the free list to this new free block.
  FREE_LIST_HEAD = firstFreeBlock;
  return 0;
}
示例#7
0
/* Get more heap space of size at least reqSize. */
static void requestMoreSpace(size_t reqSize) {
  size_t pagesize = mem_pagesize();
  size_t numPages = (reqSize + pagesize - 1) / pagesize;
  BlockInfo *newBlock;
  size_t totalSize = numPages * pagesize;
  size_t prevLastWordMask;

  void* mem_sbrk_result = mem_sbrk(totalSize);
  if ((size_t)mem_sbrk_result == -1) {
    printf("ERROR: mem_sbrk failed in requestMoreSpace\n");
    exit(0);
  }
  // SUB cause the last byte of heap is useless when add more space, we will create new last byte in heap to indicate USED later
  newBlock = (BlockInfo*)POINTER_SUB(mem_sbrk_result, WORD_SIZE);

  /* initialize header, inherit TAG_PRECEDING_USED status from the
     previously useless last word however, reset the fake TAG_USED
     bit */
    prevLastWordMask = newBlock->sizeAndTags & TAG_PRECEDING_USED;
    //prevLastWordMask = prevLastWordMask | 0x02;
    //prevLastWordMask = newBlock->sizeAndTags & TAG_USED;
    //prevLastWordMask = prevLastWordMask << 1;
  newBlock->sizeAndTags = totalSize | prevLastWordMask;
  // Initialize boundary tag.
  ((BlockInfo*)POINTER_ADD(newBlock, totalSize - WORD_SIZE))->sizeAndTags =
    totalSize | prevLastWordMask;

  /* initialize "new" useless last word
     the previous block is free at this moment
     but this word is useless, so its use bit is set
     This trick lets us do the "normal" check even at the end of
     the heap and avoid a special check to see if the following
     block is the end of the heap... */
  *((size_t*)POINTER_ADD(newBlock, totalSize)) = 0x03; // seem refer TAG_PRE

  // Add the new block to the free list and immediately coalesce newly
  // allocated memory space
  insertFreeBlock(newBlock);
  coalesceFreeBlock(newBlock);
}
// Unchecked
void* mm_realloc(void* ptr, size_t size) {
  if (ptr == NULL) {
    return mm_malloc(size);
  }
  else if (ptr != NULL && size == 0) {
    free(ptr);
    return NULL;
  } else {
    void *newPtr = mm_malloc(size);
    
    BlockInfo *oldBlock = (BlockInfo*)POINTER_SUB(ptr, WORD_SIZE);
    size_t oldBlockSize = oldBlock->sizeAndTags;
    size_t oldDataSize = oldBlockSize - WORD_SIZE;
    size_t sizeToCopy = size < oldDataSize ? size : oldDataSize;
    size_t i;
    // assume size to be copies is the multiple of WORD_SIZE
    for (i = 0; i < sizeToCopy; ++i) {
      *(char*)newPtr = *(char*)ptr;       
    }

    free(ptr);
    return newPtr;
  }
}
/* Coalesce 'oldBlock' with any preceeding or following free blocks. */
static void coalesceFreeBlock(BlockInfo* oldBlock) {
  BlockInfo *blockCursor;
  BlockInfo *newBlock;
  BlockInfo *freeBlock;
  // size of old block
  size_t oldSize = SIZE(oldBlock->sizeAndTags);
  // running sum to be size of final coalesced block
  size_t newSize = oldSize;

  // Coalesce with any preceding free block
  blockCursor = oldBlock;
  while ((blockCursor->sizeAndTags & TAG_PRECEDING_USED)==0) {
    // While the block preceding this one in memory (not the
    // prev. block in the free list) is free:

    // Get the size of the previous block from its boundary tag.
    size_t size = SIZE(*((size_t*)POINTER_SUB(blockCursor, WORD_SIZE)));
    // Use this size to find the block info for that block.
    freeBlock = (BlockInfo*)POINTER_SUB(blockCursor, size);

    // my code for checking consistency of cur block's TAG_USED
    // and following block's TAG_PRECEDING_USED
    if ((freeBlock->sizeAndTags & TAG_USED) == TAG_USED) {
      fprintf (stderr, "Oops! inconsistency in coalesce %p and %p!\n", freeBlock, blockCursor);
    } 

    // Remove that block from free list.
    removeFreeBlock(freeBlock);

    // Count that block's size and update the current block pointer.
    newSize += size;
    blockCursor = freeBlock;
  }
  newBlock = blockCursor;

  // Coalesce with any following free block.
  // Start with the block following this one in memory
  blockCursor = (BlockInfo*)POINTER_ADD(oldBlock, oldSize);
  while ((blockCursor->sizeAndTags & TAG_USED)==0) {
    // While the block is free:

    size_t size = SIZE(blockCursor->sizeAndTags);
    // Remove it from the free list.
    removeFreeBlock(blockCursor);
    // Count its size and step to the following block.
    newSize += size;
    blockCursor = (BlockInfo*)POINTER_ADD(blockCursor, size);
  }

  // If the block actually grew, remove the old entry from the free
  // list and add the new entry.
  if (newSize != oldSize) {
    // Remove the original block from the free list
    removeFreeBlock(oldBlock);

    // Save the new size in the block info and in the boundary tag
    // and tag it to show the preceding block is used (otherwise, it
    // would have become part of this one!).
    newBlock->sizeAndTags = newSize | TAG_PRECEDING_USED;
    // The boundary tag of the preceding block is the word immediately
    // preceding block in memory where we left off advancing blockCursor.
    *(size_t*)POINTER_SUB(blockCursor, WORD_SIZE) = newSize | TAG_PRECEDING_USED;

    // Put the new block in the free list.
    insertFreeBlock(newBlock);
  }
  return;
}