Skip to content

Commit 47670d1

Browse files
Chen Peng1nashif
authored andcommitted
tests: benchmarks: latency_measure: fix potential div by zero
If the count is zero in the heap_malloc_free test, a div by zero would happen, so add a condition to handle this potential error. Signed-off-by: Chen Peng1 <[email protected]>
1 parent d39410a commit 47670d1

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

tests/benchmarks/latency_measure/src/heap_malloc_free.c

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,18 @@ void heap_malloc_free(void)
4747
count++;
4848
}
4949

50-
PRINT_STATS_AVG("Average time for heap malloc", sum_malloc, count);
51-
PRINT_STATS_AVG("Average time for heap free", sum_free, count);
50+
/* if count is 0, it means that there is not enough memory heap
51+
* to do k_malloc at least once, then it's meaningless to
52+
* calculate average time of memory allocation and free.
53+
*/
54+
if (count == 0) {
55+
printk("Error: there isn't enough memory heap to do "
56+
"k_malloc at least once, please "
57+
"increase heap size\n");
58+
} else {
59+
PRINT_STATS_AVG("Average time for heap malloc", sum_malloc, count);
60+
PRINT_STATS_AVG("Average time for heap free", sum_free, count);
61+
}
5262

5363
timing_stop();
5464
}

0 commit comments

Comments
 (0)