SFBdragon / talc

A fast and flexible allocator for no_std and WebAssembly
https://crates.io/crates/talc
MIT License
419 stars 9 forks source link

RFC: C FFI API #13

Closed SFBdragon closed 9 months ago

SFBdragon commented 1 year ago

I'm thinking of adding a C interface for other languages to make use of the project, but I'm not familiar with the conventions for nostd C allocators and don't know if there's any interest in it.

pub extern fn alloc(talc: *mut CTalc, size: c_size_t, align: c_size_t) -> *mut c_void;
pub extern fn dealloc(talc: *mut CTalc, size: c_size_t);
pub extern fn grow(talc: *mut CTalc, old_size: c_size_t, new_size: c_size_t) -> *mut c_void;
pub extern fn shrink(talc: *mut CTalc, old_size: c_size_t, new_size: c_size_t) -> *mut c_void;

I'm aware malloc/free/etc are the norm, but I'm concerned about exporting conflicting symbols with different function signatures which may cause issues, but I'm not sure.

type CTalc = Talck<spin::Mutex<()>, InitElseCallFnOnOom>;
pub extern fn new_talc(span: Span, oom_handler: extern fn(talc: *mut CTalc, size: c_size_t, align: c_size_t)) -> CTalc;
pub extern fn extend(talc: *mut CTalc, span: Span);
pub extern fn truncate(talc: *mut CTalc, span: Span);

pub extern fn get_arena(talc: *mut CTalc) -> Span;
pub extern fn get_allocatable_span(talc: *mut CTalc) -> Span;
pub extern fn get_allocated_span(talc: *mut CTalc) -> Span;

I think including Span in the API could be okay. It certainly makes returning that data easier, but perhaps it's not such a good idea for other reasons?

Let me know if you're interested.

SFBdragon commented 9 months ago

Closing this for now as I doubt there's much interest.