diff --git a/src/liballocator/mem-heap.c b/src/liballocator/mem-heap.c index 7a605aaff..fd7d6c9b2 100644 --- a/src/liballocator/mem-heap.c +++ b/src/liballocator/mem-heap.c @@ -417,63 +417,73 @@ mem_heap_recommend_allocation_size( size_t minimum_allocation_size) /**< minimum * Print heap */ void -mem_heap_print( bool dump_block_data) /**< print block with data (true) - or print only block header (false) */ +mem_heap_print( bool dump_block_headers, /**< print block headers */ + bool dump_block_data, /**< print block with data (true) + or print only block header (false) */ + bool dump_stats) /**< print heap stats */ { mem_check_heap(); - __printf("Heap: start=%p size=%lu, first block->%p, last block->%p\n", - mem_heap.heap_start, - mem_heap.heap_size, - (void*) mem_heap.first_block_p, - (void*) mem_heap.last_block_p); + JERRY_ASSERT( !dump_block_data || dump_block_headers ); - for ( mem_block_header_t *block_p = mem_heap.first_block_p; - block_p != NULL; - block_p = block_p->neighbours[ MEM_DIRECTION_NEXT ] ) - { - __printf("Block (%p): magic num=0x%08x, size in chunks=%lu, previous block->%p next block->%p\n", - (void*) block_p, - block_p->magic_num, - mem_get_block_chunks_count( block_p), - (void*) block_p->neighbours[ MEM_DIRECTION_PREV ], - (void*) block_p->neighbours[ MEM_DIRECTION_NEXT ]); - - if ( dump_block_data ) + if ( dump_block_headers ) { - uint8_t *block_data_p = (uint8_t*) (block_p + 1); - for ( uint32_t offset = 0; - offset < mem_get_block_data_space_size( block_p); - offset++ ) - { - __printf("%02x ", block_data_p[ offset ]); - } - __printf("\n"); + __printf("Heap: start=%p size=%lu, first block->%p, last block->%p\n", + mem_heap.heap_start, + mem_heap.heap_size, + (void*) mem_heap.first_block_p, + (void*) mem_heap.last_block_p); + + for ( mem_block_header_t *block_p = mem_heap.first_block_p; + block_p != NULL; + block_p = block_p->neighbours[ MEM_DIRECTION_NEXT ] ) + { + __printf("Block (%p): magic num=0x%08x, size in chunks=%lu, previous block->%p next block->%p\n", + (void*) block_p, + block_p->magic_num, + mem_get_block_chunks_count( block_p), + (void*) block_p->neighbours[ MEM_DIRECTION_PREV ], + (void*) block_p->neighbours[ MEM_DIRECTION_NEXT ]); + + if ( dump_block_data ) + { + uint8_t *block_data_p = (uint8_t*) (block_p + 1); + for ( uint32_t offset = 0; + offset < mem_get_block_data_space_size( block_p); + offset++ ) + { + __printf("%02x ", block_data_p[ offset ]); + } + __printf("\n"); + } + } } - } #ifdef MEM_STATS - __printf("Heap stats:\n"); - __printf(" Size = %lu\n" - " Blocks count = %lu\n" - " Allocated blocks count = %lu\n" - " Allocated chunks count = %lu\n" - " Allocated bytes count = %lu\n" - " Waste bytes count = %lu\n" - " Peak allocated blocks count = %lu\n" - " Peak allocated chunks count = %lu\n" - " Peak allocated bytes count = %lu\n" - " Peak waste bytes count = %lu\n", - mem_heap_stats.size, - mem_heap_stats.blocks, - mem_heap_stats.allocated_blocks, - mem_heap_stats.allocated_chunks, - mem_heap_stats.allocated_bytes, - mem_heap_stats.waste_bytes, - mem_heap_stats.peak_allocated_blocks, - mem_heap_stats.peak_allocated_chunks, - mem_heap_stats.peak_allocated_bytes, - mem_heap_stats.peak_waste_bytes); + if ( dump_stats ) + { + __printf("Heap stats:\n"); + __printf(" Size = %lu\n" + " Blocks count = %lu\n" + " Allocated blocks count = %lu\n" + " Allocated chunks count = %lu\n" + " Allocated bytes count = %lu\n" + " Waste bytes count = %lu\n" + " Peak allocated blocks count = %lu\n" + " Peak allocated chunks count = %lu\n" + " Peak allocated bytes count = %lu\n" + " Peak waste bytes count = %lu\n", + mem_heap_stats.size, + mem_heap_stats.blocks, + mem_heap_stats.allocated_blocks, + mem_heap_stats.allocated_chunks, + mem_heap_stats.allocated_bytes, + mem_heap_stats.waste_bytes, + mem_heap_stats.peak_allocated_blocks, + mem_heap_stats.peak_allocated_chunks, + mem_heap_stats.peak_allocated_bytes, + mem_heap_stats.peak_waste_bytes); + } #endif /* MEM_STATS */ __printf("\n"); diff --git a/src/liballocator/mem-heap.h b/src/liballocator/mem-heap.h index ba94a06fb..08d1a5016 100644 --- a/src/liballocator/mem-heap.h +++ b/src/liballocator/mem-heap.h @@ -42,7 +42,7 @@ extern void mem_heap_init(uint8_t *heap_start, size_t heap_size); extern uint8_t* mem_heap_alloc_block(size_t size_in_bytes, mem_heap_alloc_term_t alloc_term); extern void mem_heap_free_block(uint8_t *ptr); extern size_t mem_heap_recommend_allocation_size(size_t minimum_allocation_size); -extern void mem_heap_print(bool dump_block_data); +extern void mem_heap_print(bool dump_block_headers, bool dump_block_data, bool dump_stats); #ifdef MEM_STATS /** diff --git a/src/main.c b/src/main.c index 969e74763..ba9e2886b 100644 --- a/src/main.c +++ b/src/main.c @@ -144,6 +144,8 @@ main (int argc __unused, jerry_run( source_p, source_size); + mem_heap_print( false, false, true); + return 0; } #endif diff --git a/tests/unit/test_heap.c b/tests/unit/test_heap.c index 1ee02e257..880f27d99 100644 --- a/tests/unit/test_heap.c +++ b/tests/unit/test_heap.c @@ -82,7 +82,7 @@ main( int __unused argc, } } - mem_heap_print( false); + mem_heap_print( true, false, true); return 0; } /* main */