1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
use super::{CFType, CFTypeID};
use crate::{core::Arc, core_foundation::sys};
use std::{mem::MaybeUninit, ptr};
mod callbacks;
mod context;
pub use callbacks::*;
pub use context::*;
subclass! {
/// An allocator object.
///
/// Documentation:
/// [Swift](https://developer.apple.com/documentation/corefoundation/cfallocator?language=swift) |
/// [Objective-C](https://developer.apple.com/documentation/corefoundation/cfallocator?language=objc)
///
/// # Using the default allocator
///
/// APIs that use `CFAllocator` will take an `Option<&CFAllocator>`, where
/// [`None`] is equivalent to
/// [`kCFAllocatorDefault`](https://developer.apple.com/documentation/corefoundation/kcfallocatordefault?language=objc).
pub class CFAllocator: CFType<'static>;
}
/// Predefined allocators.
///
/// See [documentation](https://developer.apple.com/documentation/corefoundation/cfallocator/predefined_allocators?language=objc).
impl CFAllocator {
/// An allocator that does nothing—it allocates no memory.
///
/// See [documentation](https://developer.apple.com/documentation/corefoundation/kcfallocatornull?language=objc).
#[doc(alias = "kCFAllocatorNull")]
pub fn null() -> &'static Self {
extern "C" {
static kCFAllocatorNull: &'static CFAllocator;
}
unsafe { kCFAllocatorNull }
}
/// Default system allocator.
///
/// You rarely need to use this.
///
/// See [documentation](https://developer.apple.com/documentation/corefoundation/kcfallocatorsystemdefault?language=objc).
#[doc(alias = "kCFAllocatorSystemDefault")]
pub fn system_default() -> &'static Self {
extern "C" {
static kCFAllocatorSystemDefault: &'static CFAllocator;
}
unsafe { kCFAllocatorSystemDefault }
}
/// An allocator that uses `malloc()`, `realloc()`, and `free()`.
///
/// See [documentation](https://developer.apple.com/documentation/corefoundation/kcfallocatormalloc?language=objc).
#[doc(alias = "kCFAllocatorMalloc")]
pub fn malloc() -> &'static Self {
extern "C" {
static kCFAllocatorMalloc: &'static CFAllocator;
}
unsafe { kCFAllocatorMalloc }
}
/// An allocator that uses the default malloc zone, returned by
/// `malloc_default_zone()`.
///
/// You should only use this when an object is safe to be allocated in non-scanned memory.
///
/// See [documentation](https://developer.apple.com/documentation/corefoundation/kcfallocatormalloczone?language=objc).
#[doc(alias = "kCFAllocatorMallocZone")]
pub fn malloc_zone() -> &'static Self {
extern "C" {
static kCFAllocatorMallocZone: &'static CFAllocator;
}
unsafe { kCFAllocatorMallocZone }
}
/// Special allocator argument to `CFAllocatorCreate`—it uses the functions
/// given in the context to allocate the allocator.
///
/// See [documentation](https://developer.apple.com/documentation/corefoundation/kcfallocatorusecontext?language=objc).
///
/// # Safety
///
/// This isn't really a `CFAllocator` reference, so most operations outside
/// of the single specific use case are unsafe. They will most likely cause
/// a [segfault](https://en.wikipedia.org/wiki/Segmentation_fault).
///
/// This is treated as a `CFAllocator` reference in order to make it easy to
/// fit into `CFAllocator` APIs.
#[doc(alias = "kCFAllocatorUseContext")]
pub unsafe fn use_context() -> &'static Self {
extern "C" {
static kCFAllocatorUseContext: &'static CFAllocator;
}
kCFAllocatorUseContext
}
}
impl CFAllocator {
/// Returns the type identifier for `CFAllocator`.
///
/// See [documentation](https://developer.apple.com/documentation/corefoundation/1521328-cfallocatorgettypeid?language=objc).
#[inline]
#[doc(alias = "CFAllocatorGetTypeID")]
pub fn type_id() -> CFTypeID {
unsafe { sys::CFAllocatorGetTypeID() }
}
/// Creates an allocator object.
///
/// See [documentation](https://developer.apple.com/documentation/corefoundation/1521159-cfallocatorcreate?language=objc).
#[inline]
#[doc(alias = "CFAllocatorCreate")]
pub unsafe fn create(
allocator: Option<&CFAllocator>,
mut context: CFAllocatorContext,
) -> Arc<Self> {
let allocator: *const CFAllocator = match allocator {
Some(allocator) => allocator,
None => ptr::null(),
};
Arc::from_raw(sys::CFAllocatorCreate(allocator, &mut context))
}
/// Returns the context of the default allocator.
///
/// See [documentation](https://developer.apple.com/documentation/corefoundation/1521267-cfallocatorgetcontext?language=objc).
#[inline]
#[doc(alias = "CFAllocatorGetContext")]
pub fn default_context() -> CFAllocatorContext {
unsafe {
let mut context = MaybeUninit::uninit();
sys::CFAllocatorGetContext(ptr::null(), context.as_mut_ptr());
context.assume_init()
}
}
/// Returns the context of this allocator.
///
/// See [documentation](https://developer.apple.com/documentation/corefoundation/1521267-cfallocatorgetcontext?language=objc).
#[inline]
#[doc(alias = "CFAllocatorGetContext")]
pub fn context(&self) -> CFAllocatorContext {
unsafe {
let mut context = MaybeUninit::uninit();
sys::CFAllocatorGetContext(self, context.as_mut_ptr());
context.assume_init()
}
}
}
/// Getting and setting the default allocator.
impl CFAllocator {
/// Gets the default allocator object for the current thread and retains it.
///
/// See [documentation](https://developer.apple.com/documentation/corefoundation/1521325-cfallocatorgetdefault?language=objc).
#[inline]
#[doc(alias = "CFAllocatorGetDefault")]
pub fn default() -> Arc<Self> {
unsafe { Arc::retain_raw(sys::CFAllocatorGetDefault()) }
}
/// Gets the default allocator object for the current thread without
/// retaining it.
///
/// See [documentation](https://developer.apple.com/documentation/corefoundation/1521325-cfallocatorgetdefault?language=objc).
///
/// # Safety
///
/// Ownership of the allocator returned by `CFAllocatorGetDefault` follows
/// [The Get Rule](https://developer.apple.com/library/archive/documentation/CoreFoundation/Conceptual/CFMemoryMgmt/Concepts/Ownership.html#//apple_ref/doc/uid/20001148-SW1),
/// so you do not own it and cannot be certain of the object's life span.
#[inline]
#[doc(alias = "CFAllocatorGetDefault")]
pub unsafe fn default_unretained<'a>() -> &'a Self {
&*sys::CFAllocatorGetDefault()
}
/// Sets this given allocator as the default for the current thread.
///
/// See [documentation](https://developer.apple.com/documentation/corefoundation/1521325-cfallocatorgetdefault?language=objc).
///
/// # Safety
///
/// This allocator must be able to deal with arbitrary allocation requests.
///
/// This allocator must never be released, even if another allocator
/// replaces it as the default.
#[inline]
#[doc(alias = "CFAllocatorSetDefault")]
pub unsafe fn make_default(&self) {
sys::CFAllocatorSetDefault(self);
}
}