[英]How do I use the Rust memory allocator for a C library that can be provided an allocator?
我正在将Rust绑定写入C库,该库可以选择使用第三方内存分配器。 它的界面如下所示:
struct allocator {
void*(*alloc)(void *old, uint);
void(*free)(void*);
};
我想,相应的Rust结构如下:
#[repr(C)]
#[derive(Copy, Clone, Debug, PartialEq)]
pub struct Allocator {
alloc: Option<extern "C" fn(*mut c_void, c_uint) -> *mut c_void>,
free: Option<extern "C" fn(*mut c_void)>,
}
如何实现这两个应该模仿分配器的extern函数? 我没有找到任何真正看起来像Rust中的allocator API(但我理解为什么),所以我很好奇是否有可能。
它并不像你想的那么容易。
分配方法在alloc
crate的heap
模块中公开。
创建一些包装器方法并填充结构是很简单的,但我们很快遇到了一个问题:
#![feature(heap_api)]
extern crate libc;
extern crate alloc;
use libc::{c_void, c_uint};
use alloc::heap;
#[repr(C)]
#[derive(Copy, Clone, Debug, PartialEq)]
pub struct Allocator {
alloc: Option<extern "C" fn(*mut c_void, c_uint) -> *mut c_void>,
free: Option<extern "C" fn(*mut c_void)>,
}
extern "C" fn alloc_ext(old: *mut c_void, size: c_uint) -> *mut c_void {
if old.is_null() {
heap::allocate(size as usize, align) as *mut c_void
} else {
heap::reallocate(old as *mut u8, old_size, size as usize, align) as *mut c_void
}
}
extern "C" fn free_ext(old: *mut c_void) {
heap::deallocate(old as *mut u8, old_size, align);
}
fn main() {
Allocator {
alloc: Some(alloc_ext),
free: Some(free_ext),
};
}
Rust分配器期望被告知任何先前分配的大小以及期望的对齐。 您匹配的API没有任何传递方式。
对齐应该 (我不是专家)可以硬编码某个值,比如16个字节。 尺寸比较棘手。 您可能需要窃取一些旧的C技巧并分配一些额外的空间来存储大小。然后您可以存储大小并返回指针。
一个完全未经测试的例子:
#![feature(alloc, heap_api)]
extern crate libc;
extern crate alloc;
use libc::{c_void, c_uint};
use alloc::heap;
use std::{mem, ptr};
#[repr(C)]
#[derive(Copy, Clone, Debug, PartialEq)]
pub struct Allocator {
alloc: Option<extern "C" fn(*mut c_void, c_uint) -> *mut c_void>,
free: Option<extern "C" fn(*mut c_void)>,
}
const ALIGNMENT: usize = 16;
extern "C" fn alloc_ext(old: *mut c_void, size: c_uint) -> *mut c_void {
unsafe {
// Should check for integer overflow
let size_size = mem::size_of::<usize>();
let size = size as usize + size_size;
let memory = if old.is_null() {
heap::allocate(size, ALIGNMENT)
} else {
let old = old as *mut u8;
let old = old.offset(-(size_size as isize));
let old_size = *(old as *const usize);
heap::reallocate(old, old_size, size, ALIGNMENT)
};
*(memory as *mut usize) = size;
memory.offset(size_size as isize) as *mut c_void
}
}
extern "C" fn free_ext(old: *mut c_void) {
if old.is_null() { return }
unsafe {
let size_size = mem::size_of::<usize>();
let old = old as *mut u8;
let old = old.offset(-(size_size as isize));
let old_size = *(old as *const usize);
heap::deallocate(old as *mut u8, old_size, ALIGNMENT);
}
}
fn main() {
Allocator {
alloc: Some(alloc_ext),
free: Some(free_ext),
};
let pointer = alloc_ext(ptr::null_mut(), 54);
let pointer = alloc_ext(pointer, 105);
free_ext(pointer);
}
是不是[...... 使用
Vec
作为分配器 ...]更高级别的解决方案?
这当然是可能的,但我不完全确定如何使用重新分配。 您还必须跟踪Vec
的大小和容量,以便重新构建它以重新分配/丢弃它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.