[vlc-commits] [Git][videolan/vlc][master] 7 commits: vlcrs-core: tracer: remove unsafe from Activate definition
Alexandre Janniaux (@alexandre-janniaux)
gitlab at videolan.org
Thu Nov 12 16:04:36 UTC 2026
Alexandre Janniaux pushed to branch master at VideoLAN / VLC
Commits:
225e0293 by Alexandre Janniaux at 2026-11-12T14:02:39+00:00
vlcrs-core: tracer: remove unsafe from Activate definition
It's not needed or not needed anymore.
- - - - -
1d4d1ce8 by Alexandre Janniaux at 2026-11-12T14:02:39+00:00
vlcrs-core: tracer: setup Drop() for tracer
The vlc_tracer was leaked when the Tracer was destroyed.
- - - - -
5786578a by Alexandre Janniaux at 2026-11-12T14:02:39+00:00
vlcrs-core: tracer: use early return operator
The ? operator will already return None.
- - - - -
611303c4 by Alexandre Janniaux at 2026-11-12T14:02:39+00:00
vlcrs-core: tracer: return Self from open
Returning Self ensure we size the output type instead of returning a dyn
trait, and we didn't need to change the returned type for anything else
given we can register different types for the capabilities.
This is an API change for rust plugins but not an ABI one since the
binding part is merged into the plugin currently.
- - - - -
7b30103d by Alexandre Janniaux at 2026-11-12T14:02:39+00:00
vlcrs-core: tracer: use one operation table per tracer type
We can't declare a static table parametrized by a type, hence why I had
set the table to use dyn traits, BUT we can actually return the table as
a &vlc_tracer_operation{} directly, which will allows parametrizing
against the proper type directly.
Using the proper type will also avoid the double-box wrapping for the
fat pointer, and remove the unsafe calls from mod.rs.
- - - - -
cbd2e6df by Alexandre Janniaux at 2026-11-12T14:02:39+00:00
vlcrs-core: tracer: remove void* usage and use Box<T> instead
Use the proper Box<T> type as opaque type since Box<T> is transparent
with pointer types. Since the operations are now depending on the type
being implemented, the operation table is now parametrized by the type
that implements the capability, which is not allowed for static or const
tables, but is allowed for a returned reference like this that has
'static lifetime.
Since Box<T> is transparent, it still work when calling the operations,
but it might need some work to be 100% compliant with platforms like
wasm or tools like CFI?
- - - - -
189bf59e by Alexandre Janniaux at 2026-11-12T14:02:39+00:00
tracer: make Trace wrap vlc_tracer_trace
Commit 6bfea2228a99288fdf5a5ec4eea5cf31928d3a89 allowed safe iteration
for traces but the added TraceValue and Trace are deriving Copy even
though they are linked to a short-lived C pointer. We need to remove
this, and at the same time, we can actually provide Trace as transparent
directly in the API to avoid using unsafe.
This is an API change for rust plugins but not an ABI one since the
binding part is merged into the plugin currently.
- - - - -
3 changed files:
- modules/logger/telegraf-rs/src/lib.rs
- src/rust/vlcrs-core/src/tracer/mod.rs
- src/rust/vlcrs-core/src/tracer/sys.rs
Changes:
=====================================
modules/logger/telegraf-rs/src/lib.rs
=====================================
@@ -38,7 +38,7 @@ struct TelegrafTracer {
}
impl TracerCapability for TelegrafTracer {
- fn open(_obj: &mut vlcrs_core::object::Object) -> Option<impl TracerCapability>
+ fn open(_obj: &mut vlcrs_core::object::Object) -> Option<Self>
where
Self: Sized,
{
=====================================
src/rust/vlcrs-core/src/tracer/mod.rs
=====================================
@@ -17,7 +17,7 @@
use std::{
cell::UnsafeCell,
- ffi::{c_char, c_void, CStr},
+ ffi::{c_char, CStr},
mem::MaybeUninit,
ptr::NonNull,
};
@@ -49,7 +49,7 @@ pub struct Tick(pub i64);
/// use vlcrs_core::tracer::{Tick, TracerCapability, Trace};
/// struct Module { last_trace_tick: Cell<Tick>, }
/// impl TracerCapability for Module {
-/// fn open(obj: &mut Object) -> Option<impl TracerCapability> {
+/// fn open(obj: &mut Object) -> Option<Self> {
/// Some(Self{ last_trace_tick: Cell::from(Tick(0)) })
/// }
///
@@ -79,7 +79,7 @@ pub struct Tick(pub i64);
/// object so that it becomes Sync and can be used in the multiples threads.
///
pub trait TracerCapability: Sync {
- fn open(obj: &mut Object) -> Option<impl TracerCapability>
+ fn open(obj: &mut Object) -> Option<Self>
where
Self: Sized;
@@ -87,46 +87,34 @@ pub trait TracerCapability: Sync {
}
#[allow(non_camel_case_types)]
-pub type TracerCapabilityActivate =
- unsafe extern "C" fn(
+pub type TracerCapabilityActivate<T> =
+ extern "C" fn(
obj: &mut Object,
- opaque: &mut MaybeUninit<*mut c_void>,
- ) -> Option<&'static sys::vlc_tracer_operations>;
+ opaque: &mut MaybeUninit<Box<T>>,
+ ) -> Option<&'static sys::vlc_tracer_operations<T>>;
-extern "C" fn tracer_trace(
- opaque: *const c_void,
+extern "C" fn tracer_trace<T: TracerCapability>(
+ tracer: &T,
tick: sys::vlc_tick,
- trace: NonNull<sys::vlc_tracer_trace>,
+ trace: &Trace,
) {
- {
- let tracer: &dyn TracerCapability =
- unsafe { &**(opaque as *const Box<dyn TracerCapability>) };
- let trace = Trace(trace);
- tracer.trace(Tick(tick), &trace);
- }
-}
-
-extern "C" fn tracer_destroy(opaque: *mut c_void) {
- let tracer: *mut Box<dyn TracerCapability> = opaque as *mut _;
- let _ = unsafe { Box::from_raw(tracer) };
+ tracer.trace(Tick(tick), trace);
}
-const TRACER_OPERATIONS: sys::vlc_tracer_operations = sys::vlc_tracer_operations {
- trace: tracer_trace,
- destroy: tracer_destroy,
-};
+extern "C" fn tracer_destroy<T>(_tracer: Box<T>) {}
extern "C" fn activate_tracer<T: TracerCapability>(
obj: &mut Object,
- opaque: &mut MaybeUninit<*mut c_void>,
-) -> Option<&'static sys::vlc_tracer_operations> {
- if let Some(instance) = T::open(obj) {
- let wrapper: Box<dyn TracerCapability> = Box::try_new(instance).ok()?;
- let sys = Box::into_raw(Box::try_new(wrapper).ok()?);
- opaque.write(sys as *mut _);
- return Some(&TRACER_OPERATIONS);
- }
- None
+ opaque: &mut MaybeUninit<Box<T>>,
+) -> Option<&'static sys::vlc_tracer_operations<T>> {
+ let instance = T::open(obj)?;
+ opaque.write(Box::try_new(instance).ok()?);
+ Some(const {
+ &sys::vlc_tracer_operations {
+ trace: tracer_trace::<T>,
+ destroy: tracer_destroy::<T>,
+ }
+ })
}
///
@@ -146,7 +134,7 @@ extern "C" fn activate_tracer<T: TracerCapability>(
///
/// // Empty implementation
/// impl TracerCapability for CustomTracer {
-/// fn open(obj: &mut Object) -> Option<impl TracerCapability> {
+/// fn open(obj: &mut Object) -> Option<Self> {
/// Some(Self{})
/// }
/// fn trace(&self, _tick: Tick, _trace: &Trace) {}
@@ -165,9 +153,9 @@ pub struct TracerModuleLoader;
impl<T> ModuleProtocol<T> for TracerModuleLoader
where
- T: TracerCapability,
+ T: TracerCapability + 'static,
{
- type Activate = TracerCapabilityActivate;
+ type Activate = TracerCapabilityActivate<T>;
fn activate_function() -> Self::Activate {
activate_tracer::<T>
}
@@ -202,18 +190,24 @@ impl Tracer {
///
/// Register the new point at time `tick` with the metadata `entries:.
///
- pub fn trace(&self, tick: Tick, trace: Trace) {
+ pub fn trace(&self, tick: Tick, trace: &Trace) {
unsafe {
// SAFETY: TODO
let tracer = *self.tracer.get();
// SAFETY: the pointer `tracer` is guaranteed to be non-null and
// nobody else has reference to it.
- sys::vlc_tracer_TraceWithTs(tracer, tick.0, trace.0);
+ sys::vlc_tracer_TraceWithTs(tracer, tick.0, trace);
}
}
}
+impl Drop for Tracer {
+ fn drop(&mut self) {
+ unsafe { sys::vlc_tracer_Destroy(*self.tracer.get()); }
+ }
+}
+
#[macro_export]
macro_rules! trace {
($tracer: ident, $ts: expr, $($key:ident = $value:expr)*) => {};
@@ -230,7 +224,7 @@ macro_rules! trace {
/// # use vlcrs_core::object::Object;
/// # struct MyTracer {}
/// impl TracerCapability for MyTracer {
-/// # fn open(_: &mut Object) -> Option<impl TracerCapability> { Some(MyTracer{}) }
+/// # fn open(_: &mut Object) -> Option<Self> { Some(MyTracer{}) }
/// fn trace(&self, _: Tick, trace: &Trace) {
/// for entry in trace.entries() {
/// println!("{}", entry.key);
@@ -238,9 +232,8 @@ macro_rules! trace {
/// }
/// }
/// ```
-#[derive(PartialEq, Copy, Clone)]
#[repr(transparent)]
-pub struct Trace(NonNull<sys::vlc_tracer_trace>);
+pub struct Trace(sys::vlc_tracer_trace);
impl Trace {
/// Get an iterator over the trace entries.
@@ -254,7 +247,7 @@ impl<'a> IntoIterator for &'a Trace {
type IntoIter = TraceIterator<'a>;
fn into_iter(self) -> Self::IntoIter {
TraceIterator {
- current_field: unsafe { self.0.read().entries },
+ current_field: self.0.entries,
_plt: std::marker::PhantomData,
}
}
@@ -346,7 +339,7 @@ mod test {
entries: NonNull::from(&entries[0]),
};
- let trace = Trace(NonNull::from(&trace));
+ let trace = Trace(trace);
let mut iterator = trace.entries();
=====================================
src/rust/vlcrs-core/src/tracer/sys.rs
=====================================
@@ -22,10 +22,11 @@
#![allow(non_snake_case)]
use std::{
- ffi::{c_char, c_double, c_void},
+ ffi::{c_char, c_double},
ptr::NonNull,
};
+use super::Trace;
use crate::object::Object;
pub type vlc_tick = i64;
@@ -70,17 +71,17 @@ pub struct vlc_tracer {
}
#[repr(C)]
-#[derive(Debug, Copy, Clone)]
-pub struct vlc_tracer_operations {
- pub trace: extern "C" fn(opaque: *const c_void, ts: vlc_tick, trace: NonNull<vlc_tracer_trace>),
- pub destroy: extern "C" fn(*mut c_void),
+pub struct vlc_tracer_operations<T> {
+ pub trace: extern "C" fn(opaque: &T, ts: vlc_tick, trace: &Trace),
+ pub destroy: extern "C" fn(Box<T>),
}
extern "C" {
pub fn vlc_tracer_Create(parent: &Object, name: *const c_char) -> Option<NonNull<vlc_tracer>>;
+ pub fn vlc_tracer_Destroy(tracer: NonNull<vlc_tracer>);
pub fn vlc_tracer_TraceWithTs(
tracer: NonNull<vlc_tracer>,
tick: vlc_tick,
- trace: NonNull<vlc_tracer_trace>,
+ trace: &Trace,
);
}
View it on GitLab: https://code.videolan.org/videolan/vlc/-/compare/539ab32b176a199bc7437d83ff590e7465377018...189bf59e3422714ebf8ecb90b6f4ea494a0e5cb1
--
View it on GitLab: https://code.videolan.org/videolan/vlc/-/compare/539ab32b176a199bc7437d83ff590e7465377018...189bf59e3422714ebf8ecb90b6f4ea494a0e5cb1
You're receiving this email because of your account on code.videolan.org. Manage all notifications: https://code.videolan.org/-/profile/notifications | Help: https://code.videolan.org/help
More information about the vlc-commits
mailing list