Skip to content

Commit 99c0c36

Browse files
committed
Add comments explaining Drop impl for FrameData
1 parent 01125b3 commit 99c0c36

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

src/source/buffered.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,20 @@ where
7373
I::Item: Sample,
7474
{
7575
fn drop(&mut self) {
76+
// This is necessary to prevent stack overflows deallocating long chains of the mutually
77+
// recursive `Frame` and `FrameData` types. This iteratively traverses as much of the
78+
// chain as needs to be deallocated, and repeatedly "pops" the head off the list. This
79+
// solves the problem, as when the time comes to actually deallocate the `FrameData`,
80+
// the `next` field will contain a `Frame::End`, or an `Arc` with additional references,
81+
// so the depth of recursive drops will be bounded.
7682
loop {
7783
if let Ok(arc_next) = self.next.get_mut() {
7884
if let Some(next_ref) = Arc::get_mut(arc_next) {
85+
// This allows us to own the next Frame.
7986
let next = mem::replace(next_ref, Frame::End);
8087
if let Frame::Data(next_data) = next {
88+
// Swap the current FrameData with the next one, allowing the current one
89+
// to go out of scope.
8190
mem::replace(self, next_data);
8291
} else {
8392
break;

0 commit comments

Comments
 (0)