Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API for allocating objects whose size is unknown at compiletime #2

Open
mystor opened this issue May 19, 2015 · 8 comments
Open

API for allocating objects whose size is unknown at compiletime #2

mystor opened this issue May 19, 2015 · 8 comments

Comments

@mystor
Copy link
Collaborator

mystor commented May 19, 2015

Right now, the only way to allocate is through the safe Gc::new() interface which allocates based on a generic type parameter. It would be nice to also have an unsafe Gc::alloc() interface or similar which allows allocating objects whose size isn't known at compile time.

@Manishearth
Copy link
Owner

I wonder if this is easily done without DST (dynamic sized types) being properly supported in rustc.

But yeah, Gc<Trait> would be nice.

/me mumbles something about becoming Java

@mystor
Copy link
Collaborator Author

mystor commented May 19, 2015

I'm also thinking of just supporting a raw api like:

fn alloc(size: usize, trace: fn (*const u8), root: fn (*const u8), unroot: fn (*const u8), drop: fn (*mut u8)) -> RawGc {}

// -- or --

struct GcHandlers { /* The above methods */ }
fn alloc(size: usize, handler: GcHandlers) -> RawGc {}

or something like that, which allows you to construct a garbage collected pointer from raw data.

It would be pretty darned unsafe, but could be useful for some persistent data structures.

@Manishearth
Copy link
Owner

Hm, that could work. But I suspect that we support just enough DST to make Gc<Trait> work like Box<Trait> and use vtables for dispatch. Not sure.

@Manishearth
Copy link
Owner

(It might be as simple as just putting a ?Sized bound on all the Ts)

@mystor
Copy link
Collaborator Author

mystor commented May 19, 2015

I'll try the ?Sized bounds later today. I think I also want to add that as an unsafe API as well. I have a use-case in mind where I'm going to have to interoperate with non-rust code with this API, and I need an API like that to do it, as non-rust code can't allocate right now.

@Manishearth
Copy link
Owner

I think we should focus on soundness first and try to break the GC with tests :) But adding bounds like this is easy enough so go ahead!

I had a closer look at the code/design, and while there are some things which could be improved (I'll comment later when I have the time or just fix directly), it looks sound to me. I think. Maybe. 😛

I also had a very illuminating talk with huon and aatch regarding stack scanning. I still think we should avoid it for this project, but perhaps it would be fun to design one of those GCs too once this is finished :)

@mystor
Copy link
Collaborator Author

mystor commented May 19, 2015

Yeah. There are a lot of problems with stack scanning, especially if we decide to let people put garbage collected pointers inside of boxes (which means that the pointer isn't on the stack). I think our mechanisms are the least likely to be unsound, but we should do some more tests.

@andersk
Copy link
Collaborator

andersk commented Dec 21, 2022

Rust is unable to convert [T] or even [u8] to dyn Trace, because fat pointer metadata has room for a vtable pointer or a length but not both (rust-lang/rust#61977). So we need a different representation than GcBox<dyn Trace> if we want this to be possible.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants