core_foundation_sys/
base.rs

1// Copyright 2013-2015 The Servo Project Developers. See the COPYRIGHT
2// file at the top-level directory of this distribution.
3//
4// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7// option. This file may not be copied, modified, or distributed
8// except according to those terms.
9
10use crate::string::CFStringRef;
11use std::cmp::Ordering;
12use std::os::raw::{c_int, c_short, c_uchar, c_uint, c_ushort, c_void};
13
14pub type Boolean = u8;
15pub type mach_port_t = c_uint;
16pub type CFAllocatorRef = *const c_void;
17pub type CFNullRef = *const c_void;
18pub type CFTypeRef = *const c_void;
19pub type ConstStr255Param = *const c_uchar;
20pub type StringPtr = *mut c_uchar;
21pub type ConstStringPtr = *const c_uchar;
22pub type OSStatus = i32;
23pub type UInt8 = c_uchar;
24pub type UInt16 = c_ushort;
25pub type SInt16 = c_short;
26pub type SInt32 = c_int;
27pub type UInt32 = c_uint;
28pub type CFTypeID = usize;
29pub type CFOptionFlags = usize;
30pub type CFHashCode = usize;
31pub type CFIndex = isize;
32pub type LangCode = SInt16;
33pub type RegionCode = SInt16;
34pub type UTF32Char = c_uint;
35pub type UTF16Char = c_ushort;
36pub type UTF8Char = c_uchar;
37
38#[repr(isize)]
39#[derive(Clone, Copy, Debug, PartialEq)]
40pub enum CFComparisonResult {
41    LessThan = -1,
42    EqualTo = 0,
43    GreaterThan = 1,
44}
45
46pub type CFComparatorFunction = extern "C" fn(
47    val1: *const c_void,
48    val2: *const c_void,
49    context: *mut c_void,
50) -> CFComparisonResult;
51
52impl From<CFComparisonResult> for Ordering {
53    fn from(val: CFComparisonResult) -> Self {
54        match val {
55            CFComparisonResult::LessThan => Ordering::Less,
56            CFComparisonResult::EqualTo => Ordering::Equal,
57            CFComparisonResult::GreaterThan => Ordering::Greater,
58        }
59    }
60}
61
62#[repr(C)]
63#[derive(Clone, Copy, Debug, PartialEq)]
64pub struct CFRange {
65    pub location: CFIndex,
66    pub length: CFIndex,
67}
68
69// for back-compat
70impl CFRange {
71    pub fn init(location: CFIndex, length: CFIndex) -> CFRange {
72        CFRange { location, length }
73    }
74}
75
76pub type CFAllocatorRetainCallBack = extern "C" fn(info: *mut c_void) -> *mut c_void;
77pub type CFAllocatorReleaseCallBack = extern "C" fn(info: *mut c_void);
78pub type CFAllocatorCopyDescriptionCallBack = extern "C" fn(info: *mut c_void) -> CFStringRef;
79pub type CFAllocatorAllocateCallBack =
80    extern "C" fn(allocSize: CFIndex, hint: CFOptionFlags, info: *mut c_void) -> *mut c_void;
81pub type CFAllocatorReallocateCallBack = extern "C" fn(
82    ptr: *mut c_void,
83    newsize: CFIndex,
84    hint: CFOptionFlags,
85    info: *mut c_void,
86) -> *mut c_void;
87pub type CFAllocatorDeallocateCallBack = extern "C" fn(ptr: *mut c_void, info: *mut c_void);
88pub type CFAllocatorPreferredSizeCallBack =
89    extern "C" fn(size: CFIndex, hint: CFOptionFlags, info: *mut c_void) -> CFIndex;
90
91#[repr(C)]
92#[derive(Clone, Copy, Debug)]
93pub struct CFAllocatorContext {
94    pub version: CFIndex,
95    pub info: *mut c_void,
96    pub retain: Option<CFAllocatorRetainCallBack>,
97    pub release: Option<CFAllocatorReleaseCallBack>,
98    pub copyDescription: Option<CFAllocatorCopyDescriptionCallBack>,
99    pub allocate: Option<CFAllocatorAllocateCallBack>,
100    pub reallocate: Option<CFAllocatorReallocateCallBack>,
101    pub deallocate: Option<CFAllocatorDeallocateCallBack>,
102    pub preferredSize: Option<CFAllocatorPreferredSizeCallBack>,
103}
104
105/// Trait for all types which are Core Foundation reference types.
106pub trait TCFTypeRef {
107    fn as_void_ptr(&self) -> *const c_void;
108
109    unsafe fn from_void_ptr(ptr: *const c_void) -> Self;
110}
111
112impl<T> TCFTypeRef for *const T {
113    fn as_void_ptr(&self) -> *const c_void {
114        (*self) as *const c_void
115    }
116
117    unsafe fn from_void_ptr(ptr: *const c_void) -> Self {
118        ptr as *const T
119    }
120}
121
122impl<T> TCFTypeRef for *mut T {
123    fn as_void_ptr(&self) -> *const c_void {
124        (*self) as *const T as *const c_void
125    }
126
127    unsafe fn from_void_ptr(ptr: *const c_void) -> Self {
128        ptr as *const T as *mut T
129    }
130}
131
132/// Constant used by some functions to indicate failed searches.
133pub static kCFNotFound: CFIndex = -1;
134
135extern "C" {
136    /*
137     * CFBase.h
138     */
139
140    /* CFAllocator Reference */
141
142    pub static kCFAllocatorDefault: CFAllocatorRef;
143    pub static kCFAllocatorSystemDefault: CFAllocatorRef;
144    pub static kCFAllocatorMalloc: CFAllocatorRef;
145    pub static kCFAllocatorMallocZone: CFAllocatorRef;
146    pub static kCFAllocatorNull: CFAllocatorRef;
147    pub static kCFAllocatorUseContext: CFAllocatorRef;
148
149    pub fn CFAllocatorCreate(
150        allocator: CFAllocatorRef,
151        context: *mut CFAllocatorContext,
152    ) -> CFAllocatorRef;
153    pub fn CFAllocatorAllocate(
154        allocator: CFAllocatorRef,
155        size: CFIndex,
156        hint: CFOptionFlags,
157    ) -> *mut c_void;
158    pub fn CFAllocatorDeallocate(allocator: CFAllocatorRef, ptr: *mut c_void);
159    pub fn CFAllocatorGetPreferredSizeForSize(
160        allocator: CFAllocatorRef,
161        size: CFIndex,
162        hint: CFOptionFlags,
163    ) -> CFIndex;
164    pub fn CFAllocatorReallocate(
165        allocator: CFAllocatorRef,
166        ptr: *mut c_void,
167        newsize: CFIndex,
168        hint: CFOptionFlags,
169    ) -> *mut c_void;
170    pub fn CFAllocatorGetDefault() -> CFAllocatorRef;
171    pub fn CFAllocatorSetDefault(allocator: CFAllocatorRef);
172    pub fn CFAllocatorGetContext(allocator: CFAllocatorRef, context: *mut CFAllocatorContext);
173    pub fn CFAllocatorGetTypeID() -> CFTypeID;
174
175    /* CFNull Reference */
176
177    pub static kCFNull: CFNullRef;
178
179    pub fn CFNullGetTypeID() -> CFTypeID;
180
181    /* CFType Reference */
182
183    pub fn CFCopyTypeIDDescription(type_id: CFTypeID) -> CFStringRef;
184    pub fn CFGetAllocator(cf: CFTypeRef) -> CFAllocatorRef;
185    pub fn CFCopyDescription(cf: CFTypeRef) -> CFStringRef;
186    pub fn CFEqual(cf1: CFTypeRef, cf2: CFTypeRef) -> Boolean;
187    pub fn CFGetRetainCount(cf: CFTypeRef) -> CFIndex;
188    pub fn CFGetTypeID(cf: CFTypeRef) -> CFTypeID;
189    pub fn CFHash(cf: CFTypeRef) -> CFHashCode;
190    //fn CFMakeCollectable
191    pub fn CFRelease(cf: CFTypeRef);
192    pub fn CFRetain(cf: CFTypeRef) -> CFTypeRef;
193    pub fn CFShow(obj: CFTypeRef);
194
195    /* Base Utilities Reference */
196    // N.B. Some things missing here.
197}