[malloc] Allow for the existence of multiple heaps

Create a generic model of a heap as a list of free blocks with
optional methods for growing and shrinking the heap.

Signed-off-by: Michael Brown <mcb30@ipxe.org>
This commit is contained in:
Michael Brown
2025-05-19 12:01:58 +01:00
parent 83449702e0
commit c6ca3d3af8
3 changed files with 219 additions and 156 deletions

View File

@@ -71,18 +71,6 @@ struct autosized_block {
char data[0]; char data[0];
}; };
/** List of free memory blocks */
static LIST_HEAD ( free_blocks );
/** Total amount of free memory */
size_t freemem;
/** Total amount of used memory */
size_t usedmem;
/** Maximum amount of used memory */
size_t maxusedmem;
/** /**
* Heap size * Heap size
* *
@@ -90,14 +78,15 @@ size_t maxusedmem;
*/ */
#define HEAP_SIZE ( 512 * 1024 ) #define HEAP_SIZE ( 512 * 1024 )
/** The heap itself */ /** The heap area */
static char heap[HEAP_SIZE]; static char heap_area[HEAP_SIZE];
/** /**
* Mark all blocks in free list as defined * Mark all blocks in free list as defined
* *
* @v heap Heap
*/ */
static inline void valgrind_make_blocks_defined ( void ) { static inline void valgrind_make_blocks_defined ( struct heap *heap ) {
struct memory_block *block; struct memory_block *block;
/* Do nothing unless running under Valgrind */ /* Do nothing unless running under Valgrind */
@@ -110,18 +99,18 @@ static inline void valgrind_make_blocks_defined ( void ) {
*/ */
/* Mark block list itself as defined */ /* Mark block list itself as defined */
VALGRIND_MAKE_MEM_DEFINED ( &free_blocks, sizeof ( free_blocks ) ); VALGRIND_MAKE_MEM_DEFINED ( &heap->blocks, sizeof ( heap->blocks ) );
/* Mark areas accessed by list_check() as defined */ /* Mark areas accessed by list_check() as defined */
VALGRIND_MAKE_MEM_DEFINED ( &free_blocks.prev->next, VALGRIND_MAKE_MEM_DEFINED ( &heap->blocks.prev->next,
sizeof ( free_blocks.prev->next ) ); sizeof ( heap->blocks.prev->next ) );
VALGRIND_MAKE_MEM_DEFINED ( free_blocks.next, VALGRIND_MAKE_MEM_DEFINED ( heap->blocks.next,
sizeof ( *free_blocks.next ) ); sizeof ( *heap->blocks.next ) );
VALGRIND_MAKE_MEM_DEFINED ( &free_blocks.next->next->prev, VALGRIND_MAKE_MEM_DEFINED ( &heap->blocks.next->next->prev,
sizeof ( free_blocks.next->next->prev ) ); sizeof ( heap->blocks.next->next->prev ) );
/* Mark each block in list as defined */ /* Mark each block in list as defined */
list_for_each_entry ( block, &free_blocks, list ) { list_for_each_entry ( block, &heap->blocks, list ) {
/* Mark block as defined */ /* Mark block as defined */
VALGRIND_MAKE_MEM_DEFINED ( block, sizeof ( *block ) ); VALGRIND_MAKE_MEM_DEFINED ( block, sizeof ( *block ) );
@@ -137,8 +126,9 @@ static inline void valgrind_make_blocks_defined ( void ) {
/** /**
* Mark all blocks in free list as inaccessible * Mark all blocks in free list as inaccessible
* *
* @v heap Heap
*/ */
static inline void valgrind_make_blocks_noaccess ( void ) { static inline void valgrind_make_blocks_noaccess ( struct heap *heap ) {
struct memory_block *block; struct memory_block *block;
struct memory_block *prev = NULL; struct memory_block *prev = NULL;
@@ -152,7 +142,7 @@ static inline void valgrind_make_blocks_noaccess ( void ) {
*/ */
/* Mark each block in list as inaccessible */ /* Mark each block in list as inaccessible */
list_for_each_entry ( block, &free_blocks, list ) { list_for_each_entry ( block, &heap->blocks, list ) {
/* Mark previous block (if any) as inaccessible. (Current /* Mark previous block (if any) as inaccessible. (Current
* block will be accessed by list_check().) * block will be accessed by list_check().)
@@ -165,8 +155,8 @@ static inline void valgrind_make_blocks_noaccess ( void ) {
* accessing the first list item. Temporarily mark * accessing the first list item. Temporarily mark
* this area as defined. * this area as defined.
*/ */
VALGRIND_MAKE_MEM_DEFINED ( &free_blocks.next->prev, VALGRIND_MAKE_MEM_DEFINED ( &heap->blocks.next->prev,
sizeof ( free_blocks.next->prev ) ); sizeof ( heap->blocks.next->prev ));
} }
/* Mark last block (if any) as inaccessible */ /* Mark last block (if any) as inaccessible */
if ( prev ) if ( prev )
@@ -175,25 +165,26 @@ static inline void valgrind_make_blocks_noaccess ( void ) {
/* Mark as inaccessible the area that was temporarily marked /* Mark as inaccessible the area that was temporarily marked
* as defined to avoid errors from list_check(). * as defined to avoid errors from list_check().
*/ */
VALGRIND_MAKE_MEM_NOACCESS ( &free_blocks.next->prev, VALGRIND_MAKE_MEM_NOACCESS ( &heap->blocks.next->prev,
sizeof ( free_blocks.next->prev ) ); sizeof ( heap->blocks.next->prev ) );
/* Mark block list itself as inaccessible */ /* Mark block list itself as inaccessible */
VALGRIND_MAKE_MEM_NOACCESS ( &free_blocks, sizeof ( free_blocks ) ); VALGRIND_MAKE_MEM_NOACCESS ( &heap->blocks, sizeof ( heap->blocks ) );
} }
/** /**
* Check integrity of the blocks in the free list * Check integrity of the blocks in the free list
* *
* @v heap Heap
*/ */
static inline void check_blocks ( void ) { static inline void check_blocks ( struct heap *heap ) {
struct memory_block *block; struct memory_block *block;
struct memory_block *prev = NULL; struct memory_block *prev = NULL;
if ( ! ASSERTING ) if ( ! ASSERTING )
return; return;
list_for_each_entry ( block, &free_blocks, list ) { list_for_each_entry ( block, &heap->blocks, list ) {
/* Check alignment */ /* Check alignment */
assert ( ( virt_to_phys ( block ) & assert ( ( virt_to_phys ( block ) &
@@ -225,9 +216,10 @@ static inline void check_blocks ( void ) {
/** /**
* Discard some cached data * Discard some cached data
* *
* @v size Failed allocation size
* @ret discarded Number of cached items discarded * @ret discarded Number of cached items discarded
*/ */
static unsigned int discard_cache ( void ) { static unsigned int discard_cache ( size_t size __unused ) {
struct cache_discarder *discarder; struct cache_discarder *discarder;
unsigned int discarded; unsigned int discarded;
@@ -247,13 +239,14 @@ static void discard_all_cache ( void ) {
unsigned int discarded; unsigned int discarded;
do { do {
discarded = discard_cache(); discarded = discard_cache ( 0 );
} while ( discarded ); } while ( discarded );
} }
/** /**
* Allocate a memory block * Allocate a memory block
* *
* @v heap Heap
* @v size Requested size * @v size Requested size
* @v align Physical alignment * @v align Physical alignment
* @v offset Offset from physical alignment * @v offset Offset from physical alignment
@@ -264,7 +257,8 @@ static void discard_all_cache ( void ) {
* *
* @c align must be a power of two. @c size may not be zero. * @c align must be a power of two. @c size may not be zero.
*/ */
void * alloc_memblock ( size_t size, size_t align, size_t offset ) { static void * heap_alloc_block ( struct heap *heap, size_t size, size_t align,
size_t offset ) {
struct memory_block *block; struct memory_block *block;
size_t actual_offset; size_t actual_offset;
size_t align_mask; size_t align_mask;
@@ -273,14 +267,14 @@ void * alloc_memblock ( size_t size, size_t align, size_t offset ) {
size_t post_size; size_t post_size;
struct memory_block *pre; struct memory_block *pre;
struct memory_block *post; struct memory_block *post;
unsigned int discarded; unsigned int grown;
void *ptr; void *ptr;
/* Sanity checks */ /* Sanity checks */
assert ( size != 0 ); assert ( size != 0 );
assert ( ( align == 0 ) || ( ( align & ( align - 1 ) ) == 0 ) ); assert ( ( align == 0 ) || ( ( align & ( align - 1 ) ) == 0 ) );
valgrind_make_blocks_defined(); valgrind_make_blocks_defined ( heap );
check_blocks(); check_blocks ( heap );
/* Calculate offset of memory block */ /* Calculate offset of memory block */
actual_offset = ( offset & ~( MEMBLOCK_ALIGN - 1 ) ); actual_offset = ( offset & ~( MEMBLOCK_ALIGN - 1 ) );
@@ -303,11 +297,11 @@ void * alloc_memblock ( size_t size, size_t align, size_t offset ) {
/* Calculate alignment mask */ /* Calculate alignment mask */
align_mask = ( ( align - 1 ) | ( MEMBLOCK_ALIGN - 1 ) ); align_mask = ( ( align - 1 ) | ( MEMBLOCK_ALIGN - 1 ) );
DBGC2 ( &heap, "HEAP allocating %#zx (aligned %#zx+%zx)\n", DBGC2 ( heap, "HEAP allocating %#zx (aligned %#zx+%#zx)\n",
size, align, offset ); size, align, offset );
while ( 1 ) { while ( 1 ) {
/* Search through blocks for the first one with enough space */ /* Search through blocks for the first one with enough space */
list_for_each_entry ( block, &free_blocks, list ) { list_for_each_entry ( block, &heap->blocks, list ) {
pre_size = ( ( actual_offset - virt_to_phys ( block ) ) pre_size = ( ( actual_offset - virt_to_phys ( block ) )
& align_mask ); & align_mask );
if ( ( block->size < pre_size ) || if ( ( block->size < pre_size ) ||
@@ -322,7 +316,7 @@ void * alloc_memblock ( size_t size, size_t align, size_t offset ) {
pre = block; pre = block;
block = ( ( ( void * ) pre ) + pre_size ); block = ( ( ( void * ) pre ) + pre_size );
post = ( ( ( void * ) block ) + actual_size ); post = ( ( ( void * ) block ) + actual_size );
DBGC2 ( &heap, "HEAP splitting [%p,%p) -> [%p,%p) " DBGC2 ( heap, "HEAP splitting [%p,%p) -> [%p,%p) "
"+ [%p,%p)\n", pre, "+ [%p,%p)\n", pre,
( ( ( void * ) pre ) + pre->size ), pre, block, ( ( ( void * ) pre ) + pre->size ), pre, block,
post, ( ( ( void * ) pre ) + pre->size ) ); post, ( ( ( void * ) pre ) + pre->size ) );
@@ -352,30 +346,30 @@ void * alloc_memblock ( size_t size, size_t align, size_t offset ) {
assert ( pre_size >= MEMBLOCK_ALIGN ); assert ( pre_size >= MEMBLOCK_ALIGN );
} }
/* Update memory usage statistics */ /* Update memory usage statistics */
freemem -= actual_size; heap->freemem -= actual_size;
usedmem += actual_size; heap->usedmem += actual_size;
if ( usedmem > maxusedmem ) if ( heap->usedmem > heap->maxusedmem )
maxusedmem = usedmem; heap->maxusedmem = heap->usedmem;
/* Return allocated block */ /* Return allocated block */
ptr = ( ( ( void * ) block ) + offset - actual_offset ); ptr = ( ( ( void * ) block ) + offset - actual_offset );
DBGC2 ( &heap, "HEAP allocated [%p,%p) within " DBGC2 ( heap, "HEAP allocated [%p,%p) within "
"[%p,%p)\n", ptr, ( ptr + size ), block, "[%p,%p)\n", ptr, ( ptr + size ), block,
( ( ( void * ) block ) + actual_size ) ); ( ( ( void * ) block ) + actual_size ) );
VALGRIND_MAKE_MEM_UNDEFINED ( ptr, size ); VALGRIND_MAKE_MEM_UNDEFINED ( ptr, size );
goto done; goto done;
} }
/* Try discarding some cached data to free up memory */ /* Attempt to grow heap to satisfy allocation */
DBGC ( &heap, "HEAP attempting discard for %#zx (aligned " DBGC ( heap, "HEAP attempting to grow for %#zx (aligned "
"%#zx+%zx), used %zdkB\n", size, align, offset, "%#zx+%zx), used %zdkB\n", size, align, offset,
( usedmem >> 10 ) ); ( heap->usedmem >> 10 ) );
valgrind_make_blocks_noaccess(); valgrind_make_blocks_noaccess ( heap );
discarded = discard_cache(); grown = ( heap->grow ? heap->grow ( actual_size ) : 0 );
valgrind_make_blocks_defined(); valgrind_make_blocks_defined ( heap );
check_blocks(); check_blocks ( heap );
if ( ! discarded ) { if ( ! grown ) {
/* Nothing available to discard */ /* Heap did not grow: fail allocation */
DBGC ( &heap, "HEAP failed to allocate %#zx (aligned " DBGC ( heap, "HEAP failed to allocate %#zx (aligned "
"%#zx)\n", size, align ); "%#zx)\n", size, align );
ptr = NULL; ptr = NULL;
goto done; goto done;
@@ -383,20 +377,21 @@ void * alloc_memblock ( size_t size, size_t align, size_t offset ) {
} }
done: done:
check_blocks(); check_blocks ( heap );
valgrind_make_blocks_noaccess(); valgrind_make_blocks_noaccess ( heap );
return ptr; return ptr;
} }
/** /**
* Free a memory block * Free a memory block
* *
* @v ptr Memory allocated by alloc_memblock(), or NULL * @v heap Heap
* @v ptr Memory allocated by heap_alloc_block(), or NULL
* @v size Size of the memory * @v size Size of the memory
* *
* If @c ptr is NULL, no action is taken. * If @c ptr is NULL, no action is taken.
*/ */
void free_memblock ( void *ptr, size_t size ) { static void heap_free_block ( struct heap *heap, void *ptr, size_t size ) {
struct memory_block *freeing; struct memory_block *freeing;
struct memory_block *block; struct memory_block *block;
struct memory_block *tmp; struct memory_block *tmp;
@@ -411,10 +406,10 @@ void free_memblock ( void *ptr, size_t size ) {
VALGRIND_MAKE_MEM_NOACCESS ( ptr, size ); VALGRIND_MAKE_MEM_NOACCESS ( ptr, size );
/* Sanity checks */ /* Sanity checks */
valgrind_make_blocks_defined(); valgrind_make_blocks_defined ( heap );
check_blocks(); check_blocks ( heap );
/* Round up to match actual block that alloc_memblock() would /* Round up to match actual block that heap_alloc_block() would
* have allocated. * have allocated.
*/ */
assert ( size != 0 ); assert ( size != 0 );
@@ -422,20 +417,20 @@ void free_memblock ( void *ptr, size_t size ) {
freeing = ( ptr - sub_offset ); freeing = ( ptr - sub_offset );
actual_size = ( ( size + sub_offset + MEMBLOCK_ALIGN - 1 ) & actual_size = ( ( size + sub_offset + MEMBLOCK_ALIGN - 1 ) &
~( MEMBLOCK_ALIGN - 1 ) ); ~( MEMBLOCK_ALIGN - 1 ) );
DBGC2 ( &heap, "HEAP freeing [%p,%p) within [%p,%p)\n", DBGC2 ( heap, "HEAP freeing [%p,%p) within [%p,%p)\n",
ptr, ( ptr + size ), freeing, ptr, ( ptr + size ), freeing,
( ( ( void * ) freeing ) + actual_size ) ); ( ( ( void * ) freeing ) + actual_size ) );
VALGRIND_MAKE_MEM_UNDEFINED ( freeing, sizeof ( *freeing ) ); VALGRIND_MAKE_MEM_UNDEFINED ( freeing, sizeof ( *freeing ) );
/* Check that this block does not overlap the free list */ /* Check that this block does not overlap the free list */
if ( ASSERTING ) { if ( ASSERTING ) {
list_for_each_entry ( block, &free_blocks, list ) { list_for_each_entry ( block, &heap->blocks, list ) {
if ( ( ( ( void * ) block ) < if ( ( ( ( void * ) block ) <
( ( void * ) freeing + actual_size ) ) && ( ( void * ) freeing + actual_size ) ) &&
( ( void * ) freeing < ( ( void * ) freeing <
( ( void * ) block + block->size ) ) ) { ( ( void * ) block + block->size ) ) ) {
assert ( 0 ); assert ( 0 );
DBGC ( &heap, "HEAP double free of [%p,%p) " DBGC ( heap, "HEAP double free of [%p,%p) "
"overlapping [%p,%p) detected from %p\n", "overlapping [%p,%p) detected from %p\n",
freeing, freeing,
( ( ( void * ) freeing ) + size ), block, ( ( ( void * ) freeing ) + size ), block,
@@ -447,7 +442,7 @@ void free_memblock ( void *ptr, size_t size ) {
/* Insert/merge into free list */ /* Insert/merge into free list */
freeing->size = actual_size; freeing->size = actual_size;
list_for_each_entry_safe ( block, tmp, &free_blocks, list ) { list_for_each_entry_safe ( block, tmp, &heap->blocks, list ) {
/* Calculate gaps before and after the "freeing" block */ /* Calculate gaps before and after the "freeing" block */
gap_before = ( ( ( void * ) freeing ) - gap_before = ( ( ( void * ) freeing ) -
( ( ( void * ) block ) + block->size ) ); ( ( ( void * ) block ) + block->size ) );
@@ -455,7 +450,7 @@ void free_memblock ( void *ptr, size_t size ) {
( ( ( void * ) freeing ) + freeing->size ) ); ( ( ( void * ) freeing ) + freeing->size ) );
/* Merge with immediately preceding block, if possible */ /* Merge with immediately preceding block, if possible */
if ( gap_before == 0 ) { if ( gap_before == 0 ) {
DBGC2 ( &heap, "HEAP merging [%p,%p) + [%p,%p) -> " DBGC2 ( heap, "HEAP merging [%p,%p) + [%p,%p) -> "
"[%p,%p)\n", block, "[%p,%p)\n", block,
( ( ( void * ) block ) + block->size ), freeing, ( ( ( void * ) block ) + block->size ), freeing,
( ( ( void * ) freeing ) + freeing->size ), ( ( ( void * ) freeing ) + freeing->size ),
@@ -476,11 +471,11 @@ void free_memblock ( void *ptr, size_t size ) {
* possible, merge the following block into the "freeing" * possible, merge the following block into the "freeing"
* block. * block.
*/ */
DBGC2 ( &heap, "HEAP freed [%p,%p)\n", DBGC2 ( heap, "HEAP freed [%p,%p)\n",
freeing, ( ( ( void * ) freeing ) + freeing->size ) ); freeing, ( ( ( void * ) freeing ) + freeing->size ) );
list_add_tail ( &freeing->list, &block->list ); list_add_tail ( &freeing->list, &block->list );
if ( gap_after == 0 ) { if ( gap_after == 0 ) {
DBGC2 ( &heap, "HEAP merging [%p,%p) + [%p,%p) -> [%p,%p)\n", DBGC2 ( heap, "HEAP merging [%p,%p) + [%p,%p) -> [%p,%p)\n",
freeing, ( ( ( void * ) freeing ) + freeing->size ), freeing, ( ( ( void * ) freeing ) + freeing->size ),
block, ( ( ( void * ) block ) + block->size ), freeing, block, ( ( ( void * ) block ) + block->size ), freeing,
( ( ( void * ) block ) + block->size ) ); ( ( ( void * ) block ) + block->size ) );
@@ -490,17 +485,26 @@ void free_memblock ( void *ptr, size_t size ) {
} }
/* Update memory usage statistics */ /* Update memory usage statistics */
freemem += actual_size; heap->freemem += actual_size;
usedmem -= actual_size; heap->usedmem -= actual_size;
check_blocks(); /* Allow heap to shrink */
valgrind_make_blocks_noaccess(); if ( heap->shrink && heap->shrink ( freeing, freeing->size ) ) {
list_del ( &freeing->list );
heap->freemem -= freeing->size;
VALGRIND_MAKE_MEM_UNDEFINED ( freeing, freeing->size );
}
/* Sanity checks */
check_blocks ( heap );
valgrind_make_blocks_noaccess ( heap );
} }
/** /**
* Reallocate memory * Reallocate memory
* *
* @v old_ptr Memory previously allocated by malloc(), or NULL * @v heap Heap
* @v old_ptr Memory previously allocated by heap_realloc(), or NULL
* @v new_size Requested size * @v new_size Requested size
* @ret new_ptr Allocated memory, or NULL * @ret new_ptr Allocated memory, or NULL
* *
@@ -514,26 +518,26 @@ void free_memblock ( void *ptr, size_t size ) {
* If allocation fails the previously allocated block is left * If allocation fails the previously allocated block is left
* untouched and NULL is returned. * untouched and NULL is returned.
* *
* Calling realloc() with a new size of zero is a valid way to free a * Calling heap_realloc() with a new size of zero is a valid way to
* memory block. * free a memory block.
*/ */
void * realloc ( void *old_ptr, size_t new_size ) { void * heap_realloc ( struct heap *heap, void *old_ptr, size_t new_size ) {
struct autosized_block *old_block; struct autosized_block *old_block;
struct autosized_block *new_block; struct autosized_block *new_block;
size_t old_total_size; size_t old_total_size;
size_t new_total_size; size_t new_total_size;
size_t old_size; size_t old_size;
size_t offset = offsetof ( struct autosized_block, data );
void *new_ptr = NOWHERE; void *new_ptr = NOWHERE;
/* Allocate new memory if necessary. If allocation fails, /* Allocate new memory if necessary. If allocation fails,
* return without touching the old block. * return without touching the old block.
*/ */
if ( new_size ) { if ( new_size ) {
new_total_size = ( new_size + new_total_size = ( new_size + offset );
offsetof ( struct autosized_block, data ) );
if ( new_total_size < new_size ) if ( new_total_size < new_size )
return NULL; return NULL;
new_block = alloc_memblock ( new_total_size, 1, 0 ); new_block = heap_alloc_block ( heap, new_total_size, 1, 0 );
if ( ! new_block ) if ( ! new_block )
return NULL; return NULL;
new_block->size = new_total_size; new_block->size = new_total_size;
@@ -555,21 +559,38 @@ void * realloc ( void *old_ptr, size_t new_size ) {
sizeof ( old_block->size ) ); sizeof ( old_block->size ) );
old_total_size = old_block->size; old_total_size = old_block->size;
assert ( old_total_size != 0 ); assert ( old_total_size != 0 );
old_size = ( old_total_size - old_size = ( old_total_size - offset );
offsetof ( struct autosized_block, data ) );
memcpy ( new_ptr, old_ptr, memcpy ( new_ptr, old_ptr,
( ( old_size < new_size ) ? old_size : new_size ) ); ( ( old_size < new_size ) ? old_size : new_size ) );
VALGRIND_FREELIKE_BLOCK ( old_ptr, 0 ); VALGRIND_FREELIKE_BLOCK ( old_ptr, 0 );
free_memblock ( old_block, old_total_size ); heap_free_block ( heap, old_block, old_total_size );
} }
if ( ASSERTED ) { if ( ASSERTED ) {
DBGC ( &heap, "HEAP detected possible memory corruption " DBGC ( heap, "HEAP detected possible memory corruption "
"from %p\n", __builtin_return_address ( 0 ) ); "from %p\n", __builtin_return_address ( 0 ) );
} }
return new_ptr; return new_ptr;
} }
/** The global heap */
static struct heap heap = {
.blocks = LIST_HEAD_INIT ( heap.blocks ),
.grow = discard_cache,
};
/**
* Reallocate memory
*
* @v old_ptr Memory previously allocated by malloc(), or NULL
* @v new_size Requested size
* @ret new_ptr Allocated memory, or NULL
*/
void * realloc ( void *old_ptr, size_t new_size ) {
return heap_realloc ( &heap, old_ptr, new_size );
}
/** /**
* Allocate memory * Allocate memory
* *
@@ -633,16 +654,70 @@ void * zalloc ( size_t size ) {
return data; return data;
} }
/**
* Allocate memory with specified physical alignment and offset
*
* @v size Requested size
* @v align Physical alignment
* @v offset Offset from physical alignment
* @ret ptr Memory, or NULL
*
* @c align must be a power of two. @c size may not be zero.
*/
void * malloc_phys_offset ( size_t size, size_t phys_align, size_t offset ) {
void * ptr;
ptr = heap_alloc_block ( &heap, size, phys_align, offset );
if ( ptr && size ) {
assert ( ( phys_align == 0 ) ||
( ( ( virt_to_phys ( ptr ) ^ offset ) &
( phys_align - 1 ) ) == 0 ) );
VALGRIND_MALLOCLIKE_BLOCK ( ptr, size, 0, 0 );
}
return ptr;
}
/**
* Allocate memory with specified physical alignment
*
* @v size Requested size
* @v align Physical alignment
* @ret ptr Memory, or NULL
*
* @c align must be a power of two. @c size may not be zero.
*/
void * malloc_phys ( size_t size, size_t phys_align ) {
return malloc_phys_offset ( size, phys_align, 0 );
}
/**
* Free memory allocated with malloc_phys()
*
* @v ptr Memory allocated by malloc_phys(), or NULL
* @v size Size of memory, as passed to malloc_phys()
*
* Memory allocated with malloc_phys() can only be freed with
* free_phys(); it cannot be freed with the standard free().
*
* If @c ptr is NULL, no action is taken.
*/
void free_phys ( void *ptr, size_t size ) {
VALGRIND_FREELIKE_BLOCK ( ptr, 0 );
heap_free_block ( &heap, ptr, size );
}
/** /**
* Add memory to allocation pool * Add memory to allocation pool
* *
* @v heap Heap
* @v start Start address * @v start Start address
* @v len Length of memory * @v len Length of memory
* *
* Adds a block of memory to the allocation pool. This is a one-way * Adds a block of memory to the allocation pool.
* operation; there is no way to reclaim this memory.
*/ */
static void mpopulate ( void *start, size_t len ) { void heap_populate ( struct heap *heap, void *start, size_t len ) {
size_t skip; size_t skip;
/* Align start of block */ /* Align start of block */
@@ -658,10 +733,10 @@ static void mpopulate ( void *start, size_t len ) {
return; return;
/* Add to allocation pool */ /* Add to allocation pool */
free_memblock ( start, len ); heap_free_block ( heap, start, len );
/* Fix up memory usage statistics */ /* Fix up memory usage statistics */
usedmem += len; heap->usedmem += len;
} }
/** /**
@@ -669,9 +744,9 @@ static void mpopulate ( void *start, size_t len ) {
* *
*/ */
static void init_heap ( void ) { static void init_heap ( void ) {
VALGRIND_MAKE_MEM_NOACCESS ( heap, sizeof ( heap ) ); VALGRIND_MAKE_MEM_NOACCESS ( heap_area, sizeof ( heap_area ) );
VALGRIND_MAKE_MEM_NOACCESS ( &free_blocks, sizeof ( free_blocks ) ); VALGRIND_MAKE_MEM_NOACCESS ( &heap.blocks, sizeof ( heap.blocks ) );
mpopulate ( heap, sizeof ( heap ) ); heap_populate ( &heap, heap_area, sizeof ( heap_area ) );
} }
/** Memory allocator initialisation function */ /** Memory allocator initialisation function */
@@ -685,7 +760,8 @@ struct init_fn heap_init_fn __init_fn ( INIT_EARLY ) = {
*/ */
static void shutdown_cache ( int booting __unused ) { static void shutdown_cache ( int booting __unused ) {
discard_all_cache(); discard_all_cache();
DBGC ( &heap, "HEAP maximum usage %zdkB\n", ( maxusedmem >> 10 ) ); DBGC ( &heap, "HEAP maximum usage %zdkB\n",
( heap.maxusedmem >> 10 ) );
} }
/** Memory allocator shutdown function */ /** Memory allocator shutdown function */
@@ -698,11 +774,11 @@ struct startup_fn heap_startup_fn __startup_fn ( STARTUP_EARLY ) = {
* Dump free block list (for debugging) * Dump free block list (for debugging)
* *
*/ */
void mdumpfree ( void ) { void heap_dump ( struct heap *heap ) {
struct memory_block *block; struct memory_block *block;
dbg_printf ( "HEAP free block list:\n" ); dbg_printf ( "HEAP free block list:\n" );
list_for_each_entry ( block, &free_blocks, list ) { list_for_each_entry ( block, &heap->blocks, list ) {
dbg_printf ( "...[%p,%p] (size %#zx)\n", block, dbg_printf ( "...[%p,%p] (size %#zx)\n", block,
( ( ( void * ) block ) + block->size ), ( ( ( void * ) block ) + block->size ),
block->size ); block->size );

View File

@@ -7,9 +7,9 @@
* *
* We have no concept of the underlying physical addresses, since * We have no concept of the underlying physical addresses, since
* these are not exposed to userspace. We provide a stub * these are not exposed to userspace. We provide a stub
* implementation of virt_to_phys() since this is required by * implementation of virt_to_phys() since this is required by the heap
* alloc_memblock(). We provide a matching stub implementation of * allocator to determine physical address alignment. We provide a
* phys_to_virt(). * matching stub implementation of phys_to_virt().
*/ */
FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL ); FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
@@ -31,11 +31,11 @@ UACCESS_INLINE ( linux, virt_to_phys ) ( volatile const void *virt ) {
/* We do not know the real underlying physical address. We /* We do not know the real underlying physical address. We
* provide this stub implementation only because it is * provide this stub implementation only because it is
* required by alloc_memblock() (which allocates memory with * required in order to allocate memory with a specified
* specified physical address alignment). We assume that the * physical address alignment. We assume that the low-order
* low-order bits of virtual addresses match the low-order * bits of virtual addresses match the low-order bits of
* bits of physical addresses, and so simply returning the * physical addresses, and so simply returning the virtual
* virtual address will suffice for the purpose of determining * address will suffice for the purpose of determining
* alignment. * alignment.
*/ */
return ( ( physaddr_t ) virt ); return ( ( physaddr_t ) virt );

View File

@@ -18,6 +18,7 @@ FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
* *
*/ */
#include <stdlib.h> #include <stdlib.h>
#include <ipxe/list.h>
#include <ipxe/tables.h> #include <ipxe/tables.h>
#include <valgrind/memcheck.h> #include <valgrind/memcheck.h>
@@ -39,62 +40,48 @@ FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
*/ */
#define NOWHERE ( ( void * ) ~( ( intptr_t ) 0 ) ) #define NOWHERE ( ( void * ) ~( ( intptr_t ) 0 ) )
extern size_t freemem; /** A heap */
extern size_t usedmem; struct heap {
extern size_t maxusedmem; /** List of free memory blocks */
struct list_head blocks;
extern void * __malloc alloc_memblock ( size_t size, size_t align, /** Total amount of free memory */
size_t offset ); size_t freemem;
extern void free_memblock ( void *ptr, size_t size ); /** Total amount of used memory */
extern void mdumpfree ( void ); size_t usedmem;
/** Maximum amount of used memory */
size_t maxusedmem;
/** /**
* Allocate memory with specified physical alignment and offset * Attempt to grow heap (optional)
* *
* @v size Requested size * @v size Failed allocation size
* @v align Physical alignment * @ret grown Heap has grown: retry allocations
* @v offset Offset from physical alignment */
* @ret ptr Memory, or NULL unsigned int ( * grow ) ( size_t size );
* /**
* @c align must be a power of two. @c size may not be zero. * Allow heap to shrink (optional)
*/ *
static inline void * __malloc malloc_phys_offset ( size_t size, * @v ptr Start of free block
size_t phys_align, * @v size Size of free block
size_t offset ) { * @ret shrunk Heap has shrunk: discard block
void * ptr = alloc_memblock ( size, phys_align, offset ); *
if ( ptr && size ) * Note that the discarded block will be accessed once after
VALGRIND_MALLOCLIKE_BLOCK ( ptr, size, 0, 0 ); * this method returns, in order to clear the free block
return ptr; * metadata.
} */
unsigned int ( * shrink ) ( void *ptr, size_t size );
};
/** extern void * heap_realloc ( struct heap *heap, void *old_ptr,
* Allocate memory with specified physical alignment size_t new_size );
* extern void heap_dump ( struct heap *heap );
* @v size Requested size extern void heap_populate ( struct heap *heap, void *start, size_t len );
* @v align Physical alignment
* @ret ptr Memory, or NULL
*
* @c align must be a power of two. @c size may not be zero.
*/
static inline void * __malloc malloc_phys ( size_t size, size_t phys_align ) {
return malloc_phys_offset ( size, phys_align, 0 );
}
/** extern void * __malloc malloc_phys_offset ( size_t size, size_t phys_align,
* Free memory allocated with malloc_phys() size_t offset );
* extern void * __malloc malloc_phys ( size_t size, size_t phys_align );
* @v ptr Memory allocated by malloc_phys(), or NULL extern void free_phys ( void *ptr, size_t size );
* @v size Size of memory, as passed to malloc_phys()
*
* Memory allocated with malloc_phys() can only be freed with
* free_phys(); it cannot be freed with the standard free().
*
* If @c ptr is NULL, no action is taken.
*/
static inline void free_phys ( void *ptr, size_t size ) {
VALGRIND_FREELIKE_BLOCK ( ptr, 0 );
free_memblock ( ptr, size );
}
/** A cache discarder */ /** A cache discarder */
struct cache_discarder { struct cache_discarder {