如何将Rust内存分配器用于可以提供分配器的C库?

时间:2016-08-31 12:58:14

标签: c memory-management rust allocation

我正在将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(我明白为什么),所以我很好奇是否可能。

1 个答案:

答案 0 :(得分:6)

这并不像你想的那么容易。

分配方法在heap module of the alloc crate

中公开

创建一些包装器方法并填充结构是很简单的,但我们很快遇到了一个问题:

#![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没有任何方法可以传递它。

Alignment 应该(我不是专家)可以硬编码一些值,比如说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);
}
  

不是[... using Vec as an allocator ...]更高层次的解决方案?

这当然是可能的,但我不完全确定如何重新分配。您还必须跟踪Vec的大小和容量,以便重新构建它以重新分配/删除它。