如何在稳定的 Rust 中分配原始可变指针?

How can you allocate a raw mutable pointer in stable Rust?

我试图构建一个带有小字符串优化的自定义 String 类结构的简单实现。现在在稳定的 Rust 中允许联合,我想出了以下代码:

struct Large {
    capacity: usize,
    buffer: *mut u8,
}

struct Small([u8; 16]);

union Container {
    large: Large,
    small: Small,
}

struct MyString {
    len: usize,
    container: Container,
}

我似乎找不到分配 *mut u8 的方法。有可能在稳定的 Rust 中做吗?看起来使用 alloc::heap 是可行的,但它只能在夜间使用。

Box::into_raw()呢?

struct TypeMatches(*mut u8);
TypeMatches(Box::into_raw(Box::new(0u8)));

但是很难从您的代码片段中判断这是否是您真正需要的。你可能想要一个真正的分配器,你可以使用 libc::malloc with an as cast, as in this example.

有一个 memalloc crate 提供稳定的分配 API。它是通过Vec::with_capacity分配内存,然后提取指针来实现的:

let vec = Vec::with_capacity(cap);
let ptr = buf.as_mut_ptr();
mem::forget(vec);

要释放内存,请使用Vec::from_raw_parts

从 Rust 1.28 开始,std::alloc::alloc 是稳定的。

这里有一个示例,大致说明了如何使用它。

use std::{
    alloc::{self, Layout},
    cmp, mem, ptr, slice, str,
};

// This really should **not** be copied
#[derive(Copy, Clone)]
struct Large {
    capacity: usize,
    buffer: *mut u8,
}

// This really should **not** be copied
#[derive(Copy, Clone, Default)]
struct Small([u8; 16]);

union Container {
    large: Large,
    small: Small,
}

struct MyString {
    len: usize,
    container: Container,
}

impl MyString {
    fn new() -> Self {
        MyString {
            len: 0,
            container: Container {
                small: Small::default(),
            },
        }
    }

    fn as_buf(&self) -> &[u8] {
        unsafe {
            if self.len <= 16 {
                &self.container.small.0[..self.len]
            } else {
                slice::from_raw_parts(self.container.large.buffer, self.len)
            }
        }
    }

    pub fn as_str(&self) -> &str {
        unsafe { str::from_utf8_unchecked(self.as_buf()) }
    }

    // Not actually UTF-8 safe!
    fn push(&mut self, c: u8) {
        unsafe {
            use cmp::Ordering::*;

            match self.len.cmp(&16) {
                Less => {
                    self.container.small.0[self.len] = c;
                }
                Equal => {
                    let capacity = 17;
                    let layout = Layout::from_size_align(capacity, mem::align_of::<u8>())
                        .expect("Bad layout");

                    let buffer = alloc::alloc(layout);

                    {
                        let buf = self.as_buf();
                        ptr::copy_nonoverlapping(buf.as_ptr(), buffer, buf.len());
                    }

                    self.container.large = Large { capacity, buffer };

                    *self.container.large.buffer.offset(self.len as isize) = c;
                }
                Greater => {
                    let Large {
                        mut capacity,
                        buffer,
                    } = self.container.large;
                    capacity += 1;

                    let layout = Layout::from_size_align(capacity, mem::align_of::<u8>())
                        .expect("Bad layout");

                    let buffer = alloc::realloc(buffer, layout, capacity);

                    self.container.large = Large { capacity, buffer };

                    *self.container.large.buffer.offset(self.len as isize) = c;
                }
            }

            self.len += 1;
        }
    }
}

impl Drop for MyString {
    fn drop(&mut self) {
        unsafe {
            if self.len > 16 {
                let Large { capacity, buffer } = self.container.large;
                let layout =
                    Layout::from_size_align(capacity, mem::align_of::<u8>()).expect("Bad layout");
                alloc::dealloc(buffer, layout);
            }
        }
    }
}

fn main() {
    let mut s = MyString::new();

    for _ in 0..32 {
        s.push(b'a');
        println!("{}", s.as_str());
    }
}

我相信这段代码在分配方面是正确的,但在其他任何方面都不正确。和所有unsafe代码一样,自己验证一下。它也完全没有效率,因为它会为每个额外的字符重新分配。


如果您想分配 u8 的集合而不是单个 u8,您可以创建一个 Vec,然后将其转换为组成部分,例如通过调用 as_mut_ptr:

use std::mem;

fn main() {
    let mut foo = vec![0; 1024]; // or Vec::<u8>::with_capacity(1024);

    let ptr = foo.as_mut_ptr();
    let cap = foo.capacity();
    let len = foo.len();
    mem::forget(foo); // Avoid calling the destructor!

    let foo_again = unsafe { Vec::from_raw_parts(ptr, len, cap) }; // Rebuild it to drop it
    // Do *NOT* use `ptr` / `cap` / `len` anymore
}

Re 虽然分配有点痛苦;你必须转换回 Vec 并向前和向后跳整个舞蹈

也就是说,您的 Large 结构似乎缺少 length,这与容量不同。您可以只使用 Vec 而不是写出来。 我现在看到它在层次结构中向上了一点。

我想知道是否有一个完整的 String 会不会更容易,即使它的效率有点低,因为长度被重复计算...

union Container {
    large: String,
    small: Small,
}

另请参阅: