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

Use a struct to represent a memory block #51

Closed
TimDiekmann opened this issue Mar 22, 2020 · 0 comments · Fixed by rust-lang/rust#70362
Closed

Use a struct to represent a memory block #51

TimDiekmann opened this issue Mar 22, 2020 · 0 comments · Fixed by rust-lang/rust#70362
Labels
A-Alloc Trait Issues regarding the Alloc trait Proposal

Comments

@TimDiekmann
Copy link
Member

TimDiekmann commented Mar 22, 2020

EDIT: Removed MemoryLayout<T> as #50 was closed.

While adding ReallocPlacement to get rid of grow_in_place and shrink_in_place I noticed, that neither of them returning a pointer, so the signatures can't be merged.
If we restructure the signatures of AllocRef a bit, we'll get a more convenient interface. First, I'll introduce a new struct:

struct MemoryBlock {
    ptr: Unique<u8>,
    layout: Layout,
}

Now, the signatures of AllocRef can be changed:

pub unsafe trait AllocRef {
    fn alloc(
        &mut self,
        layout: Layout,
        init: AllocInit,
    ) -> Result<MemoryBlock, AllocError>;

    unsafe fn dealloc(&mut self, memory: MemoryBlock);

    unsafe fn grow(
        &mut self,
        memory: &mut MemoryBlock,
        new_size: usize,
        placement: ReallocPlacement,
        init: AllocInit,
    ) -> Result<(), AllocError> { ... }

    unsafe fn shrink(
        &mut self,
        memory: &mut MemoryBlock,
        new_size: usize,
        placement: ReallocPlacement,
    ) -> Result<(), AllocError> { ... }
}

This API feels much more rusty. It returns a block when allocating, it's passed as mutable reference, when it is changed, and it's moved out of scope when deallocating.

@TimDiekmann TimDiekmann added A-Alloc Trait Issues regarding the Alloc trait Proposal labels Mar 22, 2020
@TimDiekmann TimDiekmann mentioned this issue Mar 28, 2020
18 tasks
bors added a commit to rust-lang-ci/rust that referenced this issue Apr 2, 2020
Overhaul of the `AllocRef` trait to match allocator-wg's latest consens; Take 2

GitHub won't let me reopen rust-lang#69889 so I make a new PR.

In addition to rust-lang#69889 this fixes the unsoundness of `RawVec::into_box` when using allocators supporting overallocating. Also it uses `MemoryBlock` in `AllocRef` to unify `_in_place` methods by passing `&mut MemoryBlock`. Additionally, `RawVec` now checks for `size_of::<T>()` again and ignore every ZST. The internal capacity of `RawVec` isn't used by ZSTs anymore, as `into_box` now requires a length to be specified.

r? @Amanieu

fixes rust-lang/wg-allocators#38
fixes rust-lang/wg-allocators#41
fixes rust-lang/wg-allocators#44
fixes rust-lang/wg-allocators#51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Alloc Trait Issues regarding the Alloc trait Proposal
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant