alloc_counter
Alloc counters
A redesign of the Quick and Dirty Allocation Profiling Tool.
Features
-
Although
#[no_std]is intended, it isn't currently supported due to the lack of portablethread_localimplementation. This may be "fixed" with a feature flag to use a global atomic under the contract that the program is single threaded. -
Count allocations, reallocations and deallocations individually with
count_alloc. -
Allow, deny, and forbid use of the global allocator with
allow_alloc,deny_allocandforbid_alloc. -
#[no_alloc]function attribute to deny and#[no_alloc(forbid)]to forbid use of the global allocator. -
#[count_alloc]function attribute to print the counts to stderr. Alternatively use#[count_alloc(func = "my_function")]wheremy_functionaccepts a triple ofusizes and returns()to redirect the output.
Limitations and known issues
-
Methods must either take a reference to
selforSelfmust be aCopytype. -
Ordinary and async functions must be treated differently. Use
count_allocfor functions andcount_alloc_futurefor futures.
Usage
An AllocCounter<A> wraps an allocator A to individually count the number of calls to
alloc, realloc, and dealloc.
use AllocCounter;
type MyAllocator = System;
const MyAllocator: MyAllocator = System;
static A: = AllocCounter;
Std-users may prefer to inherit their system's allocator.
use AllocCounterSystem;
static A: AllocCounterSystem = AllocCounterSystem;
To count the allocations of an expression, use count_alloc.
# use ;
#
# static A: AllocCounterSystem = AllocCounterSystem;
let = count_alloc;
assert_eq!;
To deny allocations for an expression use deny_alloc.
# use ;
#
# static A: AllocCounterSystem = AllocCounterSystem;
foo;
Similar to Rust's lints, you can still allow allocation inside a deny block.
# use ;
#
# static A: AllocCounterSystem = AllocCounterSystem;
foo;
Forbidding allocations forces a panic even when allow_alloc is used.
# use ;
#
# static A: AllocCounterSystem = AllocCounterSystem;
foo;
For added sugar you may use the #[no_alloc] attribute on functions, including methods with
self-binds. #[no_alloc] expands to calling deny_alloc and forcefully moves the parameters
into the checked block. #[no_alloc(forbid)] calls forbid_alloc.
#
# panic!;
#
License: MIT OR Apache-2.0