/* Print the heap by iterating through it as an implicit free list. */
static void examine_heap() {
  BlockInfo *block;

  fprintf(stderr, "FREE_LIST_HEAD: %p\n", (void *)FREE_LIST_HEAD);

  for(block = (BlockInfo*)POINTER_ADD(mem_heap_lo(), WORD_SIZE); /* first block on heap */
      SIZE(block->sizeAndTags) != 0 && block < mem_heap_hi();
      block = (BlockInfo*)POINTER_ADD(block, SIZE(block->sizeAndTags))) {

    /* print out common block attributes */
    fprintf(stderr, "%p: %ld %ld %ld\t",
            (void*)block,
            SIZE(block->sizeAndTags),
            block->sizeAndTags & TAG_PRECEDING_USED,
            block->sizeAndTags & TAG_USED);

    /* and allocated/free specific data */
    if (block->sizeAndTags & TAG_USED) {
      fprintf(stderr, "ALLOCATED\n");
    } else {
      fprintf(stderr, "FREE\tnext: %p, prev: %p\n",
              (void*)block->next,
              (void*)block->prev);
    }
  }
  fprintf(stderr, "END OF HEAP\n\n");
}
Example #2
0
File: mm.c Project: 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;
}
/* 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);
  
}
Example #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);
}
Example #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);
}
Example #6
0
File: mm.c Project: 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);

}
Example #7
0
File: mm.c Project: YurieCo/hsi
/* Allocate a block of size size and return a pointer to it. */
void* mm_malloc (size_t size) 
{
  size_t reqSize;
  BlockInfo * ptrFreeBlock = NULL, * head;
  size_t blockSize;
  size_t precedingBlockUseTag;
  size_t extendsize; /* Amount to extend heap if no fit */

  // Zero-size requests get NULL.
  if (size == 0) {
    return NULL;
  }

  // Add one word for the initial size header.
  // Note that we don't need to boundary tag when the block is used!
  size += WORD_SIZE;
  if (size <= MIN_BLOCK_SIZE) {
    // Make sure we allocate enough space for a blockInfo in case we
    // free this block (when we free this block, we'll need to use the
    // next pointer, the prev pointer, and the boundary tag).
    reqSize = MIN_BLOCK_SIZE;
  } 
  else {
    // Round up for correct alignment
    reqSize = ALIGNMENT * ((size + ALIGNMENT - 1) / ALIGNMENT);
  }

  // Implement mm_malloc.  You can change or remove any of the above
  // code.  It is included as a suggestion of where to start.
  // You will want to replace this return statement...
  
  //head = FREE_LIST_HEAD;
  //printf("\nSo we were requested %d (%d). Free list head was 0x%x %d 0x%x 0x%x\n",
  //			 size, reqSize, head, head->sizeAndTags, head->next, head->prev );
  			 
  // half-arsed fix
	reqSize=reqSize+ALIGNMENT;
	
  if ( ptrFreeBlock = searchFreeList(reqSize) )
  {
  	precedingBlockUseTag = ptrFreeBlock->sizeAndTags & TAG_PRECEDING_USED;
  	placeBlock( ptrFreeBlock, reqSize, precedingBlockUseTag );  	
  }
  
  else
  {
  	//printf("But there wasn't big enough free block, we requsted extension %d\n", extendsize);
  	requestMoreSpace( reqSize );
  	ptrFreeBlock = searchFreeList(reqSize);
  	//printf("We got ourselves new pointer to free block after 0x%x %d 0x%x 0x%x\n", 
  	//				(int*)ptrFreeBlock, (int)ptrFreeBlock->sizeAndTags, (int*)ptrFreeBlock->next, (int*)ptrFreeBlock->prev);
  	precedingBlockUseTag = TAG_PRECEDING_USED;
  	placeBlock( ptrFreeBlock, reqSize, precedingBlockUseTag );
  }  
    
	return ((void*) POINTER_ADD(ptrFreeBlock, WORD_SIZE)); 
}
Example #8
0
/* 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 & TAG_PRECEDING_USED;
   *((size_t*)POINTER_ADD(firstFreeBlock, totalSize )) = 0x03;
   size_t tmp = *((size_t*)POINTER_ADD(firstFreeBlock, totalSize )) ;
  // set the head of the free list to this new free block.
  FREE_LIST_HEAD = firstFreeBlock;
  return 0;
}
Example #9
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);
}
Example #10
0
File: mm.c Project: YurieCo/hsi
/*	places block for malloc function into free block
		basically changing last byte to 1 in header, so that
		block is marked as not empty, if free Block is >  tha what we need to allocate,
		also split free bloc, etc.
		*/
