3 ms·
Great! Do you have some links? I found this: https://doc.rust-lang.org/std/alloc/trait.Allocator.html https://doc.rust-lang.org/std/alloc/trait.Allocator.html
by jeff-davis 5y ago
Great! Do you have some links?
I found this: https://doc.rust-lang.org/std/alloc/trait.Allocator.html https://doc.rust-lang.org/std/alloc/trait.Allocator.html
but it doesn't look like it allows inspecting the size. I'd also like to know how it works to "move a value in" to a collection, does it actually do a memcpy into the collection's heap?
Also, is it really a separate instance of the allocator for the collection? That is, can I wipe all the memory for one hash table in O(1) without touching another hash table that uses the same allocator?
EDIT: It would also be nice if there was a way to gracefully handle OOM. Maybe if you could specify a maximum size for some heap, it could be a good way to test how your code fails on OOM (which can still happen in a lot of cases with some kernel configs).
- nynx 5y agoYeah, you'll have to add a method to whatever type you implement `Allocator` for that returns the size. When you call `Vec::push` or `HashMap::insert`, it'll allocate on the heap if there isn't already capacity for it in the already allocated space. If the custom allocator you make just allocates one large space from the global allocator, then you can deallocate the whole thing at once in the `Drop` implementation. There is limited documentation at the moment because the feature is still in flux, but take a look at `Vec::new_in` [0] to get an idea of how a collection can use an allocator. [0]: https://doc.rust-lang.org/std/vec/struct.Vec.html#method.new_in https://doc.rust-lang.org/std/vec/struct.Vec.html#method.new...
- deleted 5y ago[deleted]
- steveklabnik 5y ago(These APIs return Result, so an OOM would give an AllocError back)