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 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404
//! A `malloc`-ed box pointer type, brought to you by
//! [@NikolaiVazquez](https://twitter.com/NikolaiVazquez)!
//!
//! # Table of Contents
//!
//! 1. [Donate](#donate)
//! 2. [Usage](#usage)
//! 3. [MSRV](#msrv)
//! 4. [FFI Safety](#ffi-safety)
//! 5. [Alternatives](#alternatives)
//! 6. [License](#license)
//!
//! # Donate
//!
//! If this project is useful to you, please consider
//! [sponsoring me](https://github.com/sponsors/nvzqz) or
//! [donating directly](https://www.paypal.me/nvzqz)!
//!
//! Doing so enables me to create high-quality open source software like this. ❤️
//!
//! # Usage
//!
//! This library is available [on crates.io](https://crates.io/crates/malloced) and
//! can be used by adding the following to your project's
//! [`Cargo.toml`](https://doc.rust-lang.org/cargo/reference/manifest.html):
//!
//! ```toml
//! [dependencies]
//! malloced = "1.3.1"
//! ```
//!
//! The star of the show is [`Malloced`], [`Box`]-like pointer that calls `free` on
//! [`Drop`]:
//!
//! ```rust
//! use malloced::Malloced;
//! ```
//!
//! # MSRV
//!
//! This library's minimum supported Rust version (MSRV) is 1.64. A new version
//! requirement would result in a minor version update.
//!
//! # FFI Safety
//!
//! `Malloced<T>` is a `#[repr(transparent)]` wrapper over `NonNull<T>`, so it can
//! be safely used in C FFI. For example, the following is safe and even compiles
//! with the `improper_ctypes` lint enabled:
//!
//! ```rust
//! # use malloced::Malloced;
//! #[deny(improper_ctypes)]
//! extern "C" {
//! fn my_array_malloc() -> Malloced<[u8; 32]>;
//! }
//! ```
//!
//! # Alternatives
//!
//! - [`malloc_buf`](https://docs.rs/malloc_buf)
//! - [`mbox`](https://docs.rs/mbox)
//!
//! # License
//!
//! This project is released under either
//! [MIT License](https://github.com/nvzqz/malloced/blob/master/LICENSE-MIT) or
//! [Apache License (Version 2.0)](https://github.com/nvzqz/malloced/blob/master/LICENSE-APACHE)
//! at your choosing.
//!
//! [`Box`]: https://doc.rust-lang.org/std/boxed/struct.Box.html
//! [`Drop`]: https://doc.rust-lang.org/std/ops/trait.Drop.html
//! [`Malloced`]: struct.Malloced.html
#![cfg_attr(not(feature = "std"), no_std)]
#[cfg(test)]
extern crate alloc;
#[cfg(feature = "std")]
use std as core;
use core::{
any::Any,
ffi::{c_char, CStr},
marker::PhantomData,
mem,
mem::ManuallyDrop,
pin::Pin,
ptr::NonNull,
};
mod impls;
mod iter;
mod sys;
pub use iter::*;
/// A pointer type for `malloc`-ed heap allocation.
///
/// # Memory layout
///
/// So long as `T: Sized`, a `Malloced<T>` is guaranteed to be represented as a
/// single pointer and is also ABI-compatible with C pointers (i.e. the C type
/// `T*`). This means that if you have extern "C" Rust functions that will be
/// called from C, you can define those Rust functions using `Malloced<T>`
/// types, and use `T*` as corresponding type on the C side.
///
/// Regardless if `T: Sized`, a `Malloced<T>` is guaranteed to be ABI-compatible
/// with [`NonNull<T>`](https://doc.rust-lang.org/std/ptr/struct.NonNull.html).
#[repr(transparent)]
pub struct Malloced<T: ?Sized> {
ptr: NonNull<T>,
// Marks ownership of an instance of T.
_marker: PhantomData<T>,
}
impl<T> IntoIterator for Malloced<[T]> {
type Item = T;
type IntoIter = SliceIter<T>;
#[inline]
fn into_iter(self) -> Self::IntoIter {
unsafe {
let buf = self.ptr.cast::<T>();
let len = self.len();
mem::forget(self);
let ptr = buf.as_ptr();
let end = if mem::size_of::<T>() == 0 {
// Purposefully don't use `ptr.offset` because for slices with
// 0-size elements this would return the same pointer.
//
// Use wrapping arithmetic to avoid the requirement of the
// result pointer being in the same allocation.
(ptr as *mut i8).wrapping_add(len) as *mut T
} else {
ptr.add(len)
};
SliceIter {
buf,
marker: PhantomData,
ptr,
end,
}
}
}
}
/// Testing helpers.
#[cfg(test)]
impl<T> Malloced<[T]> {
fn alloc(values: &[T]) -> Option<Self>
where
T: Copy,
{
let value_size = mem::size_of::<T>();
let alloc_size = values.len().checked_mul(value_size.max(1))?;
unsafe {
let buf = sys::malloc(alloc_size).cast::<T>();
if buf.is_null() {
return None;
}
for (i, &value) in values.iter().enumerate() {
let ptr: *mut T = if value_size == 0 {
buf.cast::<u8>().add(i).cast()
} else {
buf.add(i)
};
ptr.write(value);
}
Some(Malloced::slice_from_raw_parts(buf, values.len()))
}
}
}
impl<T: ?Sized> Malloced<T> {
/// Constructs an instance from a raw `malloc`-ed pointer.
///
/// # Safety
///
/// The data referenced by `ptr` must be valid and must have been allocated
/// by `malloc` so that it can be `free`-d on
/// [`Drop`](https://doc.rust-lang.org/std/ops/trait.Drop.html).
#[inline]
pub unsafe fn from_raw(ptr: *mut T) -> Self {
Self {
ptr: NonNull::new_unchecked(ptr),
_marker: PhantomData,
}
}
/// Consumes the instance, returning a wrapped raw pointer.
///
/// The pointer will be properly aligned and non-null.
#[inline]
pub fn into_raw(this: Self) -> *mut T {
Self::leak(this)
}
/// Converts a `Malloced<T>` into a `Pin<Malloced<T>>`
///
/// This conversion does not allocate on the heap and happens in place.
///
/// This is also available via
/// [`From`](https://doc.rust-lang.org/std/convert/trait.From.html).
#[inline]
pub fn into_pin(this: Self) -> Pin<Malloced<T>> {
// SAFETY: It's not possible to move or replace the insides of a
// `Pin<Malloced<T>>` when `T: !Unpin`, so it's safe to pin it directly
// without any additional requirements.
unsafe { Pin::new_unchecked(this) }
}
/// Consumes and leaks the instance, returning a mutable reference,
/// `&'a mut T`.
///
/// Note that the type `T` must outlive the chosen lifetime `'a`. If the
/// type has only static references, or none at all, then this may be chosen
/// to be `'static`.
///
/// This function is mainly useful for data that lives for the remainder of
/// the program's life. Dropping the returned reference will cause a memory
/// leak. If this is not acceptable, the reference should first be wrapped
/// with the [`Malloced::from_raw`](#method.from_raw) function producing a
/// `Malloced`. This `Malloced` can then be dropped which will properly
/// destroy `T` and `free` the allocated memory.
///
/// Note: this is an associated function, which means that you have to call
/// it as `Malloced::leak(this)` instead of `this.leak()`. This is so that
/// there is no conflict with a method on the inner type.
#[inline]
pub fn leak<'a>(this: Self) -> &'a mut T
where
T: 'a,
{
unsafe { &mut *ManuallyDrop::new(this).ptr.as_ptr() }
}
/// Returns an immutable raw pointer to the data.
#[inline]
pub fn as_ptr(this: &Self) -> *const T {
this.ptr.as_ptr()
}
/// Returns a mutable raw pointer to the data.
#[inline]
pub fn as_mut_ptr(this: &mut Self) -> *mut T {
this.ptr.as_ptr()
}
// TODO: Implement `core::ops::CoerceUnsized`.
// See https://github.com/rust-lang/rust/issues/27732.
/// Erases the static type `T`.
#[inline]
pub fn into_any(this: Self) -> Malloced<dyn Any>
where
T: Sized + Any,
{
let ptr = this.ptr.as_ptr() as *mut dyn Any;
mem::forget(this);
unsafe { Malloced::from_raw(ptr) }
}
/// Erases the static type `T`.
#[inline]
pub fn into_any_send(this: Self) -> Malloced<dyn Any + Send + Sync>
where
T: Sized + Any + Send + Sync,
{
let ptr = this.ptr.as_ptr() as *mut (dyn Any + Send + Sync);
mem::forget(this);
unsafe { Malloced::from_raw(ptr) }
}
}
impl<T> Malloced<[T]> {
/// Constructs an instance for a slice from a pointer and a length.
///
/// # Safety
///
/// Behavior is undefined if any of the following conditions are violated:
///
/// - `data` must have been allocated by `malloc` so that it can be `free`-d
/// on [`Drop`](https://doc.rust-lang.org/std/ops/trait.Drop.html).
///
/// - `data` must be
/// [valid](https://doc.rust-lang.org/std/ptr/index.html#safety) for both
/// reads and writes for `len * mem::size_of::<T>()` many bytes, and it
/// must be properly aligned. This means in particular:
///
/// - The entire memory range of this slice must be contained within a
/// single allocated object! Slices can never span across multiple
/// allocated objects.
///
/// - `data` must be non-null and aligned even for zero-length slices.
/// One reason for this is that enum layout optimizations may rely on
/// references (including slices of any length) being aligned and
/// non-null to distinguish them from other data. You can obtain a
/// pointer that is usable as `data` for zero-length slices using
/// [`NonNull::dangling()`](https://doc.rust-lang.org/std/ptr/struct.NonNull.html#method.dangling).
///
/// - `data` must point to `len` consecutive properly initialized values of
/// type `T`.
///
/// - The total size `len * mem::size_of::<T>()` of the slice must be no
/// larger than `isize::MAX`. See the safety documentation of
/// [`pointer::offset`](https://doc.rust-lang.org/std/primitive.pointer.html#method.offset).
///
/// See
/// [`slice::from_raw_parts_mut`](https://doc.rust-lang.org/std/slice/fn.from_raw_parts_mut.html)
/// for details.
#[inline]
pub unsafe fn slice_from_raw_parts(data: *mut T, len: usize) -> Self {
Self::from_raw(core::ptr::slice_from_raw_parts_mut(data, len))
}
}
impl Malloced<CStr> {
/// Wraps a raw `malloc`ed C string with a safe owned C string wrapper.
///
/// # Safety
///
/// See [`CStr::from_ptr` safety docs](CStr::from_ptr).
#[inline]
pub unsafe fn from_ptr(ptr: *mut c_char) -> Self {
// If `&CStr` is a thin pointer, use a dummy length that is discarded.
let len = if mem::size_of::<*mut CStr>() == mem::size_of::<*mut c_char>() {
1
} else {
CStr::from_ptr(ptr).to_bytes_with_nul().len()
};
let ptr = core::ptr::slice_from_raw_parts_mut(ptr, len) as *mut CStr;
Self::from_raw(ptr)
}
}
impl Malloced<dyn Any> {
/// Attempt to downcast the instance to a concrete type.
#[inline]
pub fn downcast<T: Any>(self) -> Result<Malloced<T>, Self> {
if self.is::<T>() {
let raw: *mut dyn Any = Malloced::into_raw(self);
Ok(unsafe { Malloced::from_raw(raw as *mut T) })
} else {
Err(self)
}
}
}
impl Malloced<dyn Any + Send> {
/// Attempt to downcast the instance to a concrete type.
#[inline]
pub fn downcast<T: Any>(self) -> Result<Malloced<T>, Self> {
if self.is::<T>() {
let raw: *mut (dyn Any + Send) = Malloced::into_raw(self);
Ok(unsafe { Malloced::from_raw(raw as *mut T) })
} else {
Err(self)
}
}
}
impl Malloced<dyn Any + Send + Sync> {
/// Attempt to downcast the instance to a concrete type.
#[inline]
pub fn downcast<T: Any>(self) -> Result<Malloced<T>, Self> {
if self.is::<T>() {
let raw: *mut (dyn Any + Send + Sync) = Malloced::into_raw(self);
Ok(unsafe { Malloced::from_raw(raw as *mut T) })
} else {
Err(self)
}
}
}
#[cfg(test)]
mod tests {
use super::*;
mod c_str {
use super::*;
#[test]
fn from_ptr() {
let buf = Malloced::<[c_char]>::alloc(&[b'h' as _, b'i' as _, 0]).unwrap();
let ptr = ManuallyDrop::new(buf).ptr.as_ptr() as *mut c_char;
let result = unsafe { Malloced::<CStr>::from_ptr(ptr) };
assert_eq!(result.to_bytes(), b"hi");
}
}
}