gstreamer/auto/
allocator.rs

1// This file was generated by gir (https://github.com/gtk-rs/gir)
2// from gir-files (https://github.com/gtk-rs/gir-files)
3// from gst-gir-files (https://gitlab.freedesktop.org/gstreamer/gir-files-rs.git)
4// DO NOT EDIT
5
6use crate::{ffi, AllocationParams, Memory, Object};
7use glib::{prelude::*, translate::*};
8
9glib::wrapper! {
10    /// Memory is usually created by allocators with a [`AllocatorExt::alloc()`][crate::prelude::AllocatorExt::alloc()]
11    /// method call. When [`None`] is used as the allocator, the default allocator will
12    /// be used.
13    ///
14    /// New allocators can be registered with [`register()`][Self::register()].
15    /// Allocators are identified by name and can be retrieved with
16    /// [`find()`][Self::find()]. [`AllocatorExt::set_default()`][crate::prelude::AllocatorExt::set_default()] can be used to change the
17    /// default allocator.
18    ///
19    /// New memory can be created with [`Memory::new_wrapped()`][crate::Memory::new_wrapped()] that wraps the memory
20    /// allocated elsewhere.
21    ///
22    /// This is an Abstract Base Class, you cannot instantiate it.
23    ///
24    /// # Implements
25    ///
26    /// [`AllocatorExt`][trait@crate::prelude::AllocatorExt], [`GstObjectExt`][trait@crate::prelude::GstObjectExt], [`trait@glib::ObjectExt`]
27    #[doc(alias = "GstAllocator")]
28    pub struct Allocator(Object<ffi::GstAllocator, ffi::GstAllocatorClass>) @extends Object;
29
30    match fn {
31        type_ => || ffi::gst_allocator_get_type(),
32    }
33}
34
35impl Allocator {
36    pub const NONE: Option<&'static Allocator> = None;
37
38    /// Find a previously registered allocator with `name`. When `name` is [`None`], the
39    /// default allocator will be returned.
40    /// ## `name`
41    /// the name of the allocator
42    ///
43    /// # Returns
44    ///
45    /// a [`Allocator`][crate::Allocator] or [`None`] when
46    /// the allocator with `name` was not registered.
47    #[doc(alias = "gst_allocator_find")]
48    pub fn find(name: Option<&str>) -> Option<Allocator> {
49        assert_initialized_main_thread!();
50        unsafe { from_glib_full(ffi::gst_allocator_find(name.to_glib_none().0)) }
51    }
52}
53
54unsafe impl Send for Allocator {}
55unsafe impl Sync for Allocator {}
56
57mod sealed {
58    pub trait Sealed {}
59    impl<T: super::IsA<super::Allocator>> Sealed for T {}
60}
61
62/// Trait containing all [`struct@Allocator`] methods.
63///
64/// # Implementors
65///
66/// [`Allocator`][struct@crate::Allocator]
67pub trait AllocatorExt: IsA<Allocator> + sealed::Sealed + 'static {
68    /// Use `self` to allocate a new memory block with memory that is at least
69    /// `size` big.
70    ///
71    /// The optional `params` can specify the prefix and padding for the memory. If
72    /// [`None`] is passed, no flags, no extra prefix/padding and a default alignment is
73    /// used.
74    ///
75    /// The prefix/padding will be filled with 0 if flags contains
76    /// [`MemoryFlags::ZERO_PREFIXED`][crate::MemoryFlags::ZERO_PREFIXED] and [`MemoryFlags::ZERO_PADDED`][crate::MemoryFlags::ZERO_PADDED] respectively.
77    ///
78    /// When `self` is [`None`], the default allocator will be used.
79    ///
80    /// The alignment in `params` is given as a bitmask so that `align` + 1 equals
81    /// the amount of bytes to align to. For example, to align to 8 bytes,
82    /// use an alignment of 7.
83    /// ## `size`
84    /// size of the visible memory area
85    /// ## `params`
86    /// optional parameters
87    ///
88    /// # Returns
89    ///
90    /// a new [`Memory`][crate::Memory].
91    #[doc(alias = "gst_allocator_alloc")]
92    fn alloc(
93        &self,
94        size: usize,
95        params: Option<&AllocationParams>,
96    ) -> Result<Memory, glib::BoolError> {
97        unsafe {
98            Option::<_>::from_glib_full(ffi::gst_allocator_alloc(
99                self.as_ref().to_glib_none().0,
100                size,
101                mut_override(params.to_glib_none().0),
102            ))
103            .ok_or_else(|| glib::bool_error!("Failed to allocate memory"))
104        }
105    }
106
107    /// Set the default allocator.
108    #[doc(alias = "gst_allocator_set_default")]
109    fn set_default(self) {
110        unsafe {
111            ffi::gst_allocator_set_default(self.upcast().into_glib_ptr());
112        }
113    }
114}
115
116impl<O: IsA<Allocator>> AllocatorExt for O {}