Skip to content
Closed
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
4 changes: 3 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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:
//!
Expand Down
6 changes: 3 additions & 3 deletions src/report.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 7 additions & 6 deletions src/vma.cpp
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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) {
Expand All @@ -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);
}
}
11 changes: 10 additions & 1 deletion tests/to_logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use vulkan_memory_allocator_sys::LOG_TARGET;
mod common;

static LINES: Mutex<Vec<String>> = Mutex::new(Vec::new());
static LINES_AT_FLUSHES: Mutex<Vec<usize>> = Mutex::new(Vec::new());

struct Capture;

Expand All @@ -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]
Expand All @@ -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"
);
}
Loading