X-Git-Url: https://gerrit.opnfv.org/gerrit/gitweb?a=blobdiff_plain;f=qemu%2Froms%2Fipxe%2Fsrc%2Fcore%2Fmalloc.c;h=b120c03255e5e9e9daf6f304fb568a4118604ea6;hb=a14b48d18a9ed03ec191cf16b162206998a895ce;hp=d9c07495d5dcd3f3d8c709fe68a117a6edfff14d;hpb=e44e3482bdb4d0ebde2d8b41830ac2cdb07948fb;p=kvmfornfv.git diff --git a/qemu/roms/ipxe/src/core/malloc.c b/qemu/roms/ipxe/src/core/malloc.c index d9c07495d..b120c0325 100644 --- a/qemu/roms/ipxe/src/core/malloc.c +++ b/qemu/roms/ipxe/src/core/malloc.c @@ -15,9 +15,13 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA * 02110-1301, USA. + * + * You can also choose to distribute this program under the terms of + * the Unmodified Binary Distribution Licence (as given in the file + * COPYING.UBDL), provided that you have satisfied its requirements. */ -FILE_LICENCE ( GPL2_OR_LATER ); +FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL ); #include #include @@ -106,6 +110,7 @@ static char heap[HEAP_SIZE] __attribute__ (( aligned ( __alignof__(void *) ))); static inline void valgrind_make_blocks_defined ( void ) { struct memory_block *block; + /* Do nothing unless running under Valgrind */ if ( RUNNING_ON_VALGRIND <= 0 ) return; @@ -147,6 +152,7 @@ static inline void valgrind_make_blocks_noaccess ( void ) { struct memory_block *block; struct memory_block *prev = NULL; + /* Do nothing unless running under Valgrind */ if ( RUNNING_ON_VALGRIND <= 0 ) return; @@ -267,24 +273,25 @@ static void discard_all_cache ( void ) { void * alloc_memblock ( size_t size, size_t align, size_t offset ) { struct memory_block *block; size_t align_mask; + size_t actual_size; size_t pre_size; ssize_t post_size; struct memory_block *pre; struct memory_block *post; - struct memory_block *ptr; + void *ptr; /* Sanity checks */ assert ( size != 0 ); assert ( ( align == 0 ) || ( ( align & ( align - 1 ) ) == 0 ) ); - valgrind_make_blocks_defined(); check_blocks(); /* Round up size to multiple of MIN_MEMBLOCK_SIZE and * calculate alignment mask. */ - size = ( size + MIN_MEMBLOCK_SIZE - 1 ) & ~( MIN_MEMBLOCK_SIZE - 1 ); - align_mask = ( align - 1 ) | ( MIN_MEMBLOCK_SIZE - 1 ); + actual_size = ( ( size + MIN_MEMBLOCK_SIZE - 1 ) & + ~( MIN_MEMBLOCK_SIZE - 1 ) ); + align_mask = ( ( align - 1 ) | ( MIN_MEMBLOCK_SIZE - 1 ) ); DBGC2 ( &heap, "Allocating %#zx (aligned %#zx+%zx)\n", size, align, offset ); @@ -293,7 +300,7 @@ void * alloc_memblock ( size_t size, size_t align, size_t offset ) { list_for_each_entry ( block, &free_blocks, list ) { pre_size = ( ( offset - virt_to_phys ( block ) ) & align_mask ); - post_size = ( block->size - pre_size - size ); + post_size = ( block->size - pre_size - actual_size ); if ( post_size >= 0 ) { /* Split block into pre-block, block, and * post-block. After this split, the "pre" @@ -302,7 +309,7 @@ void * alloc_memblock ( size_t size, size_t align, size_t offset ) { */ pre = block; block = ( ( ( void * ) pre ) + pre_size ); - post = ( ( ( void * ) block ) + size ); + post = ( ( ( void * ) block ) + actual_size ); DBGC2 ( &heap, "[%p,%p) -> [%p,%p) + [%p,%p)\n", pre, ( ( ( void * ) pre ) + pre->size ), pre, block, post, @@ -313,8 +320,8 @@ void * alloc_memblock ( size_t size, size_t align, size_t offset ) { * the heap). */ if ( (size_t) post_size >= MIN_MEMBLOCK_SIZE ) { - VALGRIND_MAKE_MEM_DEFINED ( post, - sizeof ( *post ) ); + VALGRIND_MAKE_MEM_UNDEFINED + ( post, sizeof ( *post ) ); post->size = post_size; list_add ( &post->list, &pre->list ); } @@ -328,14 +335,18 @@ void * alloc_memblock ( size_t size, size_t align, size_t offset ) { * it is too small, which can happen only at * the very start of the heap. */ - if ( pre_size < MIN_MEMBLOCK_SIZE ) + if ( pre_size < MIN_MEMBLOCK_SIZE ) { list_del ( &pre->list ); + VALGRIND_MAKE_MEM_NOACCESS + ( pre, sizeof ( *pre ) ); + } /* Update total free memory */ - freemem -= size; + freemem -= actual_size; /* Return allocated block */ DBGC2 ( &heap, "Allocated [%p,%p)\n", block, ( ( ( void * ) block ) + size ) ); ptr = block; + VALGRIND_MAKE_MEM_UNDEFINED ( ptr, size ); goto done; } } @@ -368,13 +379,16 @@ void free_memblock ( void *ptr, size_t size ) { struct memory_block *freeing; struct memory_block *block; struct memory_block *tmp; + size_t actual_size; ssize_t gap_before; ssize_t gap_after = -1; /* Allow for ptr==NULL */ if ( ! ptr ) return; + VALGRIND_MAKE_MEM_NOACCESS ( ptr, size ); + /* Sanity checks */ valgrind_make_blocks_defined(); check_blocks(); @@ -382,9 +396,10 @@ void free_memblock ( void *ptr, size_t size ) { * would have used. */ assert ( size != 0 ); - size = ( size + MIN_MEMBLOCK_SIZE - 1 ) & ~( MIN_MEMBLOCK_SIZE - 1 ); + actual_size = ( ( size + MIN_MEMBLOCK_SIZE - 1 ) & + ~( MIN_MEMBLOCK_SIZE - 1 ) ); freeing = ptr; - VALGRIND_MAKE_MEM_DEFINED ( freeing, sizeof ( *freeing ) ); + VALGRIND_MAKE_MEM_UNDEFINED ( freeing, sizeof ( *freeing ) ); DBGC2 ( &heap, "Freeing [%p,%p)\n", freeing, ( ( ( void * ) freeing ) + size ) ); @@ -392,7 +407,7 @@ void free_memblock ( void *ptr, size_t size ) { if ( ASSERTING ) { list_for_each_entry ( block, &free_blocks, list ) { if ( ( ( ( void * ) block ) < - ( ( void * ) freeing + size ) ) && + ( ( void * ) freeing + actual_size ) ) && ( ( void * ) freeing < ( ( void * ) block + block->size ) ) ) { assert ( 0 ); @@ -407,7 +422,7 @@ void free_memblock ( void *ptr, size_t size ) { } /* Insert/merge into free list */ - freeing->size = size; + freeing->size = actual_size; list_for_each_entry_safe ( block, tmp, &free_blocks, list ) { /* Calculate gaps before and after the "freeing" block */ gap_before = ( ( ( void * ) freeing ) - @@ -421,8 +436,10 @@ void free_memblock ( void *ptr, size_t size ) { ( ( ( void * ) freeing ) + freeing->size ), block, ( ( ( void * ) freeing ) + freeing->size ) ); - block->size += size; + block->size += actual_size; list_del ( &block->list ); + VALGRIND_MAKE_MEM_NOACCESS ( freeing, + sizeof ( *freeing ) ); freeing = block; } /* Stop processing as soon as we reach a following block */ @@ -444,10 +461,11 @@ void free_memblock ( void *ptr, size_t size ) { ( ( ( void * ) block ) + block->size ) ); freeing->size += block->size; list_del ( &block->list ); + VALGRIND_MAKE_MEM_NOACCESS ( block, sizeof ( *block ) ); } /* Update free memory counter */ - freemem += size; + freemem += actual_size; check_blocks(); valgrind_make_blocks_noaccess(); @@ -490,9 +508,9 @@ void * realloc ( void *old_ptr, size_t new_size ) { 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 ) ); new_block->size = new_total_size; - VALGRIND_MAKE_MEM_NOACCESS ( new_block, offsetof ( struct autosized_block, data ) ); + VALGRIND_MAKE_MEM_NOACCESS ( &new_block->size, + sizeof ( new_block->size ) ); new_ptr = &new_block->data; VALGRIND_MALLOCLIKE_BLOCK ( new_ptr, new_size, 0, 0 ); } @@ -505,16 +523,16 @@ void * realloc ( void *old_ptr, size_t new_size ) { if ( old_ptr && ( old_ptr != NOWHERE ) ) { old_block = container_of ( old_ptr, struct autosized_block, data ); - VALGRIND_MAKE_MEM_DEFINED ( old_block, offsetof ( struct autosized_block, data ) ); + VALGRIND_MAKE_MEM_DEFINED ( &old_block->size, + sizeof ( old_block->size ) ); old_total_size = old_block->size; assert ( old_total_size != 0 ); old_size = ( old_total_size - offsetof ( struct autosized_block, data ) ); memcpy ( new_ptr, old_ptr, ( ( old_size < new_size ) ? old_size : new_size ) ); - free_memblock ( old_block, old_total_size ); - VALGRIND_MAKE_MEM_NOACCESS ( old_block, offsetof ( struct autosized_block, data ) ); VALGRIND_FREELIKE_BLOCK ( old_ptr, 0 ); + free_memblock ( old_block, old_total_size ); } if ( ASSERTED ) { @@ -611,6 +629,7 @@ void mpopulate ( void *start, size_t len ) { */ static void init_heap ( void ) { VALGRIND_MAKE_MEM_NOACCESS ( heap, sizeof ( heap ) ); + VALGRIND_MAKE_MEM_NOACCESS ( &free_blocks, sizeof ( free_blocks ) ); mpopulate ( heap, sizeof ( heap ) ); }