Skip to content

Commit 7e36040

Browse files
committed
Eliminate code duplication in memory statistics printing
JerryScript-DCO-1.0-Signed-off-by: Akos Kiss [email protected]
1 parent 0219f37 commit 7e36040

File tree

3 files changed

+34
-39
lines changed

3 files changed

+34
-39
lines changed

jerry-core/mem/mem-allocator.cpp

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -51,29 +51,14 @@ mem_finalize (bool is_show_mem_stats) /**< show heap memory stats
5151
{
5252
mem_pools_finalize ();
5353

54+
#ifdef MEM_STATS
5455
if (is_show_mem_stats)
5556
{
56-
mem_heap_print (false, false, true);
57-
58-
#ifdef MEM_STATS
59-
mem_pools_stats_t stats;
60-
mem_pools_get_stats (&stats);
61-
62-
printf ("Pools stats:\n");
63-
printf (" Chunk size: %zu\n"
64-
" Pools: %zu\n"
65-
" Allocated chunks: %zu\n"
66-
" Free chunks: %zu\n"
67-
" Peak pools: %zu\n"
68-
" Peak allocated chunks: %zu\n\n",
69-
MEM_POOL_CHUNK_SIZE,
70-
stats.pools_count,
71-
stats.allocated_chunks,
72-
stats.free_chunks,
73-
stats.peak_pools_count,
74-
stats.peak_allocated_chunks);
75-
#endif /* MEM_STATS */
57+
mem_stats_print ();
7658
}
59+
#else /* MEM_STATS */
60+
(void) is_show_mem_stats;
61+
#endif /* !MEM_STATS */
7762

7863
mem_heap_finalize ();
7964
} /* mem_finalize */
@@ -158,13 +143,13 @@ mem_stats_reset_peak (void)
158143
void
159144
mem_stats_print (void)
160145
{
161-
mem_heap_print (false, false, true);
146+
mem_heap_stats_print ();
162147

163148
mem_pools_stats_t stats;
164149
mem_pools_get_stats (&stats);
165150

166151
printf ("Pools stats:\n");
167-
printf (" Chunk size: %zu\n"
152+
printf (" Chunk size: %zu\n"
168153
" Pools: %zu\n"
169154
" Allocated chunks: %zu\n"
170155
" Free chunks: %zu\n"

jerry-core/mem/mem-heap.cpp

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -944,23 +944,7 @@ mem_heap_print (bool dump_block_headers, /**< print block headers */
944944
#ifdef MEM_STATS
945945
if (dump_stats)
946946
{
947-
printf ("Heap stats:\n");
948-
printf (" Heap size = %zu bytes\n"
949-
" Chunk size = %zu bytes\n"
950-
" Allocated chunks count = %zu\n"
951-
" Allocated = %zu bytes\n"
952-
" Waste = %zu bytes\n"
953-
" Peak allocated chunks count = %zu\n"
954-
" Peak allocated = %zu bytes\n"
955-
" Peak waste = %zu bytes\n",
956-
mem_heap_stats.size,
957-
MEM_HEAP_CHUNK_SIZE,
958-
mem_heap_stats.allocated_chunks,
959-
mem_heap_stats.allocated_bytes,
960-
mem_heap_stats.waste_bytes,
961-
mem_heap_stats.peak_allocated_chunks,
962-
mem_heap_stats.peak_allocated_bytes,
963-
mem_heap_stats.peak_waste_bytes);
947+
mem_heap_stats_print ();
964948
}
965949
#else /* MEM_STATS */
966950
(void) dump_stats;
@@ -1108,6 +1092,31 @@ mem_heap_stat_free (size_t first_chunk_index, /**< first chunk of the freed area
11081092
mem_heap_stats.allocated_bytes -= bytes;
11091093
mem_heap_stats.waste_bytes -= waste_bytes;
11101094
} /* mem_heap_stat_free */
1095+
1096+
/**
1097+
* Print heap statistics
1098+
*/
1099+
void
1100+
mem_heap_stats_print (void)
1101+
{
1102+
printf ("Heap stats:\n");
1103+
printf (" Heap size = %zu bytes\n"
1104+
" Chunk size = %zu bytes\n"
1105+
" Allocated chunks count = %zu\n"
1106+
" Allocated = %zu bytes\n"
1107+
" Waste = %zu bytes\n"
1108+
" Peak allocated chunks count = %zu\n"
1109+
" Peak allocated = %zu bytes\n"
1110+
" Peak waste = %zu bytes\n",
1111+
mem_heap_stats.size,
1112+
MEM_HEAP_CHUNK_SIZE,
1113+
mem_heap_stats.allocated_chunks,
1114+
mem_heap_stats.allocated_bytes,
1115+
mem_heap_stats.waste_bytes,
1116+
mem_heap_stats.peak_allocated_chunks,
1117+
mem_heap_stats.peak_allocated_bytes,
1118+
mem_heap_stats.peak_waste_bytes);
1119+
} /* mem_heap_stats_print */
11111120
#endif /* MEM_STATS */
11121121

11131122
/**

jerry-core/mem/mem-heap.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ typedef struct
8181

8282
extern void mem_heap_get_stats (mem_heap_stats_t *);
8383
extern void mem_heap_stats_reset_peak (void);
84+
extern void mem_heap_stats_print (void);
8485
#endif /* MEM_STATS */
8586

8687
#ifdef JERRY_VALGRIND_FREYA

0 commit comments

Comments
 (0)