snu*_*182 6 c memory-management allocation rust
我正在将Rust绑定写入C库,该库可以选择使用第三方内存分配器.它的界面如下所示:
struct allocator {
void*(*alloc)(void *old, uint);
void(*free)(void*);
};
Run Code Online (Sandbox Code Playgroud)
我想,相应的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)>,
}
Run Code Online (Sandbox Code Playgroud)
如何实现这两个应该模仿分配器的extern函数?我没有找到任何真正看起来像Rust中的allocator API(但我理解为什么),所以我很好奇是否有可能.
它并不像你想的那么容易.
的分配方法被暴露在heap所述的模块alloc箱.
创建一些包装器方法并填充结构是很简单的,但我们很快遇到了一个问题:
#![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),
};
}
Run Code Online (Sandbox Code Playgroud)
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);
}
Run Code Online (Sandbox Code Playgroud)
是不是[... 使用
Vec作为一个allocator ...]越是高层次的解决方案?
这当然是可能的,但我不完全确定它如何与重新分配一起工作.您还必须跟踪其大小和容量,Vec以便重新构建它以重新分配/删除它.
| 归档时间: |
|
| 查看次数: |
652 次 |
| 最近记录: |