Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .references/mimalloc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
b69f9cb
8ff03b6
10 changes: 7 additions & 3 deletions mimalloc/include/mimalloc-stats.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ terms of the MIT license. A copy of the license can be found in the file
#include <mimalloc.h>
#include <stdint.h>

#define MI_STAT_VERSION 3 // increased on every backward incompatible change
#define MI_STAT_VERSION 4 // increased on every backward incompatible change

// count allocation over time
typedef struct mi_stat_count_s {
Expand Down Expand Up @@ -73,7 +73,8 @@ typedef struct mi_stat_counter_s {

typedef struct mi_stats_s
{
int version;
size_t size; // size of the mi_stats_t structure
size_t version;

MI_STAT_FIELDS()

Expand All @@ -89,12 +90,15 @@ typedef struct mi_stats_s
#undef MI_STAT_COUNT
#undef MI_STAT_COUNTER

// helper
#define mi_stats_t_decl(name) mi_stats_t name = { 0 }; name.size = sizeof(mi_stats_t); name.version = MI_STAT_VERSION;

// Exported definitions
#ifdef __cplusplus
extern "C" {
#endif

mi_decl_export void mi_stats_get( size_t stats_size, mi_stats_t* stats ) mi_attr_noexcept;
mi_decl_export bool mi_stats_get( mi_stats_t* stats ) mi_attr_noexcept;
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

#ifdef __cplusplus
Expand Down
10 changes: 5 additions & 5 deletions mimalloc/include/mimalloc/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ terms of the MIT license. A copy of the license can be found in the file
// This file contains the main type definitions for mimalloc:
// mi_heap_t : all data for a thread-local heap, contains
// lists of all managed heap pages.
// mi_segment_t : a larger chunk of memory (32GiB) from where pages
// mi_segment_t : a larger chunk of memory (32MiB on 64-bit) from where pages
// are allocated. A segment is divided in slices (64KiB) from
// which pages are allocated.
// mi_page_t : a "mimalloc" page (usually 64KiB or 512KiB) from
Expand Down Expand Up @@ -63,9 +63,9 @@ terms of the MIT license. A copy of the license can be found in the file
#define MI_SECURE 0
#endif

// Define MI_DEBUG for debug mode
// #define MI_DEBUG 1 // basic assertion checks and statistics, check double free, corrupted free list, and invalid pointer free.
// #define MI_DEBUG 2 // + internal assertion checks
// Define MI_DEBUG for assertion and invariant checking
// #define MI_DEBUG 1 // basic assertion checks and statistics, check double free, corrupted free list, and invalid pointer free. (cmake -DMI_DEBUG=ON)
// #define MI_DEBUG 2 // + internal assertion checks (cmake -DMI_DEBUG_INTERNAL=ON)
// #define MI_DEBUG 3 // + extensive internal invariant checking (cmake -DMI_DEBUG_FULL=ON)
#if !defined(MI_DEBUG)
#if defined(MI_BUILD_RELEASE) || defined(NDEBUG)
Expand Down Expand Up @@ -194,7 +194,7 @@ typedef int32_t mi_ssize_t;
#define MI_SEGMENT_ALIGN MI_SEGMENT_SIZE
#define MI_SEGMENT_MASK ((uintptr_t)(MI_SEGMENT_ALIGN - 1))
#define MI_SEGMENT_SLICE_SIZE (MI_ZU(1)<< MI_SEGMENT_SLICE_SHIFT)
#define MI_SLICES_PER_SEGMENT (MI_SEGMENT_SIZE / MI_SEGMENT_SLICE_SIZE) // 1024
#define MI_SLICES_PER_SEGMENT (MI_SEGMENT_SIZE / MI_SEGMENT_SLICE_SIZE) // 512 (128 on 32-bit)

#define MI_SMALL_PAGE_SIZE (MI_ZU(1)<<MI_SMALL_PAGE_SHIFT)
#define MI_MEDIUM_PAGE_SIZE (MI_ZU(1)<<MI_MEDIUM_PAGE_SHIFT)
Expand Down
6 changes: 3 additions & 3 deletions mimalloc/src/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ mi_decl_cache_align static const mi_tld_t tld_empty = {
false,
NULL, NULL,
{ MI_SEGMENT_SPAN_QUEUES_EMPTY, 0, 0, 0, 0, 0, &mi_subproc_default, tld_empty_stats }, // segments
{ MI_STAT_VERSION, MI_STATS_NULL } // stats
{ sizeof(mi_stats_t), MI_STAT_VERSION, MI_STATS_NULL } // stats
};

mi_threadid_t _mi_thread_id(void) mi_attr_noexcept {
Expand All @@ -155,7 +155,7 @@ static mi_decl_cache_align mi_tld_t tld_main = {
0, false,
&_mi_heap_main, & _mi_heap_main,
{ MI_SEGMENT_SPAN_QUEUES_EMPTY, 0, 0, 0, 0, 0, &mi_subproc_default, &tld_main.stats }, // segments
{ MI_STAT_VERSION, MI_STATS_NULL } // stats
{ sizeof(mi_stats_t), MI_STAT_VERSION, MI_STATS_NULL } // stats
};

mi_decl_cache_align mi_heap_t _mi_heap_main = {
Expand All @@ -181,7 +181,7 @@ mi_decl_cache_align mi_heap_t _mi_heap_main = {

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

mi_stats_t _mi_stats_main = { MI_STAT_VERSION, MI_STATS_NULL };
mi_stats_t _mi_stats_main = { sizeof(mi_stats_t), MI_STAT_VERSION, MI_STATS_NULL };

#if MI_GUARDED
mi_decl_export void mi_heap_guarded_set_sample_rate(mi_heap_t* heap, size_t sample_rate, size_t seed) {
Expand Down
22 changes: 14 additions & 8 deletions mimalloc/src/stats.c
Original file line number Diff line number Diff line change
Expand Up @@ -476,12 +476,11 @@ mi_decl_export void mi_process_info(size_t* elapsed_msecs, size_t* user_msecs, s
// Return statistics
// --------------------------------------------------------

void mi_stats_get(size_t stats_size, mi_stats_t* stats) mi_attr_noexcept {
if (stats == NULL || stats_size == 0) return;
_mi_memzero(stats, stats_size);
const size_t size = (stats_size > sizeof(mi_stats_t) ? sizeof(mi_stats_t) : stats_size);
_mi_memcpy(stats, &_mi_stats_main, size);
stats->version = MI_STAT_VERSION;
bool mi_stats_get(mi_stats_t* stats) mi_attr_noexcept {
if (stats == NULL || stats->size != sizeof(mi_stats_t) || stats->version != MI_STAT_VERSION) return false;
_mi_memzero(stats,stats->size);
_mi_memcpy(stats, &_mi_stats_main, sizeof(mi_stats_t));
return true;
}


Expand Down Expand Up @@ -589,7 +588,7 @@ char* mi_stats_get_json(size_t output_size, char* output_buf) mi_attr_noexcept {
if (!mi_heap_buf_expand(&hbuf)) return NULL;
}
mi_heap_buf_print(&hbuf, "{\n");
mi_heap_buf_print_value(&hbuf, "version", MI_STAT_VERSION);
mi_heap_buf_print_value(&hbuf, "stat_version", MI_STAT_VERSION);
mi_heap_buf_print_value(&hbuf, "mimalloc_version", MI_MALLOC_VERSION);

// process info
Expand Down Expand Up @@ -629,5 +628,12 @@ char* mi_stats_get_json(size_t output_size, char* output_buf) mi_attr_noexcept {
}
mi_heap_buf_print(&hbuf, " ]\n");
mi_heap_buf_print(&hbuf, "}\n");
return hbuf.buf;
if (hbuf.used >= hbuf.size) {
// failed
if (hbuf.can_realloc) { mi_free(hbuf.buf); }
return NULL;
}
else {
return hbuf.buf;
}
}