2006-04-24 15:21:18 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License as
|
|
|
|
* published by the Free Software Foundation; either version 2 of the
|
|
|
|
* License, or any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
*/
|
|
|
|
|
2006-04-25 03:30:46 +00:00
|
|
|
#include <stddef.h>
|
2006-04-24 15:21:18 +00:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <string.h>
|
2006-04-25 03:30:46 +00:00
|
|
|
#include <strings.h>
|
2006-04-24 15:21:18 +00:00
|
|
|
#include <io.h>
|
|
|
|
#include <gpxe/list.h>
|
2006-04-25 03:30:46 +00:00
|
|
|
#include <malloc.h>
|
2006-04-24 15:21:18 +00:00
|
|
|
|
|
|
|
/** @file
|
|
|
|
*
|
2006-04-25 03:30:46 +00:00
|
|
|
* Dynamic memory allocation
|
2006-04-24 15:21:18 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/** A free block of memory */
|
2006-04-25 03:30:46 +00:00
|
|
|
struct memory_block {
|
2006-04-24 15:21:18 +00:00
|
|
|
/** List of free blocks */
|
|
|
|
struct list_head list;
|
|
|
|
/** Size of this block */
|
|
|
|
size_t size;
|
|
|
|
};
|
|
|
|
|
2006-04-25 03:30:46 +00:00
|
|
|
#define MIN_MEMBLOCK_SIZE \
|
|
|
|
( ( size_t ) ( 1 << ( fls ( sizeof ( struct memory_block ) - 1 ) ) ) )
|
|
|
|
|
|
|
|
/** A block of allocated memory complete with size information */
|
|
|
|
struct autosized_block {
|
|
|
|
/** Size of this block */
|
|
|
|
size_t size;
|
|
|
|
/** Remaining data */
|
|
|
|
char data[0];
|
|
|
|
};
|
|
|
|
|
2006-04-24 15:21:18 +00:00
|
|
|
/** List of free memory blocks */
|
|
|
|
static LIST_HEAD ( free_blocks );
|
|
|
|
|
|
|
|
/**
|
2006-04-25 03:30:46 +00:00
|
|
|
* Allocate a memory block
|
|
|
|
*
|
|
|
|
* @v size Requested size
|
|
|
|
* @v align Physical alignment
|
|
|
|
* @ret ptr Memory block, or NULL
|
2006-04-24 15:21:18 +00:00
|
|
|
*
|
2006-04-25 03:30:46 +00:00
|
|
|
* Allocates a memory block @b physically aligned as requested. No
|
|
|
|
* guarantees are provided for the alignment of the virtual address.
|
2006-04-24 15:21:18 +00:00
|
|
|
*
|
2006-04-25 03:30:46 +00:00
|
|
|
* @c align must be a power of two. @c size may not be zero.
|
2006-04-24 15:21:18 +00:00
|
|
|
*/
|
2006-04-25 03:30:46 +00:00
|
|
|
void * alloc_memblock ( size_t size, size_t align ) {
|
|
|
|
struct memory_block *block;
|
2006-04-25 21:48:16 +00:00
|
|
|
size_t align_mask;
|
2006-04-25 03:30:46 +00:00
|
|
|
size_t pre_size;
|
|
|
|
ssize_t post_size;
|
|
|
|
struct memory_block *pre;
|
|
|
|
struct memory_block *post;
|
|
|
|
|
2006-04-25 21:48:16 +00:00
|
|
|
/* Round up size to multiple of MIN_MEMBLOCK_SIZE and
|
|
|
|
* calculate alignment mask.
|
|
|
|
*/
|
2006-04-25 03:30:46 +00:00
|
|
|
size = ( size + MIN_MEMBLOCK_SIZE - 1 ) & ~( MIN_MEMBLOCK_SIZE - 1 );
|
2006-04-25 21:48:16 +00:00
|
|
|
align_mask = ( align - 1 ) | ( MIN_MEMBLOCK_SIZE - 1 );
|
2006-05-27 13:38:49 +00:00
|
|
|
DBG ( "Allocating %#zx (aligned %#zx)\n", size, align );
|
2006-04-25 03:30:46 +00:00
|
|
|
|
|
|
|
/* Search through blocks for the first one with enough space */
|
|
|
|
list_for_each_entry ( block, &free_blocks, list ) {
|
2006-04-25 21:48:16 +00:00
|
|
|
pre_size = ( - virt_to_phys ( block ) ) & align_mask;
|
2006-04-25 03:30:46 +00:00
|
|
|
post_size = block->size - pre_size - size;
|
|
|
|
if ( post_size >= 0 ) {
|
|
|
|
/* Split block into pre-block, block, and
|
|
|
|
* post-block. After this split, the "pre"
|
|
|
|
* block is the one currently linked into the
|
|
|
|
* free list.
|
|
|
|
*/
|
|
|
|
pre = block;
|
|
|
|
block = ( ( ( void * ) pre ) + pre_size );
|
|
|
|
post = ( ( ( void * ) block ) + size );
|
2006-05-27 13:38:49 +00:00
|
|
|
DBG ( "[%p,%p) -> [%p,%p) + [%p,%p)\n", pre,
|
2006-04-25 04:01:58 +00:00
|
|
|
( ( ( void * ) pre ) + pre->size ), pre, block,
|
|
|
|
post, ( ( ( void * ) pre ) + pre->size ) );
|
2006-04-25 03:30:46 +00:00
|
|
|
/* If there is a "post" block, add it in to
|
|
|
|
* the free list. Leak it if it is too small
|
|
|
|
* (which can happen only at the very end of
|
|
|
|
* the heap).
|
|
|
|
*/
|
2006-04-25 11:54:58 +00:00
|
|
|
if ( ( size_t ) post_size >= MIN_MEMBLOCK_SIZE ) {
|
2006-04-25 03:30:46 +00:00
|
|
|
post->size = post_size;
|
|
|
|
list_add ( &post->list, &pre->list );
|
|
|
|
}
|
|
|
|
/* Shrink "pre" block, leaving the main block
|
|
|
|
* isolated and no longer part of the free
|
|
|
|
* list.
|
|
|
|
*/
|
|
|
|
pre->size = pre_size;
|
|
|
|
/* If there is no "pre" block, remove it from
|
|
|
|
* the list. Also remove it (i.e. leak it) if
|
|
|
|
* it is too small, which can happen only at
|
|
|
|
* the very start of the heap.
|
|
|
|
*/
|
|
|
|
if ( pre_size < MIN_MEMBLOCK_SIZE )
|
|
|
|
list_del ( &pre->list );
|
|
|
|
/* Zero allocated memory, for calloc() */
|
|
|
|
memset ( block, 0, size );
|
2006-04-25 04:01:58 +00:00
|
|
|
DBG ( "Allocated [%p,%p)\n", block,
|
|
|
|
( ( ( void * ) block ) + size ) );
|
2006-04-25 03:30:46 +00:00
|
|
|
return block;
|
|
|
|
}
|
2006-04-24 15:21:18 +00:00
|
|
|
}
|
2006-04-28 14:07:41 +00:00
|
|
|
|
2006-05-27 13:38:49 +00:00
|
|
|
DBG ( "Failed to allocate %#zx (aligned %#zx)\n", size, align );
|
2006-04-25 03:30:46 +00:00
|
|
|
return NULL;
|
2006-04-24 15:21:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2006-04-25 03:30:46 +00:00
|
|
|
* Free a memory block
|
2006-04-24 15:21:18 +00:00
|
|
|
*
|
2006-04-25 03:30:46 +00:00
|
|
|
* @v ptr Memory allocated by alloc_memblock(), or NULL
|
|
|
|
* @v size Size of the memory
|
|
|
|
*
|
|
|
|
* If @c ptr is NULL, no action is taken.
|
2006-04-24 15:21:18 +00:00
|
|
|
*/
|
2006-04-25 03:30:46 +00:00
|
|
|
void free_memblock ( void *ptr, size_t size ) {
|
|
|
|
struct memory_block *freeing;
|
|
|
|
struct memory_block *block;
|
|
|
|
ssize_t gap_before;
|
2006-04-25 04:01:58 +00:00
|
|
|
ssize_t gap_after = -1;
|
2006-04-25 03:30:46 +00:00
|
|
|
|
|
|
|
/* Allow for ptr==NULL */
|
|
|
|
if ( ! ptr )
|
|
|
|
return;
|
2006-04-24 15:21:18 +00:00
|
|
|
|
2006-04-25 03:30:46 +00:00
|
|
|
/* Round up size to match actual size that alloc_memblock()
|
|
|
|
* would have used.
|
|
|
|
*/
|
|
|
|
size = ( size + MIN_MEMBLOCK_SIZE - 1 ) & ~( MIN_MEMBLOCK_SIZE - 1 );
|
|
|
|
freeing = ptr;
|
|
|
|
freeing->size = size;
|
2006-04-25 04:01:58 +00:00
|
|
|
DBG ( "Freeing [%p,%p)\n", freeing, ( ( ( void * ) freeing ) + size ));
|
2006-04-24 15:21:18 +00:00
|
|
|
|
2006-04-25 03:30:46 +00:00
|
|
|
/* Insert/merge into free list */
|
2006-04-24 15:21:18 +00:00
|
|
|
list_for_each_entry ( block, &free_blocks, list ) {
|
2006-04-25 03:30:46 +00:00
|
|
|
/* Calculate gaps before and after the "freeing" block */
|
|
|
|
gap_before = ( ( ( void * ) freeing ) -
|
|
|
|
( ( ( void * ) block ) + block->size ) );
|
|
|
|
gap_after = ( ( ( void * ) block ) -
|
|
|
|
( ( ( void * ) freeing ) + freeing->size ) );
|
|
|
|
/* Merge with immediately preceding block, if possible */
|
|
|
|
if ( gap_before == 0 ) {
|
2006-04-25 04:01:58 +00:00
|
|
|
DBG ( "[%p,%p) + [%p,%p) -> [%p,%p)\n", block,
|
|
|
|
( ( ( void * ) block ) + block->size ), freeing,
|
|
|
|
( ( ( void * ) freeing ) + freeing->size ),block,
|
|
|
|
( ( ( void * ) freeing ) + freeing->size ) );
|
2006-04-25 03:30:46 +00:00
|
|
|
block->size += size;
|
2006-04-24 15:21:18 +00:00
|
|
|
list_del ( &block->list );
|
2006-04-25 03:30:46 +00:00
|
|
|
freeing = block;
|
2006-04-24 15:21:18 +00:00
|
|
|
}
|
2006-04-25 04:01:58 +00:00
|
|
|
/* Stop processing as soon as we reach a following block */
|
|
|
|
if ( gap_after >= 0 )
|
2006-04-25 03:30:46 +00:00
|
|
|
break;
|
2006-04-25 04:01:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Insert before the immediately following block. If
|
|
|
|
* possible, merge the following block into the "freeing"
|
|
|
|
* block.
|
|
|
|
*/
|
|
|
|
DBG ( "[%p,%p)\n", freeing, ( ( ( void * ) freeing ) + freeing->size));
|
|
|
|
list_add_tail ( &freeing->list, &block->list );
|
|
|
|
if ( gap_after == 0 ) {
|
|
|
|
DBG ( "[%p,%p) + [%p,%p) -> [%p,%p)\n", freeing,
|
|
|
|
( ( ( void * ) freeing ) + freeing->size ), block,
|
|
|
|
( ( ( void * ) block ) + block->size ), freeing,
|
|
|
|
( ( ( void * ) block ) + block->size ) );
|
|
|
|
freeing->size += block->size;
|
|
|
|
list_del ( &block->list );
|
2006-04-24 15:21:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2006-04-25 03:30:46 +00:00
|
|
|
* Allocate memory
|
2006-04-24 15:21:18 +00:00
|
|
|
*
|
2006-04-25 03:30:46 +00:00
|
|
|
* @v size Requested size
|
|
|
|
* @ret ptr Memory, or NULL
|
2006-04-24 15:21:18 +00:00
|
|
|
*
|
2006-04-25 03:30:46 +00:00
|
|
|
* Allocates memory with no particular alignment requirement. @c ptr
|
|
|
|
* will be aligned to at least a multiple of sizeof(void*).
|
|
|
|
*/
|
|
|
|
void * malloc ( size_t size ) {
|
|
|
|
size_t total_size;
|
|
|
|
struct autosized_block *block;
|
|
|
|
|
|
|
|
total_size = size + offsetof ( struct autosized_block, data );
|
|
|
|
block = alloc_memblock ( total_size, 1 );
|
|
|
|
if ( ! block )
|
|
|
|
return NULL;
|
2006-04-25 04:01:58 +00:00
|
|
|
block->size = total_size;
|
2006-04-25 03:30:46 +00:00
|
|
|
return &block->data;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Free memory
|
2006-04-24 15:21:18 +00:00
|
|
|
*
|
2006-04-25 03:30:46 +00:00
|
|
|
* @v size Memory allocated by malloc(), or NULL
|
2006-04-24 15:21:18 +00:00
|
|
|
*
|
2006-04-25 03:30:46 +00:00
|
|
|
* Memory allocated with malloc_dma() cannot be freed with free(); it
|
|
|
|
* must be freed with free_dma() instead.
|
2006-04-24 15:21:18 +00:00
|
|
|
*
|
2006-04-25 03:30:46 +00:00
|
|
|
* If @c ptr is NULL, no action is taken.
|
2006-04-24 15:21:18 +00:00
|
|
|
*/
|
2006-04-25 03:30:46 +00:00
|
|
|
void free ( void *ptr ) {
|
|
|
|
struct autosized_block *block;
|
2006-04-24 15:21:18 +00:00
|
|
|
|
2006-04-25 03:30:46 +00:00
|
|
|
if ( ptr ) {
|
|
|
|
block = container_of ( ptr, struct autosized_block, data );
|
|
|
|
free_memblock ( block, block->size );
|
2006-04-24 15:21:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add memory to allocation pool
|
|
|
|
*
|
|
|
|
* @v start Start address
|
2006-04-25 03:30:46 +00:00
|
|
|
* @v end End address
|
2006-04-24 15:21:18 +00:00
|
|
|
*
|
2006-04-25 03:30:46 +00:00
|
|
|
* Adds a block of memory [start,end) to the allocation pool. This is
|
|
|
|
* a one-way operation; there is no way to reclaim this memory.
|
2006-04-24 15:21:18 +00:00
|
|
|
*
|
2006-04-25 03:30:46 +00:00
|
|
|
* @c start must be aligned to at least a multiple of sizeof(void*).
|
2006-04-24 15:21:18 +00:00
|
|
|
*/
|
2006-04-25 03:30:46 +00:00
|
|
|
void mpopulate ( void *start, size_t len ) {
|
2006-04-25 04:01:58 +00:00
|
|
|
/* Prevent free_memblock() from rounding up len beyond the end
|
|
|
|
* of what we were actually given...
|
|
|
|
*/
|
2006-04-25 03:30:46 +00:00
|
|
|
free_memblock ( start, ( len & ~( MIN_MEMBLOCK_SIZE - 1 ) ) );
|
2006-04-24 15:21:18 +00:00
|
|
|
}
|
|
|
|
|
2006-04-25 04:01:58 +00:00
|
|
|
#if 0
|
2006-04-24 15:21:18 +00:00
|
|
|
#include <vsprintf.h>
|
|
|
|
/**
|
|
|
|
* Dump free block list
|
|
|
|
*
|
|
|
|
*/
|
2006-04-25 03:30:46 +00:00
|
|
|
void mdumpfree ( void ) {
|
|
|
|
struct memory_block *block;
|
2006-04-24 15:21:18 +00:00
|
|
|
|
|
|
|
printf ( "Free block list:\n" );
|
|
|
|
list_for_each_entry ( block, &free_blocks, list ) {
|
2006-05-27 13:38:49 +00:00
|
|
|
printf ( "[%p,%p] (size %#zx)\n", block,
|
2006-04-24 15:21:18 +00:00
|
|
|
( ( ( void * ) block ) + block->size ), block->size );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|