From b490ff58838151c2cab62eaf6b269232121656cb Mon Sep 17 00:00:00 2001 From: Stuart Parmenter Date: Wed, 4 Nov 2026 08:42:08 -0800 Subject: [PATCH] Flush the logger after each leak report --- src/lib.rs | 4 +++- src/report.rs | 6 +++--- src/vma.cpp | 13 +++++++------ tests/to_logger.rs | 11 ++++++++++- 4 files changed, 23 insertions(+), 11 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index d6734ab..392a626 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -24,7 +24,9 @@ //! Leak reports are always on. Each one goes to `log`, or to stderr if `log_enabled!` is false for //! [`LOG_TARGET`]. `log` can't tell a missing logger from a filter, so filtering out the target //! moves leak reports to stderr rather than silencing them. A logger that accepts a report and -//! then drops it loses it. +//! then drops it loses it. The logger is flushed after every line below except the per-allocation +//! lines of a block, which its closing line flushes. Reports of memory still mapped when it's +//! freed flush once per allocation, with VMA's locks held. //! //! Each leak report is one line starting `VMA leak: `, cut off at 1023 bytes. They say: //! diff --git a/src/report.rs b/src/report.rs index c518461..83ec5cd 100644 --- a/src/report.rs +++ b/src/report.rs @@ -22,21 +22,21 @@ impl fmt::Display for Lossy<'_> { } } -/// Logs `message`, and flushes the logger if `fatal` isn't 0. Returns 0 if the logger doesn't +/// Logs `message`, and flushes the logger if `flush` isn't 0. Returns 0 if the logger doesn't /// want it. /// /// # Safety /// /// `message` is NUL-terminated. #[unsafe(no_mangle)] -unsafe extern "C" fn vma_sys_log(message: *const c_char, fatal: c_int) -> c_int { +unsafe extern "C" fn vma_sys_log(message: *const c_char, flush: c_int) -> c_int { if !log::log_enabled!(target: LOG_TARGET, Level::Error) { return 0; } // SAFETY: the caller passes a NUL-terminated string. let message = unsafe { CStr::from_ptr(message) }.to_bytes(); log::error!(target: LOG_TARGET, "{}", Lossy(message)); - if fatal != 0 { + if flush != 0 { log::logger().flush(); } 1 diff --git a/src/vma.cpp b/src/vma.cpp index c67ba6a..d857fe8 100644 --- a/src/vma.cpp +++ b/src/vma.cpp @@ -1,15 +1,16 @@ // VMA's implementation. Failed checks and leaks go to `log` and stderr, so no C runtime dialog // opens. static void vma_sys_check_failed(const char* expr, const char* file, int line); -static void vma_sys_leak(const char* format, ...); +static void vma_sys_leak(int flush, const char* format, ...); #ifdef VMA_SYS_CHECKS #define VMA_ASSERT(expr) ((expr) ? (void)0 : vma_sys_check_failed(#expr, __FILE__, __LINE__)) #else #define VMA_ASSERT(expr) #endif -#define VMA_ASSERT_LEAK(expr) ((expr) ? (void)0 : vma_sys_leak("VMA leak: %s", #expr)) -#define VMA_LEAK_LOG_FORMAT(format, ...) vma_sys_leak("VMA leak: " format, __VA_ARGS__) +// A block's listing comes first, so the summary is where a report ends. +#define VMA_ASSERT_LEAK(expr) ((expr) ? (void)0 : vma_sys_leak(1, "VMA leak: %s", #expr)) +#define VMA_LEAK_LOG_FORMAT(format, ...) vma_sys_leak(0, "VMA leak: " format, __VA_ARGS__) #define VMA_IMPLEMENTATION #include "vma.h" @@ -25,7 +26,7 @@ extern "C" #ifdef __GNUC__ __attribute__((visibility("hidden"))) #endif -int vma_sys_log(const char* message, int fatal); +int vma_sys_log(const char* message, int flush); // A logger can drop or hold a message, so stderr comes first. static void vma_sys_check_failed(const char* expr, const char* file, int line) { @@ -37,13 +38,13 @@ static void vma_sys_check_failed(const char* expr, const char* file, int line) { std::abort(); } -static void vma_sys_leak(const char* format, ...) { +static void vma_sys_leak(int flush, const char* format, ...) { char message[1024]; std::va_list args; va_start(args, format); std::vsnprintf(message, sizeof message, format, args); va_end(args); - if (!vma_sys_log(message, 0)) { + if (!vma_sys_log(message, flush)) { std::fprintf(stderr, "%s\n", message); } } diff --git a/tests/to_logger.rs b/tests/to_logger.rs index 200caba..de15612 100644 --- a/tests/to_logger.rs +++ b/tests/to_logger.rs @@ -8,6 +8,7 @@ use vulkan_memory_allocator_sys::LOG_TARGET; mod common; static LINES: Mutex> = Mutex::new(Vec::new()); +static LINES_AT_FLUSHES: Mutex> = Mutex::new(Vec::new()); struct Capture; @@ -20,7 +21,10 @@ impl Log for Capture { LINES.lock().unwrap().push(record.args().to_string()); } - fn flush(&self) {} + fn flush(&self) { + let lines = LINES.lock().unwrap().len(); + LINES_AT_FLUSHES.lock().unwrap().push(lines); + } } #[test] @@ -37,4 +41,9 @@ fn a_leak_reaches_the_logger_with_its_offset_and_size() { "{lines:?}" ); assert!(lines[1].starts_with("VMA leak: "), "{lines:?}"); + assert_eq!( + *LINES_AT_FLUSHES.lock().unwrap(), + [2], + "the logger is flushed once, after the last line" + ); }