-
Notifications
You must be signed in to change notification settings - Fork 9
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
Idea: Make the pointer a generic associated type #113
Comments
Typical relative pointers store address offset relative to the pointer's address. Therefore it is not reasonable to return such pointer from allocator. Move invalidates it. Instead you can use allocator to fetch real pointer and then construct relative pointer, knowing relative pointer's address in advance. |
A move will only invalidate the pointer if its offset is relative to where the pointer itself is stored. But that isn't the only type of relative pointer that exists. I can write an arena allocator that sub-allocates from a contiguous chunk of memory (acquired from the global allocator), where
That issue does not apply to all kinds of relative pointer, but you've brought up a good point about move semantics. We can address that by requiring the associated
This doesn't accomplish anything. I want to be able to store a I've already written that arena allocator, but I can't use it how I want with What would prevent us from doing the following?
|
It's unsafe to read or write during that move without pinning the pointer, however. Not sure you can properly model this via the allocator trait as-designed. I could see this combined with a |
What I meant to say was that the byte index (the offset, what the (edit: This is why allocator-agnostic containers would need to store
Isn't it fine as long as actions like "moving the arena" require |
Even a
Not sufficient. You have to notify the allocator it's safe to move again as well. Unfortunately, this ends up functioning more like a mutex lock than a mere reference box, so it'd need some more implicit stuff.
In theory, yes, due to |
I've just been using
What are the other implicit invariants? If it's valid with I've written an allocator like I've described, and I know I could use it in specific scenarios1. My issue is I can't allocate collections from From what I see, my allocator would satisfy the safety invariants of Footnotes
|
I never said it failed to satisfy the proposed modified And the move for such relatively-positioned memory is executed not by the caller, but by the allocator. |
There are lots of applications for custom allocation strategies.
Video games will employ custom allocators in many places to improve performance. One class of strategies used by games that is currently inhibited by
std
are ones that involve relative pointers.Think about a classic video game emulator. It might allocate one big chunk of memory upfront to serve as the "system RAM" (e.g. Gameboy Advance had 384 KiB of RAM). The emulator can then sub-allocate from that chunk for collections like
Vec<T>
. If the pointer held by aVec<T>
was the index of a byte within the chunk rather than its absolute address, the emulator could memcpy the entire chunk to cheaply create a perfect snapshot/savestate. The move/copy would not invalidate the pointer.Similar tools can already be found in the Rust ecosystem.
The
rkyv
crate is built from the same principle. Its serializer replaces pointers with "self-relative" pointers (and replaces structs with stable#[repr(C)]
variants) so that you can later load the serialized blob from the filesystem and read from it directly without having to deserialize it first (which is very expensive), similar to a memory-mapped file.We could do similar things with general allocation, but the problem is the
Allocator
trait is locked toNonNull
, so it can't be used to return non-standard pointers. Likewise, all containers instd
are hardcoded to hold aNonNull<T>
/*mut T
.If possible, I'd like to see something to this effect.
i.e.
Then the collections from
std
could also be made generic and storeA::Pointer
.So a few questions:
The text was updated successfully, but these errors were encountered: