Skip to content
Merged
Changes from all commits
Commits
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
16 changes: 16 additions & 0 deletions datafusion/ffi/src/record_batch_stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ pub struct FFI_RecordBatchStream {
/// Return the schema of the record batch
pub schema: unsafe extern "C" fn(stream: &Self) -> WrappedSchema,

/// Release the memory of the private data when it is no longer being used.
pub release: unsafe extern "C" fn(arg: &mut Self),

/// Internal data. This is only to be accessed by the provider of the plan.
/// The foreign library should never attempt to access this data.
pub private_data: *mut c_void,
Expand All @@ -82,6 +85,7 @@ impl FFI_RecordBatchStream {
FFI_RecordBatchStream {
poll_next: poll_next_fn_wrapper,
schema: schema_fn_wrapper,
release: release_fn_wrapper,
private_data,
}
}
Expand All @@ -96,6 +100,12 @@ unsafe extern "C" fn schema_fn_wrapper(stream: &FFI_RecordBatchStream) -> Wrappe
(*stream).schema().into()
}

unsafe extern "C" fn release_fn_wrapper(provider: &mut FFI_RecordBatchStream) {
let private_data =
Box::from_raw(provider.private_data as *mut RecordBatchStreamPrivateData);
drop(private_data);
}

fn record_batch_to_wrapped_array(
record_batch: RecordBatch,
) -> RResult<WrappedArray, RString> {
Expand Down Expand Up @@ -197,6 +207,12 @@ impl Stream for FFI_RecordBatchStream {
}
}

impl Drop for FFI_RecordBatchStream {
fn drop(&mut self) {
unsafe { (self.release)(self) }
}
}

#[cfg(test)]
mod tests {
use std::sync::Arc;
Expand Down
Loading