Skip to content

std::alloc::Global.shrink returns an error if realloc returns null #79603

Open
@mahkoh

Description

@mahkoh

Some allocators do not (wish to) permit reducing the size of a large allocation below a certain threshold. This leaves the GlobalAlloc::realloc implementation with two choices to handle such shrink operations:

  • Create a new, smaller allocation, copy the contents from the large to the small allocation, free the large allocation
  • Return a null pointer to indicate that the allocation has not changed

The second choice corresponds to the allocator behavior. Unfortunately std::alloc::Global.shrink translates this into an error instead of returning the old pointer:

let raw_ptr = realloc(ptr.as_ptr(), old_layout, new_size);
let ptr = NonNull::new(raw_ptr).ok_or(AllocError)?;

Downstream (e.g. RawVec) aborts the process if this happens even though there is still plenty of memory available.

Until GlobalAlloc has been completely replaced by AllocRef, I believe that it would be better if std::alloc::Global.shrink handled null pointer return values of the global allocator by returning the original allocation.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-allocatorsArea: Custom and system allocatorsC-bugCategory: This is a bug.T-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions