| 1 |
// Copyright 2013 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 |
|
| 10 |
//! Core Foundation byte buffers. |
| 11 |
|
| 12 |
use core_foundation_sys::base::kCFAllocatorDefault; |
| 13 |
use core_foundation_sys::base::CFIndex; |
| 14 |
pub use core_foundation_sys::data::*; |
| 15 |
use std::ops::Deref; |
| 16 |
use std::slice; |
| 17 |
use std::sync::Arc; |
| 18 |
|
| 19 |
use crate::base::{CFIndexConvertible, TCFType}; |
| 20 |
|
| 21 |
declare_TCFType! { |
| 22 |
/// A byte buffer. |
| 23 |
CFData, CFDataRef |
| 24 |
} |
| 25 |
impl_TCFType!(CFData, CFDataRef, CFDataGetTypeID); |
| 26 |
impl_CFTypeDescription!(CFData); |
| 27 |
|
| 28 |
impl CFData { |
| 29 |
/// Creates a [`CFData`] around a copy `buffer` |
| 30 |
pub fn from_buffer(buffer: &[u8]) -> CFData { |
| 31 |
unsafe { |
| 32 |
let data_ref = CFDataCreate( |
| 33 |
kCFAllocatorDefault, |
| 34 |
buffer.as_ptr(), |
| 35 |
buffer.len().to_CFIndex(), |
| 36 |
); |
| 37 |
TCFType::wrap_under_create_rule(data_ref) |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
/// Creates a [`CFData`] referencing `buffer` without creating a copy |
| 42 |
pub fn from_arc<T: AsRef<[u8]> + Sync + Send>(buffer: Arc<T>) -> Self { |
| 43 |
use crate::base::{CFAllocator, CFAllocatorContext}; |
| 44 |
use std::os::raw::c_void; |
| 45 |
|
| 46 |
unsafe { |
| 47 |
let ptr = (*buffer).as_ref().as_ptr() as *const _; |
| 48 |
let len = (*buffer).as_ref().len().to_CFIndex(); |
| 49 |
let info = Arc::into_raw(buffer) as *mut c_void; |
| 50 |
|
| 51 |
extern "C" fn deallocate<T>(_: *mut c_void, info: *mut c_void) { |
| 52 |
unsafe { |
| 53 |
drop(Arc::from_raw(info as *mut T)); |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
// Use a separate allocator for each allocation because |
| 58 |
// we need `info` to do the deallocation vs. `ptr` |
| 59 |
let allocator = CFAllocator::new(CFAllocatorContext { |
| 60 |
info, |
| 61 |
version: 0, |
| 62 |
retain: None, |
| 63 |
reallocate: None, |
| 64 |
release: None, |
| 65 |
copyDescription: None, |
| 66 |
allocate: None, |
| 67 |
deallocate: Some(deallocate::<T>), |
| 68 |
preferredSize: None, |
| 69 |
}); |
| 70 |
let data_ref = CFDataCreateWithBytesNoCopy( |
| 71 |
kCFAllocatorDefault, |
| 72 |
ptr, |
| 73 |
len, |
| 74 |
allocator.as_CFTypeRef(), |
| 75 |
); |
| 76 |
TCFType::wrap_under_create_rule(data_ref) |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
/// Returns a pointer to the underlying bytes in this data. Note that this byte buffer is |
| 81 |
/// read-only. |
| 82 |
#[inline] |
| 83 |
pub fn bytes(&self) -> &[u8] { |
| 84 |
unsafe { |
| 85 |
let ptr = CFDataGetBytePtr(self.0); |
| 86 |
// Rust slice must never have a NULL pointer |
| 87 |
if ptr.is_null() { |
| 88 |
return &[]; |
| 89 |
} |
| 90 |
slice::from_raw_parts(ptr, self.len() as usize) |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
/// Returns the length of this byte buffer. |
| 95 |
#[inline] |
| 96 |
pub fn len(&self) -> CFIndex { |
| 97 |
unsafe { CFDataGetLength(self.0) } |
| 98 |
} |
| 99 |
|
| 100 |
/// Returns `true` if this byte buffer is empty. |
| 101 |
#[inline] |
| 102 |
pub fn is_empty(&self) -> bool { |
| 103 |
self.len() == 0 |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
impl Deref for CFData { |
| 108 |
type Target = [u8]; |
| 109 |
|
| 110 |
#[inline] |
| 111 |
fn deref(&self) -> &[u8] { |
| 112 |
self.bytes() |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
#[cfg(test)] |
| 117 |
mod test { |
| 118 |
use super::CFData; |
| 119 |
use std::sync::Arc; |
| 120 |
|
| 121 |
#[test] |
| 122 |
fn test_data_provider() { |
| 123 |
let l = vec![5]; |
| 124 |
CFData::from_arc(Arc::new(l)); |
| 125 |
|
| 126 |
let l = vec![5]; |
| 127 |
CFData::from_arc(Arc::new(l.into_boxed_slice())); |
| 128 |
|
| 129 |
// Make sure the buffer is actually dropped |
| 130 |
use std::sync::atomic::{AtomicBool, Ordering::SeqCst}; |
| 131 |
struct VecWrapper { |
| 132 |
inner: Vec<u8>, |
| 133 |
dropped: Arc<AtomicBool>, |
| 134 |
} |
| 135 |
|
| 136 |
impl Drop for VecWrapper { |
| 137 |
fn drop(&mut self) { |
| 138 |
self.dropped.store(true, SeqCst) |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
impl std::convert::AsRef<[u8]> for VecWrapper { |
| 143 |
fn as_ref(&self) -> &[u8] { |
| 144 |
&self.inner |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
let dropped = Arc::new(AtomicBool::default()); |
| 149 |
let l = Arc::new(VecWrapper { |
| 150 |
inner: vec![5], |
| 151 |
dropped: dropped.clone(), |
| 152 |
}); |
| 153 |
let m = l.clone(); |
| 154 |
let dp = CFData::from_arc(l); |
| 155 |
drop(m); |
| 156 |
assert!(!dropped.load(SeqCst)); |
| 157 |
drop(dp); |
| 158 |
assert!(dropped.load(SeqCst)) |
| 159 |
} |
| 160 |
} |