Skip to content

Commit 03a69ef

Browse files
committed
add size field to stats and modify mi_stats_get to check the version
1 parent 19a6913 commit 03a69ef

File tree

3 files changed

+19
-14
lines changed

3 files changed

+19
-14
lines changed

include/mimalloc-stats.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ terms of the MIT license. A copy of the license can be found in the file
1111
#include <mimalloc.h>
1212
#include <stdint.h>
1313

14-
#define MI_STAT_VERSION 3 // increased on every backward incompatible change
14+
#define MI_STAT_VERSION 4 // increased on every backward incompatible change
1515

1616
// count allocation over time
1717
typedef struct mi_stat_count_s {
@@ -73,7 +73,8 @@ typedef struct mi_stat_counter_s {
7373

7474
typedef struct mi_stats_s
7575
{
76-
int version;
76+
size_t size; // size of the mi_stats_t structure
77+
size_t version;
7778

7879
MI_STAT_FIELDS()
7980

@@ -94,7 +95,7 @@ typedef struct mi_stats_s
9495
extern "C" {
9596
#endif
9697

97-
mi_decl_export bool mi_stats_get( size_t stats_size, mi_stats_t* stats ) mi_attr_noexcept;
98+
mi_decl_export bool mi_stats_get( mi_stats_t* stats ) mi_attr_noexcept;
9899
mi_decl_export char* mi_stats_get_json( size_t buf_size, char* buf ) mi_attr_noexcept; // use mi_free to free the result if the input buf == NULL
99100

100101
#ifdef __cplusplus

src/init.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ static mi_decl_cache_align mi_tld_t tld_main = {
136136
0, 0, 0, 0, 0, &mi_subproc_default,
137137
&tld_main.stats
138138
}, // segments
139-
{ MI_STAT_VERSION, MI_STATS_NULL } // stats
139+
{ sizeof(mi_stats_t), MI_STAT_VERSION, MI_STATS_NULL } // stats
140140
};
141141

142142
mi_decl_cache_align mi_heap_t _mi_heap_main = {
@@ -162,7 +162,7 @@ mi_decl_cache_align mi_heap_t _mi_heap_main = {
162162

163163
bool _mi_process_is_initialized = false; // set to `true` in `mi_process_init`.
164164

165-
mi_stats_t _mi_stats_main = { MI_STAT_VERSION, MI_STATS_NULL };
165+
mi_stats_t _mi_stats_main = { sizeof(mi_stats_t), MI_STAT_VERSION, MI_STATS_NULL };
166166

167167
#if MI_GUARDED
168168
mi_decl_export void mi_heap_guarded_set_sample_rate(mi_heap_t* heap, size_t sample_rate, size_t seed) {

src/stats.c

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -474,13 +474,10 @@ mi_decl_export void mi_process_info(size_t* elapsed_msecs, size_t* user_msecs, s
474474
// Return statistics
475475
// --------------------------------------------------------
476476

477-
bool mi_stats_get(size_t stats_size, mi_stats_t* stats) mi_attr_noexcept {
478-
if (stats == NULL || stats_size == 0) return false;
479-
_mi_memzero(stats, stats_size);
480-
if (stats_size < sizeof(mi_stats_t)) return false;
481-
const size_t size = (stats_size > sizeof(mi_stats_t) ? sizeof(mi_stats_t) : stats_size);
482-
_mi_memcpy(stats, &_mi_stats_main, size);
483-
stats->version = MI_STAT_VERSION;
477+
bool mi_stats_get(mi_stats_t* stats) mi_attr_noexcept {
478+
if (stats == NULL || stats->size != sizeof(mi_stats_t) || stats->version != MI_STAT_VERSION) return false;
479+
_mi_memzero(stats,stats->size);
480+
_mi_memcpy(stats, &_mi_stats_main, sizeof(mi_stats_t));
484481
return true;
485482
}
486483

@@ -584,7 +581,7 @@ char* mi_stats_get_json(size_t output_size, char* output_buf) mi_attr_noexcept {
584581
if (!mi_heap_buf_expand(&hbuf)) return NULL;
585582
}
586583
mi_heap_buf_print(&hbuf, "{\n");
587-
mi_heap_buf_print_value(&hbuf, "version", MI_STAT_VERSION);
584+
mi_heap_buf_print_value(&hbuf, "stat_version", MI_STAT_VERSION);
588585
mi_heap_buf_print_value(&hbuf, "mimalloc_version", MI_MALLOC_VERSION);
589586

590587
// process info
@@ -624,5 +621,12 @@ char* mi_stats_get_json(size_t output_size, char* output_buf) mi_attr_noexcept {
624621
}
625622
mi_heap_buf_print(&hbuf, " ]\n");
626623
mi_heap_buf_print(&hbuf, "}\n");
627-
return hbuf.buf;
624+
if (hbuf.used >= hbuf.size) {
625+
// failed
626+
if (hbuf.can_realloc) { mi_free(hbuf.buf); }
627+
return NULL;
628+
}
629+
else {
630+
return hbuf.buf;
631+
}
628632
}

0 commit comments

Comments
 (0)