objc2_core_media/generated/
CMMemoryPool.rs

1//! This file has been automatically generated by `objc2`'s `header-translator`.
2//! DO NOT EDIT
3use core::cell::UnsafeCell;
4use core::ffi::*;
5use core::marker::{PhantomData, PhantomPinned};
6use core::ptr::NonNull;
7#[cfg(feature = "objc2")]
8use objc2::__framework_prelude::*;
9use objc2_core_foundation::*;
10
11use crate::*;
12
13/// CMMemoryPool.h
14///
15/// Memory pool for optimizing repeated large block allocation.
16///
17/// CMMemoryPool is a memory allocation service that holds onto a pool of
18/// recently deallocated memory so as to speed up subsequent allocations of the same size.
19/// It's intended for cases where large memory blocks need to be repeatedly allocated --
20/// for example, the compressed data output by a video encoder.
21///
22/// All of its allocations are on the granularity of page sizes; it does not suballocate
23/// memory within pages, so it is a poor choice for allocating tiny blocks.
24/// For example, it's appropriate to use as the blockAllocator argument to
25/// CMBlockBufferCreateWithMemoryBlock, but not the structureAllocator argument --
26/// use kCFAllocatorDefault instead.
27///
28/// When you no longer need to allocate memory from the pool, call CMMemoryPoolInvalidate
29/// and CFRelease.  Calling CMMemoryPoolInvalidate tells the pool to stop holding onto
30/// memory for reuse.  Note that the pool's CFAllocator can outlive the pool, owing
31/// to the way that CoreFoundation is designed: CFAllocators are themselves CF objects,
32/// and every object allocated with a CFAllocator implicitly retains the CFAllocator
33/// until it is finalized.  After the CMMemoryPool is invalidated or finalized,
34/// its CFAllocator allocates and deallocates with no pooling behavior.
35///
36/// CMMemoryPool deallocates memory if it has not been recycled in 0.5 second,
37/// so that short-term peak usage does not cause persistent bloat.
38/// (This period may be overridden by specifying kCMMemoryPoolOption_AgeOutPeriod.)
39/// Such "aging out" is done during the pool's CFAllocatorAllocate and
40/// CFAllocatorDeallocate methods.
41///
42/// See also [Apple's documentation](https://developer.apple.com/documentation/coremedia/cmmemorypool?language=objc)
43#[doc(alias = "CMMemoryPoolRef")]
44#[repr(C)]
45pub struct CMMemoryPool {
46    inner: [u8; 0],
47    _p: UnsafeCell<PhantomData<(*const UnsafeCell<()>, PhantomPinned)>>,
48}
49
50cf_type!(
51    unsafe impl CMMemoryPool {}
52);
53#[cfg(feature = "objc2")]
54cf_objc2_type!(
55    unsafe impl RefEncode<"OpaqueCMMemoryPool"> for CMMemoryPool {}
56);
57
58unsafe impl Send for CMMemoryPool {}
59
60unsafe impl Sync for CMMemoryPool {}
61
62/// [Apple's documentation](https://developer.apple.com/documentation/coremedia/kcmmemorypoolerror_allocationfailed?language=objc)
63pub const kCMMemoryPoolError_AllocationFailed: OSStatus = -15490;
64/// [Apple's documentation](https://developer.apple.com/documentation/coremedia/kcmmemorypoolerror_invalidparameter?language=objc)
65pub const kCMMemoryPoolError_InvalidParameter: OSStatus = -15491;
66
67unsafe impl ConcreteType for CMMemoryPool {
68    #[doc(alias = "CMMemoryPoolGetTypeID")]
69    #[inline]
70    fn type_id() -> CFTypeID {
71        extern "C-unwind" {
72            fn CMMemoryPoolGetTypeID() -> CFTypeID;
73        }
74        unsafe { CMMemoryPoolGetTypeID() }
75    }
76}
77
78extern "C" {
79    /// Specifies how long memory should be allowed to hang out in the pool before being deallocated.
80    ///
81    /// Pass this in the options dictionary to CMMemoryPoolCreate.
82    ///
83    /// See also [Apple's documentation](https://developer.apple.com/documentation/coremedia/kcmmemorypooloption_ageoutperiod?language=objc)
84    pub static kCMMemoryPoolOption_AgeOutPeriod: &'static CFString;
85}
86
87impl CMMemoryPool {
88    /// Creates a new CMMemoryPool.
89    ///
90    /// # Safety
91    ///
92    /// `options` generics must be of the correct type.
93    #[doc(alias = "CMMemoryPoolCreate")]
94    #[inline]
95    pub unsafe fn new(options: Option<&CFDictionary>) -> CFRetained<CMMemoryPool> {
96        extern "C-unwind" {
97            fn CMMemoryPoolCreate(options: Option<&CFDictionary>) -> Option<NonNull<CMMemoryPool>>;
98        }
99        let ret = unsafe { CMMemoryPoolCreate(options) };
100        let ret =
101            ret.expect("function was marked as returning non-null, but actually returned NULL");
102        unsafe { CFRetained::from_raw(ret) }
103    }
104
105    /// Returns the pool's CFAllocator.
106    #[doc(alias = "CMMemoryPoolGetAllocator")]
107    #[inline]
108    pub unsafe fn allocator(&self) -> CFRetained<CFAllocator> {
109        extern "C-unwind" {
110            fn CMMemoryPoolGetAllocator(pool: &CMMemoryPool) -> Option<NonNull<CFAllocator>>;
111        }
112        let ret = unsafe { CMMemoryPoolGetAllocator(self) };
113        let ret =
114            ret.expect("function was marked as returning non-null, but actually returned NULL");
115        unsafe { CFRetained::retain(ret) }
116    }
117
118    /// Deallocates all memory the pool was holding for recycling.
119    #[doc(alias = "CMMemoryPoolFlush")]
120    #[inline]
121    pub unsafe fn flush(&self) {
122        extern "C-unwind" {
123            fn CMMemoryPoolFlush(pool: &CMMemoryPool);
124        }
125        unsafe { CMMemoryPoolFlush(self) }
126    }
127
128    /// Stops the pool from recycling.
129    ///
130    /// When CMMemoryPoolInvalidate is called the pool's allocator stops recycling memory.
131    /// The pool deallocates any memory it was holding for recycling.
132    /// This also happens when the retain count of the CMMemoryPool drops to zero,
133    /// except that under GC it may be delayed.
134    #[doc(alias = "CMMemoryPoolInvalidate")]
135    #[inline]
136    pub unsafe fn invalidate(&self) {
137        extern "C-unwind" {
138            fn CMMemoryPoolInvalidate(pool: &CMMemoryPool);
139        }
140        unsafe { CMMemoryPoolInvalidate(self) }
141    }
142}
143
144#[deprecated = "renamed to `CMMemoryPool::new`"]
145#[inline]
146pub unsafe extern "C-unwind" fn CMMemoryPoolCreate(
147    options: Option<&CFDictionary>,
148) -> CFRetained<CMMemoryPool> {
149    extern "C-unwind" {
150        fn CMMemoryPoolCreate(options: Option<&CFDictionary>) -> Option<NonNull<CMMemoryPool>>;
151    }
152    let ret = unsafe { CMMemoryPoolCreate(options) };
153    let ret = ret.expect("function was marked as returning non-null, but actually returned NULL");
154    unsafe { CFRetained::from_raw(ret) }
155}
156
157#[deprecated = "renamed to `CMMemoryPool::allocator`"]
158#[inline]
159pub unsafe extern "C-unwind" fn CMMemoryPoolGetAllocator(
160    pool: &CMMemoryPool,
161) -> CFRetained<CFAllocator> {
162    extern "C-unwind" {
163        fn CMMemoryPoolGetAllocator(pool: &CMMemoryPool) -> Option<NonNull<CFAllocator>>;
164    }
165    let ret = unsafe { CMMemoryPoolGetAllocator(pool) };
166    let ret = ret.expect("function was marked as returning non-null, but actually returned NULL");
167    unsafe { CFRetained::retain(ret) }
168}
169
170extern "C-unwind" {
171    #[deprecated = "renamed to `CMMemoryPool::flush`"]
172    pub fn CMMemoryPoolFlush(pool: &CMMemoryPool);
173}
174
175extern "C-unwind" {
176    #[deprecated = "renamed to `CMMemoryPool::invalidate`"]
177    pub fn CMMemoryPoolInvalidate(pool: &CMMemoryPool);
178}