diff options
author | Michael Brown | 2012-08-29 16:35:46 +0200 |
---|---|---|
committer | Michael Brown | 2012-08-31 22:22:45 +0200 |
commit | e2becce1862abbf4b656967c2fda95f28a19e0e4 (patch) | |
tree | d7bc42e0ae8e1c829cd1893b654535291a36f53c /src/core/malloc.c | |
parent | [retry] Expose retry_poll() to explicitly poll all running timers (diff) | |
download | ipxe-e2becce1862abbf4b656967c2fda95f28a19e0e4.tar.gz ipxe-e2becce1862abbf4b656967c2fda95f28a19e0e4.tar.xz ipxe-e2becce1862abbf4b656967c2fda95f28a19e0e4.zip |
[malloc] Allow allocation of memory with a specified alignment offset
Allow for allocation of memory blocks having a specified offset from a
specified physical alignment, such as being 12 bytes before a 2kB
boundary.
Signed-off-by: Michael Brown <mcb30@ipxe.org>
Diffstat (limited to 'src/core/malloc.c')
-rw-r--r-- | src/core/malloc.c | 12 |
1 files changed, 7 insertions, 5 deletions
diff --git a/src/core/malloc.c b/src/core/malloc.c index 9d81ca5c..56ca7edc 100644 --- a/src/core/malloc.c +++ b/src/core/malloc.c @@ -220,6 +220,7 @@ static void discard_all_cache ( void ) { * * @v size Requested size * @v align Physical alignment + * @v offset Offset from physical alignment * @ret ptr Memory block, or NULL * * Allocates a memory block @b physically aligned as requested. No @@ -227,7 +228,7 @@ static void discard_all_cache ( void ) { * * @c align must be a power of two. @c size may not be zero. */ -void * alloc_memblock ( size_t size, size_t align ) { +void * alloc_memblock ( size_t size, size_t align, size_t offset ) { struct memory_block *block; size_t align_mask; size_t pre_size; @@ -244,12 +245,13 @@ void * alloc_memblock ( size_t size, size_t align ) { size = ( size + MIN_MEMBLOCK_SIZE - 1 ) & ~( MIN_MEMBLOCK_SIZE - 1 ); align_mask = ( align - 1 ) | ( MIN_MEMBLOCK_SIZE - 1 ); - DBG ( "Allocating %#zx (aligned %#zx)\n", size, align ); + DBG ( "Allocating %#zx (aligned %#zx+%zx)\n", size, align, offset ); while ( 1 ) { /* Search through blocks for the first one with enough space */ list_for_each_entry ( block, &free_blocks, list ) { - pre_size = ( - virt_to_phys ( block ) ) & align_mask; - post_size = block->size - pre_size - size; + pre_size = ( ( offset - virt_to_phys ( block ) ) + & align_mask ); + 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" @@ -418,7 +420,7 @@ void * realloc ( void *old_ptr, size_t new_size ) { if ( new_size ) { new_total_size = ( new_size + offsetof ( struct autosized_block, data ) ); - new_block = alloc_memblock ( new_total_size, 1 ); + new_block = alloc_memblock ( new_total_size, 1, 0 ); if ( ! new_block ) return NULL; VALGRIND_MAKE_MEM_UNDEFINED ( new_block, offsetof ( struct autosized_block, data ) ); |