static void * placeBlock(BlockInfo * freeBlock, size_t reqSize, size_t precedingBlockUseTag)
{
	size_t sizeFree;
	BlockInfo * restOfFreeBlock;
	
	sizeFree = (size_t)SIZE( freeBlock->sizeAndTags ); //gives size without last 3 indicator bits
	removeFreeBlock(freeBlock);
	
	if( (sizeFree - reqSize) > MIN_BLOCK_SIZE )
	{
		freeBlock->sizeAndTags = reqSize | precedingBlockUseTag;
		//printf("Ex-Free block after allocation 0x%x %d 0x%x 0x%x\n", 
		//			(int*)freeBlock, (int)freeBlock->sizeAndTags, (int*)freeBlock->next, (int*)freeBlock->prev);
		restOfFreeBlock = POINTER_ADD( freeBlock, reqSize );
		restOfFreeBlock->sizeAndTags = ((sizeFree-reqSize) | TAG_PRECEDING_USED) & (~TAG_USED);
		
		//Updates the boundary tag
		*((int*)POINTER_ADD(freeBlock, (reqSize-WORD_SIZE))) = reqSize;
		*((int*)POINTER_ADD(freeBlock, (sizeFree-WORD_SIZE))) = sizeFree-reqSize;
		
		insertFreeBlock(restOfFreeBlock);		
		//printf("Rest of free block after allocation 0x%x %d 0x%x 0x%x\n", 
		//		(int*)restOfFreeBlock, (int)restOfFreeBlock->sizeAndTags, (int*)restOfFreeBlock->next, (int*)restOfFreeBlock->prev);
	}
	else
	{
		//freeBlock->sizeAndTags = sizeFree | TAG_USED;
		// Updates the following blocks preceding used and the boundary tag
		*((int*)POINTER_ADD(freeBlock, (sizeFree-WORD_SIZE))) = sizeFree;
		restOfFreeBlock = (BlockInfo*)POINTER_ADD(freeBlock, sizeFree);
		restOfFreeBlock->sizeAndTags = restOfFreeBlock->sizeAndTags | TAG_PRECEDING_USED;		
	}
	
	freeBlock->sizeAndTags |= TAG_USED;
	freeBlock->sizeAndTags |= precedingBlockUseTag;
}
/* Allocate a block of size size and return a pointer to it. */
void* mm_malloc (size_t size) {
  size_t reqSize;
  BlockInfo * ptrFreeBlock = NULL;
  size_t blockSize;
  size_t precedingBlockUseTag;

  // Zero-size requests get NULL.
  if (size == 0) {
    return NULL;
  }

  // Add one word for the initial size header.
  // Note that we don't need to boundary tag when the block is used!
  size += WORD_SIZE;
  if (size <= MIN_BLOCK_SIZE) {
    // Make sure we allocate enough space for a blockInfo in case we
    // free this block (when we free this block, we'll need to use the
    // next pointer, the prev pointer, and the boundary tag).
    reqSize = MIN_BLOCK_SIZE;
  } else {
    // Round up for correct alignment
    reqSize = ALIGNMENT * ((size + ALIGNMENT - 1) / ALIGNMENT);
  }

  // Implement mm_malloc.  You can change or remove any of the above
  // code.  It is included as a suggestion of where to start.
  // You will want to replace this return statement...
 
  // Search the free list for a fit
  ptrFreeBlock = searchFreeList(reqSize);
 
  // No fit found. Get more memory
  if (ptrFreeBlock == NULL) {
    requestMoreSpace(reqSize);
    ptrFreeBlock = searchFreeList(reqSize);
  }

  // place the acquired block and split excessive part as needed
  removeFreeBlock(ptrFreeBlock);

  blockSize = SIZE(ptrFreeBlock->sizeAndTags);
  precedingBlockUseTag = ptrFreeBlock->sizeAndTags & TAG_PRECEDING_USED;

  if (blockSize - reqSize >= MIN_BLOCK_SIZE) {
    size_t newFreeBlockSize = blockSize - reqSize;

    BlockInfo *newPtrFreeBlock = (BlockInfo*)POINTER_ADD(ptrFreeBlock, reqSize);
    newPtrFreeBlock->sizeAndTags = newFreeBlockSize | TAG_PRECEDING_USED;	// !TAG_USED

    // update the boundary tag
    *((size_t*)POINTER_ADD(newPtrFreeBlock, newFreeBlockSize - WORD_SIZE)) = 
            newFreeBlockSize | TAG_PRECEDING_USED;	// !TAG_USED
    // insert the new free block into free list
    insertFreeBlock(newPtrFreeBlock);

    blockSize = reqSize;
  } else {
    // do not need to split the block, but need to update the status of following block
    BlockInfo *followingBlock = (BlockInfo*)POINTER_ADD(ptrFreeBlock, blockSize);
    size_t followingBlockSize = SIZE(followingBlock->sizeAndTags);
    size_t followingBlockUsed = followingBlock->sizeAndTags & TAG_USED;
    followingBlock->sizeAndTags = followingBlockSize | TAG_PRECEDING_USED | followingBlockUsed;
    if (followingBlockUsed != TAG_USED) {
      *((size_t*)POINTER_ADD(followingBlock, followingBlockSize - WORD_SIZE)) = followingBlock->sizeAndTags; 
    }
  }

  ptrFreeBlock->sizeAndTags = blockSize | precedingBlockUseTag | TAG_USED;
  // we do not care about the boundary tag of used block!

  return POINTER_ADD(ptrFreeBlock, WORD_SIZE); 
}
/* 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;
}
Example #13
0
void
acpi_rs_dump_resource_list (
	acpi_resource       *resource)
{
	u8                  count = 0;
	u8                  done = FALSE;


	FUNCTION_ENTRY ();


	if (acpi_dbg_level & ACPI_LV_RESOURCES && _COMPONENT & acpi_dbg_layer) {
		while (!done) {
			acpi_os_printf ("Resource structure %x.\n", count++);

			switch (resource->id) {
			case ACPI_RSTYPE_IRQ:
				acpi_rs_dump_irq (&resource->data);
				break;

			case ACPI_RSTYPE_DMA:
				acpi_rs_dump_dma (&resource->data);
				break;

			case ACPI_RSTYPE_START_DPF:
				acpi_rs_dump_start_dependent_functions (&resource->data);
				break;

			case ACPI_RSTYPE_END_DPF:
				acpi_os_printf ("End_dependent_functions Resource\n");
				/* Acpi_rs_dump_end_dependent_functions (Resource->Data);*/
				break;

			case ACPI_RSTYPE_IO:
				acpi_rs_dump_io (&resource->data);
				break;

			case ACPI_RSTYPE_FIXED_IO:
				acpi_rs_dump_fixed_io (&resource->data);
				break;

			case ACPI_RSTYPE_VENDOR:
				acpi_rs_dump_vendor_specific (&resource->data);
				break;

			case ACPI_RSTYPE_END_TAG:
				/*Rs_dump_end_tag (Resource->Data);*/
				acpi_os_printf ("End_tag Resource\n");
				done = TRUE;
				break;

			case ACPI_RSTYPE_MEM24:
				acpi_rs_dump_memory24 (&resource->data);
				break;

			case ACPI_RSTYPE_MEM32:
				acpi_rs_dump_memory32 (&resource->data);
				break;

			case ACPI_RSTYPE_FIXED_MEM32:
				acpi_rs_dump_fixed_memory32 (&resource->data);
				break;

			case ACPI_RSTYPE_ADDRESS16:
				acpi_rs_dump_address16 (&resource->data);
				break;

			case ACPI_RSTYPE_ADDRESS32:
				acpi_rs_dump_address32 (&resource->data);
				break;

			case ACPI_RSTYPE_ADDRESS64:
				acpi_rs_dump_address64 (&resource->data);
				break;

			case ACPI_RSTYPE_EXT_IRQ:
				acpi_rs_dump_extended_irq (&resource->data);
				break;

			default:
				acpi_os_printf ("Invalid resource type\n");
				break;

			}

			resource = POINTER_ADD (acpi_resource, resource, resource->length);
		}
	}

	return;
}
Example #14
0
acpi_status
acpi_rs_calculate_byte_stream_length (
	acpi_resource           *linked_list,
	u32                     *size_needed)
{
	u32                     byte_stream_size_needed = 0;
	u32                     segment_size;
	acpi_resource_ext_irq   *ex_irq = NULL;
	u8                      done = FALSE;


	FUNCTION_TRACE ("Rs_calculate_byte_stream_length");


	while (!done) {
		/*
		 * Init the variable that will hold the size to add to the total.
		 */
		segment_size = 0;

		switch (linked_list->id) {
		case ACPI_RSTYPE_IRQ:
			/*
			 * IRQ Resource
			 * For an IRQ Resource, Byte 3, although optional, will
			 * always be created - it holds IRQ information.
			 */
			segment_size = 4;
			break;

		case ACPI_RSTYPE_DMA:
			/*
			 * DMA Resource
			 * For this resource the size is static
			 */
			segment_size = 3;
			break;

		case ACPI_RSTYPE_START_DPF:
			/*
			 * Start Dependent Functions Resource
			 * For a Start_dependent_functions Resource, Byte 1,
			 * although optional, will always be created.
			 */
			segment_size = 2;
			break;

		case ACPI_RSTYPE_END_DPF:
			/*
			 * End Dependent Functions Resource
			 * For this resource the size is static
			 */
			segment_size = 1;
			break;

		case ACPI_RSTYPE_IO:
			/*
			 * IO Port Resource
			 * For this resource the size is static
			 */
			segment_size = 8;
			break;

		case ACPI_RSTYPE_FIXED_IO:
			/*
			 * Fixed IO Port Resource
			 * For this resource the size is static
			 */
			segment_size = 4;
			break;

		case ACPI_RSTYPE_VENDOR:
			/*
			 * Vendor Defined Resource
			 * For a Vendor Specific resource, if the Length is
			 * between 1 and 7 it will be created as a Small
			 * Resource data type, otherwise it is a Large
			 * Resource data type.
			 */
			if (linked_list->data.vendor_specific.length > 7) {
				segment_size = 3;
			}
			else {
				segment_size = 1;
			}
			segment_size += linked_list->data.vendor_specific.length;
			break;

		case ACPI_RSTYPE_END_TAG:
			/*
			 * End Tag
			 * For this resource the size is static
			 */
			segment_size = 2;
			done = TRUE;
			break;

		case ACPI_RSTYPE_MEM24:
			/*
			 * 24-Bit Memory Resource
			 * For this resource the size is static
			 */
			segment_size = 12;
			break;

		case ACPI_RSTYPE_MEM32:
			/*
			 * 32-Bit Memory Range Resource
			 * For this resource the size is static
			 */
			segment_size = 20;
			break;

		case ACPI_RSTYPE_FIXED_MEM32:
			/*
			 * 32-Bit Fixed Memory Resource
			 * For this resource the size is static
			 */
			segment_size = 12;
			break;

		case ACPI_RSTYPE_ADDRESS16:
			/*
			 * 16-Bit Address Resource
			 * The base size of this byte stream is 16. If a
			 * Resource Source string is not NULL, add 1 for
			 * the Index + the length of the null terminated
			 * string Resource Source + 1 for the null.
			 */
			segment_size = 16;

			if (NULL != linked_list->data.address16.resource_source.string_ptr) {
				segment_size += (1 +
					linked_list->data.address16.resource_source.string_length);
			}
			break;

		case ACPI_RSTYPE_ADDRESS32:
			/*
			 * 32-Bit Address Resource
			 * The base size of this byte stream is 26. If a Resource
			 * Source string is not NULL, add 1 for the Index + the
			 * length of the null terminated string Resource Source +
			 * 1 for the null.
			 */
			segment_size = 26;

			if (NULL != linked_list->data.address32.resource_source.string_ptr) {
				segment_size += (1 +
					linked_list->data.address32.resource_source.string_length);
			}
			break;

		case ACPI_RSTYPE_ADDRESS64:
			/*
			 * 64-Bit Address Resource
			 * The base size of this byte stream is 46. If a Resource
			 * Source string is not NULL, add 1 for the Index + the
			 * length of the null terminated string Resource Source +
			 * 1 for the null.
			 */
			segment_size = 46;

			if (NULL != linked_list->data.address64.resource_source.string_ptr) {
				segment_size += (1 +
					linked_list->data.address64.resource_source.string_length);
			}
			break;

		case ACPI_RSTYPE_EXT_IRQ:
			/*
			 * Extended IRQ Resource
			 * The base size of this byte stream is 9. This is for an
			 * Interrupt table length of 1.  For each additional
			 * interrupt, add 4.
			 * If a Resource Source string is not NULL, add 1 for the
			 * Index + the length of the null terminated string
			 * Resource Source + 1 for the null.
			 */
			segment_size = 9 +
				((linked_list->data.extended_irq.number_of_interrupts - 1) * 4);

			if (NULL != ex_irq->resource_source.string_ptr) {
				segment_size += (1 +
					linked_list->data.extended_irq.resource_source.string_length);
			}
			break;

		default:
			/*
			 * If we get here, everything is out of sync,
			 * so exit with an error
			 */
			return_ACPI_STATUS (AE_AML_INVALID_RESOURCE_TYPE);
			break;

		} /* switch (Linked_list->Id) */

		/*
		 * Update the total
		 */
		byte_stream_size_needed += segment_size;

		/*
		 * Point to the next object
		 */
		linked_list = POINTER_ADD (acpi_resource,
				  linked_list, linked_list->length);
	}

	/*
	 * This is the data the caller needs
	 */
	*size_needed = byte_stream_size_needed;
	return_ACPI_STATUS (AE_OK);
}
Example #15
0
/* Allocate a block of size size and return a pointer to it. */
void* mm_malloc (size_t size) {
  size_t reqSize;
  BlockInfo * ptrFreeBlock = NULL;
  size_t blockSize;
  size_t precedingBlockUseTag;
  BlockInfo * nextBlock = NULL;
  BlockInfo * newBlock = NULL;	
  // Zero-size requests get NULL.
  if (size == 0) {
    return NULL;
  }

  // Add one word for the initial size header.
  // Note that we don't need to boundary tag when the block is used!
  size += WORD_SIZE;
  if (size <= MIN_BLOCK_SIZE) {
    // Make sure we allocate enough space for a blockInfo in case we
    // free this block (when we free this block, we'll need to use the
    // next pointer, the prev pointer, and the boundary tag).
    reqSize = MIN_BLOCK_SIZE;
  } else {
    // Round up for correct alignment
    reqSize = ALIGNMENT * ((size + ALIGNMENT - 1) / ALIGNMENT);
  }

  // Implement mm_malloc.  You can change or remove any of the above
  // code.  It is included as a suggestion of where to start.
  // You will want to replace this return statement...
/*
  ptrFreeBlock = searchFreeList(reqSize);
  if( ptrFreeBlock != NULL) {
	  blockSize = SIZE(ptrFreeBlock->sizeAndTags);
	  nextBlock = (BlockInfo* )POINTER_ADD(ptrFreeBlock, blockSize);
	  nextBlock->sizeAndTags = nextBlock->sizeAndTags | TAG_PRECEDING_USED;
	  removeFreeBlock(ptrFreeBlock);
	  ptrFreeBlock->sizeAndTags =   ptrFreeBlock->sizeAndTags | TAG_USED;
	  return POINTER_ADD( ptrFreeBlock, WORD_SIZE);
  }
  
  requestMoreSpace(reqSize);
  ptrFreeBlock = searchFreeList(reqSize);
  if( ptrFreeBlock != NULL) {
	   blockSize = SIZE(ptrFreeBlock->sizeAndTags);
	  nextBlock = (BlockInfo* )POINTER_ADD(ptrFreeBlock, blockSize);
	  nextBlock->sizeAndTags = nextBlock->sizeAndTags | TAG_PRECEDING_USED;
	  removeFreeBlock(ptrFreeBlock);
	  ptrFreeBlock->sizeAndTags =   ptrFreeBlock->sizeAndTags | TAG_USED; 
	  return POINTER_ADD( ptrFreeBlock, WORD_SIZE);
  }
*/
 ptrFreeBlock = searchFreeList(reqSize);
 if( ptrFreeBlock == NULL){
	 requestMoreSpace(reqSize);
     ptrFreeBlock = searchFreeList(reqSize);
 }
 
 blockSize = SIZE(ptrFreeBlock->sizeAndTags);
 
 if( (blockSize - reqSize) >= MIN_BLOCK_SIZE ){
	precedingBlockUseTag = ptrFreeBlock->sizeAndTags & TAG_PRECEDING_USED;
	ptrFreeBlock->sizeAndTags = reqSize | precedingBlockUseTag | TAG_USED ;
    removeFreeBlock(ptrFreeBlock);
    
    newBlock = (BlockInfo* )POINTER_ADD(ptrFreeBlock, reqSize);
    newBlock->sizeAndTags = blockSize - reqSize;
    newBlock->sizeAndTags = newBlock->sizeAndTags | TAG_PRECEDING_USED;
    *((size_t*)POINTER_ADD(newBlock, blockSize - reqSize - WORD_SIZE ) ) = newBlock->sizeAndTags;

	insertFreeBlock(newBlock);
 }else{
    nextBlock = (BlockInfo* )POINTER_ADD(ptrFreeBlock, blockSize);
    nextBlock->sizeAndTags = nextBlock->sizeAndTags | TAG_PRECEDING_USED;
   
    ptrFreeBlock->sizeAndTags =   ptrFreeBlock->sizeAndTags | TAG_USED;
    removeFreeBlock(ptrFreeBlock);  
 }
 return POINTER_ADD( ptrFreeBlock, WORD_SIZE);
}
Example #16
0
/* Allocate a block of size size and return a pointer to it. */
void* mm_malloc (size_t size) {
  size_t reqSize;
  BlockInfo * ptrFreeBlock = NULL;
  BlockInfo * splitBlock;
  size_t blockSize;
  size_t precedingBlockUseTag;

  // Zero-size requests get NULL.
  if (size == 0) {
    return NULL;
  }

  // Add one word for the initial size header.
  // Note that we don't need to boundary tag when the block is used!
  size += WORD_SIZE;
  if (size <= MIN_BLOCK_SIZE) {
    // Make sure we allocate enough space for a blockInfo in case we
    // free this block (when we free this block, we'll need to use the
    // next pointer, the prev pointer, and the boundary tag).
    reqSize = MIN_BLOCK_SIZE;
  } else {
    // Round up for correct alignment
    reqSize = ALIGNMENT * ((size + ALIGNMENT - 1) / ALIGNMENT);
  }

  // Implement mm_malloc.  You can change or remove any of the above
  // code.  It is included as a suggestion of where to start.
  // You will want to replace this return statement...

  // find free block of reqSize
  ptrFreeBlock = searchFreeList(reqSize);
  while ( ptrFreeBlock == NULL ) {
    requestMoreSpace(1 << 14);
    ptrFreeBlock = searchFreeList(reqSize);
  }
  // check free block size vs reqSize vs alignment requirements,
  // split if necessary. If split, reformat newly created free
  // block (add header, set bits, add footer).
  blockSize = SIZE(ptrFreeBlock->sizeAndTags);
  if ( blockSize - reqSize >= MIN_BLOCK_SIZE ) {
    // Split, set size and tags of new block
    splitBlock = (BlockInfo*) POINTER_ADD(ptrFreeBlock, reqSize);
    blockSize -= reqSize;
    splitBlock->sizeAndTags = blockSize | TAG_PRECEDING_USED;
    splitBlock->sizeAndTags &= ~0 << 1; // turn off use bit - preserve others
    
    // set footer equal to header. Current ptr + blockSize - one word = boundary
    // tag of current block (word prior to start of next block)
    *((size_t*) POINTER_ADD(splitBlock, blockSize- WORD_SIZE))= 
      splitBlock->sizeAndTags;
    insertFreeBlock(splitBlock);
    // set size of ptrFreeBlock to exclude size of newBlock
    blockSize = reqSize;
  } else { // if we didnt split, set the next block's preceding tag to 1
    *((size_t*) POINTER_ADD(ptrFreeBlock, SIZE(ptrFreeBlock->sizeAndTags))) |= 
			   TAG_PRECEDING_USED;
  }
  removeFreeBlock(ptrFreeBlock);

  precedingBlockUseTag = ptrFreeBlock->sizeAndTags & TAG_PRECEDING_USED;
  // if the preceding block is used as well, set lower two bits. Else, set only
  // used bit.
  if ( precedingBlockUseTag ) {
    ptrFreeBlock->sizeAndTags = blockSize | (TAG_PRECEDING_USED + TAG_USED);
  } else {
    ptrFreeBlock->sizeAndTags = blockSize | TAG_USED;
  }

  // return pointer to block (beginning of payload)
  // if null, return null. else return 8 after start of block (ie skip header,
  // return ptr to payload region)
  if ( ptrFreeBlock == NULL) { return NULL; }
  else { return (void*) POINTER_ADD(ptrFreeBlock, WORD_SIZE); }
 }