* 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 <stddef.h>
#include <stdint.h>
static inline void valgrind_make_blocks_defined ( void ) {
struct memory_block *block;
+ /* Do nothing unless running under Valgrind */
if ( RUNNING_ON_VALGRIND <= 0 )
return;
struct memory_block *block;
struct memory_block *prev = NULL;
+ /* Do nothing unless running under Valgrind */
if ( RUNNING_ON_VALGRIND <= 0 )
return;
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 );
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"
*/
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,
* 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 );
}
* 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;
}
}
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();
* 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 ) );
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 );
}
/* 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 ) -
( ( ( 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 */
( ( ( 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();
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 );
}
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 ) {
*/
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 ) );
}