Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
82 commits
Select commit Hold shift + click to select a range
7ca4485
d3d12: an image made for slot-70 views holds no device reference, so …
adrianmjim Nov 28, 2026
f28d6cf
d3d12_xs: PSO streams with equal shader bytecode share one copy (RT-P…
adrianmjim Nov 28, 2026
5fa9c20
d3d12: upload memory comes back at its command allocator's Reset (RT-…
adrianmjim Nov 28, 2026
c7d8306
d3d12: LEONIDA_SHADOW_GPU=1 alone keeps no released ranges for the co…
adrianmjim Nov 28, 2026
096bedb
d3d12: an idle list-cut spare drops its recording and holds back no r…
adrianmjim Nov 28, 2026
a1f7aac
d3d12: a title texture destroyed inside the registry leaves the async…
adrianmjim Nov 28, 2026
51677ea
d3d12: a slot-70 image a newer texture covers, or whose pages went, i…
adrianmjim Nov 28, 2026
e64222c
d3d12: a slot-record bucket goes with the last half it holds (RT-SLOT…
adrianmjim Nov 28, 2026
fc07a0f
mem probe: on native Windows a vmmap line shows the process's memory …
adrianmjim Nov 28, 2026
0da6887
d3d12: a shim's scratch stays alive until the submission that used it…
adrianmjim Nov 28, 2026
9ebbc49
d3d12: the PSOs of one program share its code-base copy in the arena …
adrianmjim Nov 28, 2026
0711e4d
d3d12: a title vertex or index view keeps only a buffer that covers i…
adrianmjim Nov 28, 2026
e95d5fa
d3d12: a shape whose shared zero image could not be made is tried aga…
adrianmjim Nov 28, 2026
cf93f49
d3d12: the kept-scratch test waits until the queue has released what …
adrianmjim Nov 28, 2026
6ce683a
d3d12: no stand-in buffer is made for a refused title view, and refus…
adrianmjim Nov 28, 2026
9d8d111
d3d12: a slot-70 image's pages are looked at only under a change with…
adrianmjim Nov 28, 2026
5f16ee1
d3d12: a shape whose shared zero image keeps failing waits longer at …
adrianmjim Nov 28, 2026
ecc91ed
Merge branch 'leaks/spare-lists-epochs' into leaks/integration
adrianmjim Nov 28, 2026
fb915a0
Merge branch 'leaks/pso-bytecode-intern' into leaks/integration
adrianmjim Nov 28, 2026
780fb24
Merge branch 'leaks/shadow-gpu-released-ranges' into leaks/integration
adrianmjim Nov 28, 2026
b67a89f
Merge branch 'leaks/title-texture-stale-ranges' into leaks/integration
adrianmjim Nov 28, 2026
cff952c
Merge branch 'leaks/slot70-device-cycle' into leaks/integration
adrianmjim Nov 28, 2026
1926773
Merge branch 'leaks/slot70-owned-textures' into leaks/integration
adrianmjim Nov 28, 2026
876887d
d3d12: slot-70 image retirement is opt-in until a game session measur…
adrianmjim Nov 28, 2026
d0d3a0f
Merge branch 'leaks/slot70-owned-textures' into leaks/integration
adrianmjim Nov 28, 2026
3a7a259
Merge branch 'leaks/slot-records-buckets' into leaks/integration
adrianmjim Nov 28, 2026
4f8a41c
Merge branch 'leaks/rehydrate-scratch-ticket' into leaks/integration
adrianmjim Nov 28, 2026
25b866a
Merge branch 'leaks/mem-probe-windows' into leaks/integration
adrianmjim Nov 28, 2026
7121a76
Merge branch 'leaks/fssm-arena-share' into leaks/integration
adrianmjim Nov 28, 2026
6dfab52
Merge branch 'leaks/title-buffer-fallback' into leaks/integration
adrianmjim Nov 28, 2026
38e705d
Merge branch 'leaks/zero-images-retry' into leaks/integration
adrianmjim Nov 28, 2026
1ea7df6
leonida-hle: XMemFreeDefault tells the GPU side of freed 32-bit memor…
adrianmjim Nov 28, 2026
f468d3e
leonida-gpu: the early pipeline list keeps the last two runs (RS-EARL…
adrianmjim Nov 28, 2026
74a89d8
leonida-gpu: replaced host copies go with LEONIDA_TEXTURE_SWEEP=0 too…
adrianmjim Nov 28, 2026
ae6bb29
leonida-gpu: a texture refresh thread keeps at most 16 MiB of read bu…
adrianmjim Nov 28, 2026
b179a98
leonida-gpu: submitted lists hold no host list until reset, idle ones…
adrianmjim Nov 28, 2026
692ca27
leonida-hle: the XTaskQueue thread pool starts few workers in a burst…
adrianmjim Nov 28, 2026
6ab9419
leonida-gpu: snapshot buffers no snapshot took for the idle time leav…
adrianmjim Nov 28, 2026
922ea46
leonida-gpu: title pipelines of one graphics state share it (RS-GRAPH…
adrianmjim Nov 28, 2026
a505e14
leonida-gpu: thin lists give back uploads a reset or release leaves b…
adrianmjim Nov 28, 2026
11c9d1a
leonida-hle: XApu decodes each packet into memory the connection keep…
adrianmjim Nov 28, 2026
ff0b728
leonida-gpu: spare buffers of view copies are capped at 64 MiB and go…
adrianmjim Nov 28, 2026
5e07de7
leonida-hle: idle pool workers leave even while tasks keep coming (RS…
adrianmjim Nov 28, 2026
8e1bf0b
leonida-hle: a pool worker that cannot be started leaves the pool abl…
adrianmjim Nov 28, 2026
8ebf6c0
leonida-gpu: reads of textures over 16 MiB reuse buffers from a share…
adrianmjim Nov 28, 2026
8f0d83d
leonida-gpu: idle snapshot buffers are freed on the reaper thread, no…
adrianmjim Nov 28, 2026
b421086
leonida-gpu: the window sweep asks for the RAM idle time once and pas…
adrianmjim Nov 28, 2026
ace7659
leonida-gpu: the window sweep asks for the RAM idle time once and pas…
adrianmjim Nov 28, 2026
d48f6b4
leonida-gpu: a full spare list gives up its oldest buffer, released o…
adrianmjim Nov 28, 2026
4b8af77
leonida-gpu: the texture read pool's comment says what a zeroed alloc…
adrianmjim Nov 28, 2026
158ef71
Merge remote-tracking branch 'origin/main' into leaks/all
adrianmjim Nov 28, 2026
198cef9
d3d12: draws that bind the same views again share one views block (RT…
adrianmjim Nov 28, 2026
98bbaff
d3d12: a geometry range inside a live view is answered from that view…
adrianmjim Nov 28, 2026
f11269f
Merge branch 'leaks/rs-host-list-allocators' into leaks/all
adrianmjim Nov 28, 2026
7b52a34
Merge branch 'leaks/rs-xtaskqueue-pool-trim' into leaks/all
adrianmjim Nov 28, 2026
54f3bf9
Merge branch 'leaks/rs-texture-read-trim' into leaks/all
adrianmjim Nov 28, 2026
e1bca19
Merge branch 'leaks/rs-snapshot-pool-trim' into leaks/all
adrianmjim Nov 28, 2026
ecbaa3f
Merge branch 'leaks/rs-exact-spare-cap' into leaks/all
adrianmjim Nov 28, 2026
1a34901
Merge branch 'leaks/rs-graphics-state-intern' into leaks/all
adrianmjim Nov 28, 2026
5d87f84
Merge branch 'leaks/rs-early-start-age' into leaks/all
adrianmjim Nov 28, 2026
f40aefe
Merge branch 'leaks/rs-xmem-free-default' into leaks/all
adrianmjim Nov 28, 2026
4763734
Merge branch 'leaks/rs-thin-upload-drop' into leaks/all
adrianmjim Nov 28, 2026
7751a34
Merge branch 'leaks/rs-apu-scratch' into leaks/all
adrianmjim Nov 28, 2026
f4cc2b3
Merge branch 'leaks/rs-replaced-drain' into leaks/all
adrianmjim Nov 28, 2026
098342d
Merge remote-tracking branch 'origin/main' into leaks/all
adrianmjim Nov 28, 2026
5893834
d3d12: a pinned geometry view keeps its written tiles; a contained ra…
adrianmjim Nov 28, 2026
cd36679
Merge branch 'leaks/geometry-views-contain' into leaks/all
adrianmjim Nov 28, 2026
9fc3ad4
Merge branch 'leaks/draw-views-unchanged' into leaks/all
adrianmjim Nov 28, 2026
46f3cdd
vulkan: after a device loss, released objects are destroyed at once (…
adrianmjim Nov 28, 2026
4b584bd
leonida-hle: DirectStorage workers reuse their request buffers (RS-DS…
adrianmjim Nov 28, 2026
30b0e9d
leonida-gpu: a translated shader lets its container go (RS-SHADER-CON…
adrianmjim Nov 28, 2026
a685db4
leonida-gpu: an arena of memory by address is in the PC's memory once…
adrianmjim Nov 28, 2026
8960d41
vulkan: image views wait with their bindless slot after a device loss…
adrianmjim Nov 28, 2026
defc55e
vulkan: a batch that takes the tracker lock after a device loss stops…
adrianmjim Nov 28, 2026
04d2c93
leonida-gpu: an arena's take finds the changed dwords only where they…
adrianmjim Nov 28, 2026
d1fc186
leonida-gpu: an opened arena's first send and its race are where they…
adrianmjim Nov 28, 2026
abdb93f
Merge main d48a957bb into leaks/all
adrianmjim Nov 28, 2026
95aa081
Merge branch 'leaks/rs-arena-one-copy' into leaks/all
adrianmjim Nov 28, 2026
10e88c8
Merge branch 'leaks/rs-shader-container-free' into leaks/all
adrianmjim Nov 28, 2026
1dac80e
Merge branch 'leaks/rs-dstorage-scratch' into leaks/all
adrianmjim Nov 28, 2026
32d2020
Merge branch 'leaks/device-lost-drain' into leaks/all
adrianmjim Nov 28, 2026
eb145de
leonida-hle: the tests that count allocations share one allocator
adrianmjim Nov 28, 2026
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 d3d12/apps/leonida/src/warm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ pub fn run(jobs: Option<usize>, limit: Option<usize>, again: bool, used: bool, t
let bad = std::fs::read_to_string(&bad_file).unwrap_or_default();
let bad: HashSet<&str> = bad.lines().filter_map(|l| l.split(' ').next()).collect();
let early = std::fs::read_to_string(dir.join("early-pipelines.txt")).unwrap_or_default();
let early: HashSet<&str> = early.lines().collect();
// A key, then the run that last drew with it (a list of an older build
// has the key alone).
let early: HashSet<&str> = early.lines().filter_map(|l| l.split(' ').next()).collect();
// Those earlier runs drew with first, the others in no order (by key).
let mut all: Vec<usize> = (0..keys.len())
.filter(|&n| {
Expand Down
3 changes: 2 additions & 1 deletion d3d12/crates/leonida-gpu/src/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ pub(crate) fn references(object: &impl Interface) -> u32 {

/// Once a second, before a submission starts: lets go of what nothing
/// names any more (copies of windows of title memory, staging buffers,
/// host copies of textures the title fills).
/// host copies of textures the title fills, idle host lists nobody took).
/// `LEONIDA_SWEEP=0` keeps everything, as before.
fn sweep() {
// The in-place probe and census, if asked for (`LEONIDA_INPLACE_PROBE`).
Expand All @@ -446,6 +446,7 @@ fn sweep() {
trim_spare_backs();
}
crate::pool::room(host().and_then(|h| h.video_memory()));
crate::list::trim_idle();
crate::pipeline::trim_library();
crate::shadow::let_go_idle();
crate::texture::let_go_idle();
Expand Down
18 changes: 14 additions & 4 deletions d3d12/crates/leonida-gpu/src/host/send.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,10 +232,20 @@ pub(super) fn take(host: &Host, dirty: &[(std::sync::Arc<Shadow>, Vec<(usize, us
let mut to = to as *mut [u32; 2];
for &(a, b) in ranges.iter().filter(|r| short(r)) {
for offset in (a..b).step_by(4) {
// SAFETY: inside the copy's mapping (its CPU side, which
// the thread that asked, and waits, alone writes in these
// ranges) and inside the memory, which has room for every
// pair, a piece of work's behind the last one's.
// SAFETY: inside the copy's mapping (its CPU side) and
// inside the memory, which has room for every pair, a piece
// of work's behind the last one's. The thread that asked,
// and waits, alone writes in these ranges of a CPU side,
// but for a side that is memory of ours (`Shadow::opened`,
// an arena of `crate::memory`'s): there a recording thread
// may write a word while it is read here, or while
// `copy_all` below reads its run. Two plain accesses at
// once are a race the language leaves undefined. What the
// processor reads is the old bytes or the new ones, and
// the writer resends what it wrote after it wrote it, so
// the next submission sends the new ones. (Before, a
// refresh read the arena through the kernel while it was
// written, with the same outcome.)
unsafe {
let word = ((from + offset) as *const u32).read_unaligned();
to.write([offset as u32, word]);
Expand Down
229 changes: 187 additions & 42 deletions d3d12/crates/leonida-gpu/src/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,25 +218,73 @@ impl Detached {
}
}

/// A host list that has run and waits to be used again, and the bytes of
/// upload memory it keeps.
/// A host list that has run and waits to be used again.
#[cfg(windows)]
struct Idle(crate::host::HostList, u64);
struct Idle(crate::host::HostList);

// SAFETY: D3D12 objects are free-threaded, and an idle list is touched by
// nobody until it is taken out again.
#[cfg(windows)]
unsafe impl Send for Idle {}

/// Things kept to be used again, the oldest first, each with the bytes of
/// upload memory it keeps.
#[cfg(windows)]
struct Pool<T> {
kept: Vec<(T, u64)>,
bytes: u64,
/// The fewest kept at any time since the last [`Pool::trim`]: the
/// oldest that many were not taken since then.
low: usize,
}

#[cfg(windows)]
impl<T> Pool<T> {
const fn new() -> Self {
Pool { kept: Vec::new(), bytes: 0, low: 0 }
}

/// The thing given last.
fn take(&mut self) -> Option<T> {
let (thing, bytes) = self.kept.pop()?;
self.bytes -= bytes;
self.low = self.low.min(self.kept.len());
Some(thing)
}

/// Keeps `thing`, which keeps `bytes`, unless that takes the pool past
/// `most` things or bytes: then it comes back.
fn give(&mut self, thing: T, bytes: u64, most: (usize, u64)) -> Option<T> {
if self.kept.len() >= most.0 || self.bytes + bytes > most.1 {
return Some(thing);
}
self.bytes += bytes;
self.kept.push((thing, bytes));
None
}

/// Takes out the oldest half (rounded up) of the things nobody took
/// since the last trim, so that what is kept follows what was needed
/// lately. Halving rather than taking out all of them keeps a need that
/// comes and goes from making things anew every time.
fn trim(&mut self) -> Vec<(T, u64)> {
let gone: Vec<(T, u64)> = self.kept.drain(..self.low.div_ceil(2)).collect();
self.bytes -= gone.iter().map(|(_, bytes)| bytes).sum::<u64>();
self.low = self.kept.len();
gone
}
}

/// Host lists that have run, reset and ready to record: the host side of
/// a list a queue held, or of a part of a list cut at a wait, is taken
/// from the title's list at its submission, and the title's list needs
/// another. Each was made new and thrown away after its run: a hundred a
/// frame in gameplay, each two command allocators and two lists, and for
/// four in ten a budget and an argument buffer (run 61: "104 host lists
/// made 21.2 ms" a frame, and the graphics kernel's time to free them).
/// from the title's list at its submission, and the title's list takes
/// another at its next reset. Each was made new and thrown away after its
/// run: a hundred a frame in gameplay, each two command allocators and two
/// lists, and for four in ten a budget and an argument buffer (run 61:
/// "104 host lists made 21.2 ms" a frame, and the graphics kernel's time to
/// free them).
#[cfg(windows)]
static IDLE: std::sync::Mutex<(Vec<Idle>, u64)> = std::sync::Mutex::new((Vec::new(), 0));
static IDLE: std::sync::Mutex<Pool<Idle>> = std::sync::Mutex::new(Pool::new());

/// The most host lists kept idle (128 were too few: flights land several at
/// a time, so lists were thrown away and others made in the same frame, with
Expand All @@ -259,20 +307,30 @@ fn pools() -> bool {
/// after its reset, or a new one.
#[cfg(windows)]
fn fresh_list() -> Option<crate::host::HostList> {
let idle = {
let mut idle = IDLE.lock().unwrap_or_else(|e| e.into_inner());
let taken = idle.0.pop();
if let Some(Idle(_, bytes)) = &taken {
idle.1 -= bytes;
}
taken
};
let idle = IDLE.lock().unwrap_or_else(|e| e.into_inner()).take();
match idle {
Some(Idle(list, _)) => Some(list),
Some(Idle(list)) => Some(list),
None => crate::host::host().and_then(|h| h.new_list().ok()),
}
}

/// Lets go of idle host lists nobody took lately ([`Pool::trim`]), once a
/// second (`crate::host::sweep`). A command allocator keeps the memory of
/// the most a list ever recorded into it over its resets, and the pool
/// hands its lists to any title list, so every idle list grows to the size
/// of the largest recording; the pool may keep [`IDLE_MOST`] of them,
/// which a burst of landed flights fills and nothing emptied before.
#[cfg(windows)]
pub(crate) fn trim_idle() {
let gone = IDLE.lock().unwrap_or_else(|e| e.into_inner()).trim();
if !gone.is_empty() {
// Freeing allocators is the driver's work: done where retired
// objects are dropped (the reaper thread), not on the submitting
// thread that sweeps.
crate::host::retire(gone);
}
}

/// The host sides of lists whose submission has run (submissions are
/// waited for; `ran` names the ones it ran, by their place in `lists`):
/// what they held is let go, as before, and the host lists are reset and
Expand All @@ -286,18 +344,16 @@ pub(crate) fn recycle(lists: Vec<Detached>, ran: &[usize]) {
// The descriptor slots and the upload memory its commands named.
drop(_state);
drop(_uploads);
if !ran.contains(&i) || IDLE.lock().unwrap_or_else(|e| e.into_inner()).0.len() >= IDLE_MOST.0 {
if !ran.contains(&i) || IDLE.lock().unwrap_or_else(|e| e.into_inner()).kept.len() >= IDLE_MOST.0 {
continue;
}
// As the title's reset does: what the list kept for its commands
// goes, its allocators stay.
host.reset();
let bytes = host.scratch_kept();
let mut idle = IDLE.lock().unwrap_or_else(|e| e.into_inner());
if idle.0.len() < IDLE_MOST.0 && idle.1 + bytes <= IDLE_MOST.1 {
idle.0.push(Idle(host, bytes));
idle.1 += bytes;
}
let refused = IDLE.lock().unwrap_or_else(|e| e.into_inner()).give(Idle(host), bytes, IDLE_MOST);
// (Dropped outside the lock.)
drop(refused);
}
}

Expand Down Expand Up @@ -379,8 +435,8 @@ pub(crate) fn cut_list(this: This) -> bool {
pub(crate) type Parts = Vec<(Vec<ValueWait>, Vec<Detached>, Vec<usize>)>;

/// Takes the host sides of the submitted `lists`, part by part ([`cut`]),
/// and leaves each list a new, closed host list for its next reset, as
/// [`detach`] does. What comes back is in the order the queue runs it: a
/// and leaves each list without one until its next reset, as [`detach`]
/// does. What comes back is in the order the queue runs it: a
/// new entry starts at every part that has waits; host lists without
/// waits join the entry before them. The list's upload memory and draw
/// state (its dynamic descriptor slots) go with its last part, which runs
Expand Down Expand Up @@ -410,16 +466,11 @@ pub(crate) fn detach_parts(lists: &[This]) -> Parts {
}
continue;
}
let Some(fresh) = fresh_list() else {
continue;
};
// A new host list is open; the title's next call on its list is a
// reset, which wants it closed.
fresh.close();
// SAFETY: one of our lists, closed and submitted: nothing records
// into it until the title resets it.
let list = unsafe { &mut *this.cast_mut().cast::<List>() };
let Some(host) = list.host.replace(fresh) else {
// The list takes another host list at its reset ([`reset`]).
let Some(host) = list.host.take() else {
continue;
};
let earlier = std::mem::take(&mut list.earlier);
Expand Down Expand Up @@ -488,8 +539,8 @@ pub(crate) fn ahead_of_frame() -> Option<Detached> {
}

/// Takes the host sides of submitted lists (each once, however often the
/// submission names it) and leaves each list a new, closed host list for
/// its next reset: what was taken, and which of them run in what order.
/// submission names it) and leaves each list without one until its next
/// reset ([`reset`]): what was taken, and which of them run in what order.
#[cfg(windows)]
pub(crate) fn detach(lists: &[This]) -> (Vec<Detached>, Vec<usize>) {
let mut taken: Vec<(This, Detached)> = Vec::new();
Expand All @@ -502,17 +553,11 @@ pub(crate) fn detach(lists: &[This]) -> (Vec<Detached>, Vec<usize>) {
order.push(i);
continue;
}
let Some(fresh) = fresh_list() else {
continue;
};
// A new host list is open; the title's next call on its list is a
// reset, which wants it closed.
fresh.close();
let list = this.cast_mut().cast::<List>();
// SAFETY: one of our lists, closed and submitted: nothing records
// into it until the title resets it.
let (host, uploads, state) = unsafe {
let Some(host) = (*list).host.replace(fresh) else {
let Some(host) = (*list).host.take() else {
continue;
};
let uploads =
Expand Down Expand Up @@ -711,6 +756,18 @@ extern "system" fn reset(this: This, allocator: This, pipeline: This) -> HRESULT
let list = unsafe { &mut *this.cast_mut().cast::<List>() };
list.earlier.clear();
list.gate.clear();
// A submission took the list's host list ([`detach`]): it takes one
// now. A submitted list used to get a new one at once and keep it
// until this reset, which for a list the title leaves alone for a
// while is a command allocator grown to the size of some other
// list's recording, kept for nothing. Closed, so that its reset
// below is the same as the one the list had before.
if list.host.is_none()
&& let Some(fresh) = fresh_list()
{
fresh.close();
list.host = Some(fresh);
}
}
#[cfg(windows)]
with_state(this, |host, state| {
Expand Down Expand Up @@ -2690,6 +2747,94 @@ mod tests {
assert_eq!(release(allocator), 0);
}

/// A title list that was submitted holds no host list, and so no command
/// allocator grown by what it recorded, until the title resets it. Each
/// used to get a new host list at its submission and keep it while it
/// was idle, so every title list kept one alive besides those in flight
/// and in the pool.
#[cfg(windows)]
#[test]
fn a_submitted_list_keeps_no_host_list_until_its_reset() {
if crate::host::host().is_none() {
eprintln!("no D3D12 device; the host lists of submitted lists are not tested");
return;
}
const LISTS: usize = 64;
let null = std::ptr::null();
let mut allocator = null;
assert_eq!(create_allocator(null, 0, &ALLOCATOR_IIDS[0], &mut allocator), S_OK);
let lists: Vec<This> = (0..LISTS)
.map(|_| {
let mut list = null;
assert_eq!(create_list(null, 0, 0, allocator, null, &LIST_IIDS[1], &mut list), S_OK);
list
})
.collect();
let held = |lists: &[This]| lists.iter().filter(|&&l| host_list(l).is_some()).count();
assert_eq!(held(&lists), LISTS, "a new list records at once");
for round in 0..4 {
for &list in &lists {
// (A new list starts open.)
if round > 0 {
assert_eq!(reset(list, allocator, null), S_OK);
assert!(host_list(list).is_some(), "round {round}: a reset list records");
}
assert_eq!(close(list), S_OK);
}
// Submitted, run and landed, as a queue that holds its lists
// does it: their host sides are taken and given back once run.
let (taken, order) = detach(&lists);
assert_eq!(taken.len(), LISTS);
recycle(taken, &order);
assert_eq!(held(&lists), 0, "round {round}: submitted lists keep host lists alive");
}
let release = |o: This| slot::<extern "system" fn(This) -> u32>(o, 0x10)(o);
for list in lists {
assert_eq!(release(list), 0);
}
assert_eq!(release(allocator), 0);
}

/// The idle pool follows what was needed lately: after a burst fills it
/// to the most, a steady need of a few lists a second leaves it with
/// about those few. Each trim lets go of the oldest half of those
/// nobody took since the one before, and never of those that were taken.
#[cfg(windows)]
#[test]
fn idle_lists_nobody_takes_are_let_go() {
let mut pool: Pool<u32> = Pool::new();
for i in 0..IDLE_MOST.0 as u32 {
assert_eq!(pool.give(i, 1, IDLE_MOST), None);
}
assert_eq!(pool.give(9999, 1, IDLE_MOST), Some(9999), "no more than the most");
assert_eq!(pool.give(9999, IDLE_MOST.1, (usize::MAX, IDLE_MOST.1)), Some(9999), "no more bytes than the most");
// Given since the last trim: nobody had the time to take them.
assert!(pool.trim().is_empty());
let mut kept = Vec::new();
for second in 0..12 {
// Eight lists recorded, run and given back.
let taken: Vec<u32> = (0..8).map(|_| pool.take().expect("kept")).collect();
for &t in &taken {
assert_eq!(pool.give(t, 1, IDLE_MOST), None);
}
let gone = pool.trim();
assert!(gone.iter().all(|(t, _)| !taken.contains(t)), "second {second}: a list in use was let go");
if second == 0 {
assert_eq!(gone.len(), (IDLE_MOST.0 - 8).div_ceil(2));
assert_eq!(gone[0].0, 0, "the oldest go first");
}
kept = taken;
}
assert_eq!(pool.kept.len(), 8, "what is kept is what was needed lately");
assert_eq!(pool.bytes, 8);
assert!(pool.kept.iter().all(|(t, _)| kept.contains(t)));
// Nothing needed at all: the pool empties.
for _ in 0..8 {
pool.trim();
}
assert!(pool.kept.is_empty() && pool.bytes == 0);
}

/// An offset near `u64::MAX` must not overflow and write outside the
/// query destination buffer.
#[test]
Expand Down
Loading