-/*********************************************************************
-**********************************************************************
-***** PACKAGE-INTERNAL FUNCTIONS BELOW HERE *****
-**********************************************************************
-*********************************************************************/
-
-
-
-/*********************************************************************
-* malloc_create_region()
-*
-* Package-internal function. VM-allocates a new region and adds it to
-* the given region list.
-*********************************************************************/
-__private_extern__
-malloc_region * malloc_create_region(vm_size_t block_size) {
-
- malloc_region * new_region;
- vm_address_t vm_address;
- vm_size_t region_size;
- kern_return_t kern_result;
-
-
- /* Figure out how big the region needs to be and allocate it.
- */
- region_size = block_size + sizeof(malloc_region);
- region_size = round_page(region_size);
-
- kern_result = kmem_alloc(kernel_map,
- &vm_address, region_size);
-
- if (kern_result != KERN_SUCCESS) {
- // printf("kmem_alloc() failed in malloc_create_region()\n");
- return NULL;
- // panic();
- }
-
-
- /* Cast the allocated pointer to a region header.
- */
- new_region = (malloc_region *)vm_address;
-
-
- /* Initialize the region header fields and link it onto
- * the previous region.
- */
- new_region->region_size = region_size;
- queue_init(&new_region->block_list);
-// queue_init(&new_region->free_list);
-
- new_region->free_size = region_size - sizeof(malloc_region);
- new_region->free_address = &new_region->buffer;
-
- queue_enter(&malloc_region_list, new_region, malloc_region *, links);
-
- /* If debugging, add the new region's size to the total.
- */
-#ifdef CLIENT_DEBUG
- malloc_hiwater_mark += region_size;
- num_regions++;
-#endif /* CLIENT_DEBUG */
-
- return new_region;
-
-} /* malloc_create_region() */
-
-
-/*********************************************************************
-* malloc_free_region()
-*
-* Package-internal function. VM-deallocates the given region.
-*********************************************************************/
-__private_extern__
-kern_return_t malloc_free_region(malloc_region * region) {
-
- kmem_free(kernel_map,
- (vm_address_t)region,
- region->region_size);
-
-#ifdef CLIENT_DEBUG
- num_regions--;
-#endif /* CLIENT_DEBUG */
- return KERN_SUCCESS;
-
-} /* malloc_free_region() */
-
-
-/*********************************************************************
-* malloc_create_block_in_region()
-*
-* Package-internal function. Allocates a new block out of the given
-* region. The requested size must include the block header. If the
-* size requested is larger than the region's free size, returns NULL.
-*********************************************************************/
-__private_extern__
-malloc_block * malloc_create_block_in_region(
- malloc_region * region,
- size_t block_size) {
-
- malloc_block * new_block = NULL;
-
-
- /* Sanity checking.
- */
- if (block_size > region->free_size) {
- return NULL;
- // FIXME: panic?
- }
-
-
- /* Carve out a new block.
- */
- new_block = (malloc_block *)region->free_address;
- region->free_address = (char *)region->free_address + block_size;
- region->free_size -= block_size;
-
- memset(new_block, 0, sizeof(malloc_block));
-
- new_block->region = region;
- new_block->block_size = block_size;
-
- /* Record the new block as the last one in the region.
- */
- queue_enter(®ion->block_list, new_block, malloc_block *, links);
-
- return new_block;
-
-} /* malloc_create_block_in_region() */
-
-
-/*********************************************************************
-* malloc_find_block()
-*
-* Package-internal function. Given a client buffer address, find the
-* malloc_block for it.
-*********************************************************************/
-__private_extern__
-void malloc_find_block(void * address,
- malloc_block ** block,
- malloc_region ** region) {
-
- malloc_region * cur_region;
-
- *block = NULL;
- *region = NULL;
-
- queue_iterate(&malloc_region_list, cur_region, malloc_region *, links) {
-
- malloc_block * cur_block;
-
- queue_iterate(&cur_region->block_list, cur_block, malloc_block *, links) {
- if (cur_block->buffer == address) {
- *block = cur_block;
- *region = cur_region;
- return;
- }
- }
- }
-
- return;
-
-} /* malloc_find_block() */
-
-
-/*********************************************************************
-* malloc_get_free_block()
-*********************************************************************/
-__private_extern__
-void malloc_get_free_block(
- size_t size,
- malloc_block ** block,
- malloc_region ** region) {
-
- malloc_block * cur_block;
- size_t fit_threshold = 512;
-
- *block = NULL;
- *region = NULL;
-
- queue_iterate(&sorted_free_block_list, cur_block, malloc_block *, links) {
-
- /* If we find a block large enough, but not too large to waste memory,
- * pull it out and return it, along with its region.
- */
- if (cur_block->block_size >= size &&
- cur_block->block_size < (size + fit_threshold)) {
-
- queue_remove(&sorted_free_block_list, cur_block, malloc_block *, links);
- *block = cur_block;
- *region = cur_block->region;
- return;
- }
- }
- return;
-}