#malloc #box #free #cstr #array-string

no-std mbox

malloc-based box. Supports wrapping pointers or null-terminated strings returned from malloc as a Rust type, which will be free'd on drop

2 unstable releases

0.7.1 Mar 8, 2024
0.6.1 Mar 8, 2024
0.6.0 Mar 31, 2021
0.5.0 Apr 10, 2018
0.1.2 Jul 5, 2016

#21 in FFI

Download history 2799/week @ 2024-07-20 3355/week @ 2024-07-27 2909/week @ 2024-08-03 3533/week @ 2024-08-10 3473/week @ 2024-08-17 3778/week @ 2024-08-24 2711/week @ 2024-08-31 2887/week @ 2024-09-07 2609/week @ 2024-09-14 2973/week @ 2024-09-21 3193/week @ 2024-09-28 2940/week @ 2024-10-05 2792/week @ 2024-10-12 3474/week @ 2024-10-19 2919/week @ 2024-10-26 2734/week @ 2024-11-02

12,346 downloads per month
Used in 15 crates (2 directly)

MIT license

66KB
1.5K SLoC

mbox: malloc-based box

Crates.io docs.rs Build status MIT

This crate provides structures that wrap pointers returned from malloc as a Box, and automatically free them on drop. These types allow you to interact with pointers and null-terminated strings and arrays in a Rusty style.

Examples

extern crate libc;
extern crate mbox;

use libc::{c_char, malloc, strcpy};
use mbox::MString;

// Assume we have a C function that returns a malloc'ed string.
unsafe extern "C" fn create_str() -> *mut c_char {
    let ptr = malloc(12) as *mut c_char;
    strcpy(ptr, b"Hello world\0".as_ptr() as *const c_char);
    ptr
}

fn main() {
    // we wrap the null-terminated string into an MString.
    let string = unsafe { MString::from_raw_unchecked(create_str()) };

    // check the content is expected.
    assert_eq!(&*string, "Hello world");

    // the string will be dropped by `free` after the code is done.
}

Note: This crate does not support Windows in general.

Pointers in Rust are required to be aligned to be sound. However, there is no API on Windows that are both compatible with free() and supports aligned-malloc.

Because the primary purpose of this crate is interoperability with C code working with malloc(), it is impossible for us to switch to the safe variant like _aligned_malloc() (which requires _aligned_free()).

On Windows, trying to use MBox<T> or MArray<T> with T's alignment not equal to 1 will not compile.

# #[cfg(not(windows))] { _ };

use mbox::MBox;
let value = MBox::new(1_u64); // will not compile,

Installation

Add this to your Cargo.toml:

[dependencies]
mbox = "0.7"

Usage

This crate provides three main types, all of which uses the system's malloc/free as the allocator.

  • MBox<T> — Similar to Box<T>.
  • MString — Similar to std::ffi::CString.
  • MArray<T> — A null-terminated array, which can be used to represent e.g. array of C strings terminated by a null pointer.

#![no_std]

You may compile mbox and disable the std feature to not link to std (it will still link to core.

[dependencies]
mbox = { version = "0.7", default-features = false }

When #![no_std] is activated, you cannot convert an MString into a std::ffi::CStr, as the type simply does not exist 🙂.

Nightly

To use nightly-channel features (if you need support for custom dynamic-sized types), enable the nightly feature:

[dependencies]
mbox = { version = "0.7", features = ["nightly"] }

Migrating from other crates

Note that MBox does not support custom allocator. If the type requires custom allocation, MBox cannot serve you.

  • malloc_bufMalloc<T> is equivalent to MBox<T>. Note however that MBox<[T]>::from_raw_parts will not allow null, 0-length buffers; use a dangling pointer instead.

  • cbox — When not using a custom DisposeRef, the CSemiBox<'static, T> type is equivalent to MBox<T>, and CBox<T> is equivalent to &'static T.

  • c_vec — When using free as the destructor, CVec<T> is equivalent to MBox<[T]> and CSlice<T> as [T].

  • mallocedMalloced<T> is equivalent to MBox<T>. Note however that mbox depends on libc (more stable, but also longer build-time) and doesn't support dyn Any downcasting.

  • malloc-arrayHeapArray<T> is similar to MBox<T>, but this crate focuses more on raw memory management.

Dependencies

~53KB