Skip to content

GlobalAlloc::realloc’s parameters are backward #3

Closed
@SimonSapin

Description

@SimonSapin

The GlobalAlloc trait is stable with this method:

unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8

(And similarly the std::alloc::realloc function.)

Note new_size: usize instead of new_layout: Layout. This is because this method does not support changing the alignment.

When (re)allocation fails (when the method returns null), the caller typically wants to call handle_alloc_error which takes a Layout. This value needs to be created with code like Layout::from_size_align(new_size, layout.align()).unwrap(). It would be better if the value passed to the error handler was already available. This would be the case if the parameters to realloc were old_size: usize, new_layout: Layout instead.

For the Alloc trait, we could:

  • Keep consistency with GlobalAlloc, or
  • Change to old_size: usize, new_layout: Layout, or
  • If we also decide to lift the same-alignment restriction (which should be discussed in Support reallocating to a different alignment? #5), then the signature becomes old_layout: Layout, new_layout: Layout and this issue is moot.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-Alloc TraitIssues regarding the Alloc traitDiscussionIssues containing a specific idea, which needs to be discussed in the WG

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions