122018a5aSAlice Ryhl // SPDX-License-Identifier: GPL-2.0 222018a5aSAlice Ryhl 322018a5aSAlice Ryhl //! Seq file bindings. 422018a5aSAlice Ryhl //! 522018a5aSAlice Ryhl //! C header: [`include/linux/seq_file.h`](srctree/include/linux/seq_file.h) 622018a5aSAlice Ryhl 722018a5aSAlice Ryhl use crate::{bindings, c_str, types::NotThreadSafe, types::Opaque}; 822018a5aSAlice Ryhl 922018a5aSAlice Ryhl /// A utility for generating the contents of a seq file. 1022018a5aSAlice Ryhl #[repr(transparent)] 1122018a5aSAlice Ryhl pub struct SeqFile { 1222018a5aSAlice Ryhl inner: Opaque<bindings::seq_file>, 1322018a5aSAlice Ryhl _not_send: NotThreadSafe, 1422018a5aSAlice Ryhl } 1522018a5aSAlice Ryhl 1622018a5aSAlice Ryhl impl SeqFile { 1722018a5aSAlice Ryhl /// Creates a new [`SeqFile`] from a raw pointer. 1822018a5aSAlice Ryhl /// 1922018a5aSAlice Ryhl /// # Safety 2022018a5aSAlice Ryhl /// 21*cd1ed11aSBorys Tyran /// The caller must ensure that for the duration of `'a` the following is satisfied: 2222018a5aSAlice Ryhl /// * The pointer points at a valid `struct seq_file`. 2322018a5aSAlice Ryhl /// * The `struct seq_file` is not accessed from any other thread. 2422018a5aSAlice Ryhl pub unsafe fn from_raw<'a>(ptr: *mut bindings::seq_file) -> &'a SeqFile { 2522018a5aSAlice Ryhl // SAFETY: The caller ensures that the reference is valid for 'a. There's no way to trigger 2622018a5aSAlice Ryhl // a data race by using the `&SeqFile` since this is the only thread accessing the seq_file. 2722018a5aSAlice Ryhl // 2822018a5aSAlice Ryhl // CAST: The layout of `struct seq_file` and `SeqFile` is compatible. 2922018a5aSAlice Ryhl unsafe { &*ptr.cast() } 3022018a5aSAlice Ryhl } 3122018a5aSAlice Ryhl 3222018a5aSAlice Ryhl /// Used by the [`seq_print`] macro. 330b9817caSKunwu Chan #[inline] 3422018a5aSAlice Ryhl pub fn call_printf(&self, args: core::fmt::Arguments<'_>) { 3522018a5aSAlice Ryhl // SAFETY: Passing a void pointer to `Arguments` is valid for `%pA`. 3622018a5aSAlice Ryhl unsafe { 3722018a5aSAlice Ryhl bindings::seq_printf( 3822018a5aSAlice Ryhl self.inner.get(), 3922018a5aSAlice Ryhl c_str!("%pA").as_char_ptr(), 4027c7518eSMiguel Ojeda &args as *const _ as *const crate::ffi::c_void, 4122018a5aSAlice Ryhl ); 4222018a5aSAlice Ryhl } 4322018a5aSAlice Ryhl } 4422018a5aSAlice Ryhl } 4522018a5aSAlice Ryhl 4622018a5aSAlice Ryhl /// Write to a [`SeqFile`] with the ordinary Rust formatting syntax. 4722018a5aSAlice Ryhl #[macro_export] 4822018a5aSAlice Ryhl macro_rules! seq_print { 4922018a5aSAlice Ryhl ($m:expr, $($arg:tt)+) => ( 5022018a5aSAlice Ryhl $m.call_printf(format_args!($($arg)+)) 5122018a5aSAlice Ryhl ); 5222018a5aSAlice Ryhl } 5322018a5aSAlice Ryhl pub use seq_print; 54