From 6ced002a691145a31478b68b0d2a607ed5d44950 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20P=C3=A9gouri=C3=A9-Gonnard?= Date: Wed, 5 Jan 2022 10:05:54 +0100 Subject: [PATCH] Count allocs without side-effects MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit At the end of the benchmark program, heap stats are printed, and these stats will be wrong if we reset counters in the middle. Also remove the function to reset counters, in order to encourage other programs to behave correctly as well. Signed-off-by: Manuel Pégourié-Gonnard --- include/mbedtls/memory_buffer_alloc.h | 5 ----- library/memory_buffer_alloc.c | 6 ------ programs/test/benchmark.c | 7 ++++--- 3 files changed, 4 insertions(+), 14 deletions(-) diff --git a/include/mbedtls/memory_buffer_alloc.h b/include/mbedtls/memory_buffer_alloc.h index ee1973cd0c..b024162833 100644 --- a/include/mbedtls/memory_buffer_alloc.h +++ b/include/mbedtls/memory_buffer_alloc.h @@ -98,11 +98,6 @@ void mbedtls_memory_buffer_alloc_status( void ); */ void mbedtls_memory_buffer_alloc_count_get( size_t *alloc_count, size_t *free_count ); -/** - * \brief Reset alloc/free counters. - */ -void mbedtls_memory_buffer_alloc_count_reset( void ); - /** * \brief Get the peak heap usage so far * diff --git a/library/memory_buffer_alloc.c b/library/memory_buffer_alloc.c index ddb9595fbc..8c6b442657 100644 --- a/library/memory_buffer_alloc.c +++ b/library/memory_buffer_alloc.c @@ -528,12 +528,6 @@ void mbedtls_memory_buffer_alloc_count_get( size_t *alloc_count, size_t *free_co *free_count = heap.free_count; } -void mbedtls_memory_buffer_alloc_count_reset( void ) -{ - heap.alloc_count = 0; - heap.free_count = 0; -} - void mbedtls_memory_buffer_alloc_max_get( size_t *max_used, size_t *max_blocks ) { *max_used = heap.maximum_used; diff --git a/programs/test/benchmark.c b/programs/test/benchmark.c index 2797c1463f..2465f1f6e1 100644 --- a/programs/test/benchmark.c +++ b/programs/test/benchmark.c @@ -186,12 +186,12 @@ do { \ #define MEMORY_MEASURE_INIT \ size_t max_used, max_blocks, max_bytes; \ size_t prv_used, prv_blocks; \ - size_t alloc_cnt, free_cnt; \ + size_t alloc_cnt, free_cnt, prv_alloc, prv_free; \ mbedtls_memory_buffer_alloc_cur_get( &prv_used, &prv_blocks ); \ mbedtls_memory_buffer_alloc_max_reset( ); #define MEMORY_MEASURE_RESET \ - mbedtls_memory_buffer_alloc_count_reset( ); + mbedtls_memory_buffer_alloc_count_get( &prv_alloc, &prv_free ); #define MEMORY_MEASURE_PRINT( title_len ) \ mbedtls_memory_buffer_alloc_max_get( &max_used, &max_blocks ); \ @@ -202,7 +202,8 @@ do { \ max_blocks -= prv_blocks; \ max_bytes = max_used + MEM_BLOCK_OVERHEAD * max_blocks; \ mbedtls_printf( "%6u heap bytes, %6u allocs", \ - (unsigned) max_bytes, (unsigned) alloc_cnt ); + (unsigned) max_bytes, \ + (unsigned)( alloc_cnt - prv_alloc ) ); #else #define MEMORY_MEASURE_INIT