david/ipxe
Archived
1
0

[malloc] Rewrite unrelicensable portions of malloc.c

Signed-off-by: Michael Brown <mcb30@ipxe.org>
This commit is contained in:
Michael Brown 2015-03-03 15:16:32 +00:00
parent 5f3d165232
commit b2f38da888
2 changed files with 43 additions and 24 deletions

View File

@ -15,9 +15,13 @@
* along with this program; if not, write to the Free Software * along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA. * 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 <stddef.h> #include <stddef.h>
#include <stdint.h> #include <stdint.h>
@ -106,6 +110,7 @@ static char heap[HEAP_SIZE] __attribute__ (( aligned ( __alignof__(void *) )));
static inline void valgrind_make_blocks_defined ( void ) { static inline void valgrind_make_blocks_defined ( void ) {
struct memory_block *block; struct memory_block *block;
/* Do nothing unless running under Valgrind */
if ( RUNNING_ON_VALGRIND <= 0 ) if ( RUNNING_ON_VALGRIND <= 0 )
return; return;
@ -147,6 +152,7 @@ static inline void valgrind_make_blocks_noaccess ( void ) {
struct memory_block *block; struct memory_block *block;
struct memory_block *prev = NULL; struct memory_block *prev = NULL;
/* Do nothing unless running under Valgrind */
if ( RUNNING_ON_VALGRIND <= 0 ) if ( RUNNING_ON_VALGRIND <= 0 )
return; return;
@ -267,24 +273,25 @@ static void discard_all_cache ( void ) {
void * alloc_memblock ( size_t size, size_t align, size_t offset ) { void * alloc_memblock ( size_t size, size_t align, size_t offset ) {
struct memory_block *block; struct memory_block *block;
size_t align_mask; size_t align_mask;
size_t actual_size;
size_t pre_size; size_t pre_size;
ssize_t post_size; ssize_t post_size;
struct memory_block *pre; struct memory_block *pre;
struct memory_block *post; struct memory_block *post;
struct memory_block *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();
check_blocks(); check_blocks();
/* Round up size to multiple of MIN_MEMBLOCK_SIZE and /* Round up size to multiple of MIN_MEMBLOCK_SIZE and
* calculate alignment mask. * calculate alignment mask.
*/ */
size = ( size + MIN_MEMBLOCK_SIZE - 1 ) & ~( MIN_MEMBLOCK_SIZE - 1 ); actual_size = ( ( size + MIN_MEMBLOCK_SIZE - 1 ) &
align_mask = ( align - 1 ) | ( MIN_MEMBLOCK_SIZE - 1 ); ~( MIN_MEMBLOCK_SIZE - 1 ) );
align_mask = ( ( align - 1 ) | ( MIN_MEMBLOCK_SIZE - 1 ) );
DBGC2 ( &heap, "Allocating %#zx (aligned %#zx+%zx)\n", DBGC2 ( &heap, "Allocating %#zx (aligned %#zx+%zx)\n",
size, align, offset ); 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 ) { list_for_each_entry ( block, &free_blocks, list ) {
pre_size = ( ( offset - virt_to_phys ( block ) ) pre_size = ( ( offset - virt_to_phys ( block ) )
& align_mask ); & align_mask );
post_size = ( block->size - pre_size - size ); post_size = ( block->size - pre_size - actual_size );
if ( post_size >= 0 ) { if ( post_size >= 0 ) {
/* Split block into pre-block, block, and /* Split block into pre-block, block, and
* post-block. After this split, the "pre" * 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; pre = block;
block = ( ( ( void * ) pre ) + pre_size ); block = ( ( ( void * ) pre ) + pre_size );
post = ( ( ( void * ) block ) + size ); post = ( ( ( void * ) block ) + actual_size );
DBGC2 ( &heap, "[%p,%p) -> [%p,%p) + [%p,%p)\n", DBGC2 ( &heap, "[%p,%p) -> [%p,%p) + [%p,%p)\n",
pre, ( ( ( void * ) pre ) + pre->size ), pre, ( ( ( void * ) pre ) + pre->size ),
pre, block, post, pre, block, post,
@ -313,8 +320,8 @@ void * alloc_memblock ( size_t size, size_t align, size_t offset ) {
* the heap). * the heap).
*/ */
if ( (size_t) post_size >= MIN_MEMBLOCK_SIZE ) { if ( (size_t) post_size >= MIN_MEMBLOCK_SIZE ) {
VALGRIND_MAKE_MEM_DEFINED ( post, VALGRIND_MAKE_MEM_UNDEFINED
sizeof ( *post ) ); ( post, sizeof ( *post ) );
post->size = post_size; post->size = post_size;
list_add ( &post->list, &pre->list ); 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 * it is too small, which can happen only at
* the very start of the heap. * the very start of the heap.
*/ */
if ( pre_size < MIN_MEMBLOCK_SIZE ) if ( pre_size < MIN_MEMBLOCK_SIZE ) {
list_del ( &pre->list ); list_del ( &pre->list );
VALGRIND_MAKE_MEM_NOACCESS
( pre, sizeof ( *pre ) );
}
/* Update total free memory */ /* Update total free memory */
freemem -= size; freemem -= actual_size;
/* Return allocated block */ /* Return allocated block */
DBGC2 ( &heap, "Allocated [%p,%p)\n", block, DBGC2 ( &heap, "Allocated [%p,%p)\n", block,
( ( ( void * ) block ) + size ) ); ( ( ( void * ) block ) + size ) );
ptr = block; ptr = block;
VALGRIND_MAKE_MEM_UNDEFINED ( ptr, size );
goto done; goto done;
} }
} }
@ -368,13 +379,16 @@ void free_memblock ( 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;
size_t actual_size;
ssize_t gap_before; ssize_t gap_before;
ssize_t gap_after = -1; ssize_t gap_after = -1;
/* Allow for ptr==NULL */ /* Allow for ptr==NULL */
if ( ! ptr ) if ( ! ptr )
return; return;
VALGRIND_MAKE_MEM_NOACCESS ( ptr, size );
/* Sanity checks */
valgrind_make_blocks_defined(); valgrind_make_blocks_defined();
check_blocks(); check_blocks();
@ -382,9 +396,10 @@ void free_memblock ( void *ptr, size_t size ) {
* would have used. * would have used.
*/ */
assert ( size != 0 ); 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; freeing = ptr;
VALGRIND_MAKE_MEM_DEFINED ( freeing, sizeof ( *freeing ) ); VALGRIND_MAKE_MEM_UNDEFINED ( freeing, sizeof ( *freeing ) );
DBGC2 ( &heap, "Freeing [%p,%p)\n", DBGC2 ( &heap, "Freeing [%p,%p)\n",
freeing, ( ( ( void * ) freeing ) + size ) ); freeing, ( ( ( void * ) freeing ) + size ) );
@ -392,7 +407,7 @@ void free_memblock ( void *ptr, size_t size ) {
if ( ASSERTING ) { if ( ASSERTING ) {
list_for_each_entry ( block, &free_blocks, list ) { list_for_each_entry ( block, &free_blocks, list ) {
if ( ( ( ( void * ) block ) < if ( ( ( ( void * ) block ) <
( ( void * ) freeing + size ) ) && ( ( void * ) freeing + actual_size ) ) &&
( ( void * ) freeing < ( ( void * ) freeing <
( ( void * ) block + block->size ) ) ) { ( ( void * ) block + block->size ) ) ) {
assert ( 0 ); assert ( 0 );
@ -407,7 +422,7 @@ void free_memblock ( void *ptr, size_t size ) {
} }
/* Insert/merge into free list */ /* Insert/merge into free list */
freeing->size = size; freeing->size = actual_size;
list_for_each_entry_safe ( block, tmp, &free_blocks, list ) { list_for_each_entry_safe ( block, tmp, &free_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 ) -
@ -421,8 +436,10 @@ void free_memblock ( void *ptr, size_t size ) {
( ( ( void * ) freeing ) + freeing->size ), ( ( ( void * ) freeing ) + freeing->size ),
block, block,
( ( ( void * ) freeing ) + freeing->size ) ); ( ( ( void * ) freeing ) + freeing->size ) );
block->size += size; block->size += actual_size;
list_del ( &block->list ); list_del ( &block->list );
VALGRIND_MAKE_MEM_NOACCESS ( freeing,
sizeof ( *freeing ) );
freeing = block; freeing = block;
} }
/* Stop processing as soon as we reach a following 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 ) ); ( ( ( void * ) block ) + block->size ) );
freeing->size += block->size; freeing->size += block->size;
list_del ( &block->list ); list_del ( &block->list );
VALGRIND_MAKE_MEM_NOACCESS ( block, sizeof ( *block ) );
} }
/* Update free memory counter */ /* Update free memory counter */
freemem += size; freemem += actual_size;
check_blocks(); check_blocks();
valgrind_make_blocks_noaccess(); 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 ); new_block = alloc_memblock ( new_total_size, 1, 0 );
if ( ! new_block ) if ( ! new_block )
return NULL; return NULL;
VALGRIND_MAKE_MEM_UNDEFINED ( new_block, offsetof ( struct autosized_block, data ) );
new_block->size = new_total_size; 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; new_ptr = &new_block->data;
VALGRIND_MALLOCLIKE_BLOCK ( new_ptr, new_size, 0, 0 ); 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 ) ) { if ( old_ptr && ( old_ptr != NOWHERE ) ) {
old_block = container_of ( old_ptr, struct autosized_block, old_block = container_of ( old_ptr, struct autosized_block,
data ); 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; 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 -
offsetof ( struct autosized_block, data ) ); 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 ) );
free_memblock ( old_block, old_total_size );
VALGRIND_MAKE_MEM_NOACCESS ( old_block, offsetof ( struct autosized_block, data ) );
VALGRIND_FREELIKE_BLOCK ( old_ptr, 0 ); VALGRIND_FREELIKE_BLOCK ( old_ptr, 0 );
free_memblock ( old_block, old_total_size );
} }
if ( ASSERTED ) { if ( ASSERTED ) {
@ -611,6 +629,7 @@ 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, sizeof ( heap ) );
VALGRIND_MAKE_MEM_NOACCESS ( &free_blocks, sizeof ( free_blocks ) );
mpopulate ( heap, sizeof ( heap ) ); mpopulate ( heap, sizeof ( heap ) );
} }

View File

@ -9,7 +9,7 @@
* *
*/ */
FILE_LICENCE ( GPL2_OR_LATER ); FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
/* /*
* Prototypes for the standard functions (malloc() et al) are in * Prototypes for the standard functions (malloc() et al) are in
@ -77,8 +77,8 @@ static inline void * __malloc malloc_dma ( size_t size, size_t phys_align ) {
* If @c ptr is NULL, no action is taken. * If @c ptr is NULL, no action is taken.
*/ */
static inline void free_dma ( void *ptr, size_t size ) { static inline void free_dma ( void *ptr, size_t size ) {
free_memblock ( ptr, size );
VALGRIND_FREELIKE_BLOCK ( ptr, 0 ); VALGRIND_FREELIKE_BLOCK ( ptr, 0 );
free_memblock ( ptr, size );
} }
/** A cache discarder */ /** A cache discarder */