Atomic per-device allocation counter bank.
All counters live in static storage, one bank per Device.
Updates use std::atomic so track_alloc / track_free
can be invoked from any thread without external locking. Peak
tracking uses a compare-exchange loop, avoiding a mutex while still
guaranteeing a monotonically non-decreasing peak.
Notes
All public methods are static — the class is effectively a
namespace over the two global counter banks. There is no instance
state.
Thread Safety
Counter updates are atomic. Snapshots returned by
get_stats are eventually consistent — a get_stats call
during concurrent allocation may observe current_bytes slightly
behind the true total or a peak that lags the true maximum by one
CAS cycle.
Static methods
5Returns the live Counters bank for device.
Implementation detail — the two banks live in static storage
inside the .cpp file's anonymous namespace.
Parameters
deviceDeviceReturns
Counters&Reference to the static bank for the requested device.
MemoryStats get_stats(Device device)Returns a coherent-looking snapshot of one device's counters.
Each field is read separately with memory_order_relaxed, so
the four values may not correspond to the same instant under
concurrent activity. Sufficient for diagnostics; not safe to
base correctness invariants on.
Parameters
deviceDeviceReturns
MemoryStatsValue-typed snapshot disconnected from the live atomics.
Resets peak_bytes to the current current_bytes value.
Useful at the start of a profiling window — after this call,
peak_bytes reports the maximum live byte total reached since
the reset rather than since process start.
Parameters
deviceDeviceNotes
Mirrors the reference framework's reset_peak_memory_stats for
API parity.
Records an allocation of nbytes on device.
Adds nbytes to current_bytes, increments alloc_count,
and — if the new current_bytes exceeds the previous peak —
advances peak_bytes via a relaxed compare-exchange loop.
Parameters
nbytesstd::size_tdeviceDeviceNotes
Called from the allocator's deleter; not for direct application use.
Records a deallocation of nbytes on device.
Subtracts nbytes from current_bytes and increments
free_count. Does not touch peak_bytes — peaks are
historical, by definition.
Parameters
nbytesstd::size_ttrack_alloc.deviceDevice