#allocator #panic #assert #print #format #global-allocator

nightly no-std bin+lib alloc-fmt

Formatting utilities safe for use in an allocator

2 unstable releases

Uses old Rust 2015

0.2.0 Apr 4, 2020
0.1.0 Oct 20, 2017

#410 in Memory management


Used in alloc-tls

Apache-2.0/MIT

22KB
374 lines

alloc-fmt

alloc-fmt provides formatting and assertion macros similar to println, eprintln, panic, assert, debug_assert, etc which are safe for use in a global allocator. The standard library's formatting, panic, and assertion macros can allocate, meaning that if they are used in the implementation of a global allocator, it can cause infinite recursion. The macros in this crate do not allocate in order to avoid this problem.


lib.rs:

Allocator-safe formatting and assertion macros.

alloc-fmt provides formatting and assertion macros similar to the standard library's println, eprintln, panic, assert, debug_assert, etc which are safe for use in a global allocator. The standard library's formatting and assertion macros can allocate, meaning that if they are used in the implementation of a global allocator, it can cause infinite recursion. The macros in this crate avoid this problem by either not allocating (in the case of formatting macros) or detecting recursion (in the case of panic and assertion macros).

Usage and Behavior

The macros in this crate are named alloc_xxx, where xxx is the name of the equivalent standard library macro (e.g., alloc_println, alloc_debug_assert, etc).

The behavior of the formatting macros is identical to the behavior of their standard library counterparts. The behavior of the panic and assertion macros is somewhat different. When an assertion fails or an explicit panic is invoked, a message is first unconditionally printed to stderr (in case further processing causes a crash). A stack trace is then printed, and the process aborts. If recursion is detected during the printing of the stack trace, the process immediately aborts. Recusion can happen if, for example, the code that computes the stack trace allocates, triggering further assertion failures or panics. This check is conservative - it may sometimes detect recursion when there is none.

Unlike the standard library assertion and panic macros, the stack is not unwound, and once an assertion failure or panic triggers, it cannot be caught or aborted.

Dependencies

~0.2–5MB
~14K SLoC