Closed
Description
PR #49669 adds a GlobalAlloc
trait, separate from Alloc
. This issue track the stabilization of this trait and some related APIs, to provide the ability to change the global allocator, as well as allocating memory without abusing Vec::with_capacity
+ mem::forget
.
Defined in or reexported from the std::alloc
module:
Update: below is the API proposed when this issue was first opened. The one being stabilized is at #49668 (comment).
/// #[global_allocator] can only be applied to a `static` item that implements this trait
pub unsafe trait GlobalAlloc {
unsafe fn alloc(&self, layout: Layout) -> *mut Opaque;
unsafe fn dealloc(&self, ptr: *mut Opaque, layout: Layout);
unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut Opaque {
// Default impl: self.alloc() and ptr::write_bytes()
}
unsafe fn realloc(&self, ptr: *mut Opaque, layout: Layout, new_size: usize) -> *mut Opaque {
// Default impl: self.alloc() and ptr::copy_nonoverlapping() and self.dealloc()
}
// More methods with default impls may be added in the future
}
extern {
pub type Opaque;
}
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct Layout { /* private */ }
impl Layout {
pub fn from_size_align(size: usize: align: usize) -> Result<Self, LayoutError> {…}
pub unsafe fn from_size_align_unchecked(size: usize: align: usize) -> Self {…}
pub fn size(&self) -> usize {…}
pub fn align(&self) -> usize {…}
pub fn new<T>() -> Self {…}
pub fn for_value<T: ?Sized>(t: &T) -> Self {…}
}
#[derive(Copy, Clone, Debug)]
pub struct LayoutError { /* private */ }
/// Forwards method calls to the `static` item registered
/// with `#[global_allocator]` if any, or the operating system’s default.
pub struct Global;
/// The operating system’s default allocator.
pub struct System;
impl GlobalAlloc for Global {…}
impl GlobalAlloc for System {…}
CC @rust-lang/libs, @glandium
Unresolved questions or otherwise blocking
- Wait for extern types Tracking issue for RFC 1861: Extern types #43467 to be stable in the language before stabilazing one in the standard library.
- Name of the
Global
type.GlobalAllocator
? -
Name of theRenamed toVoid
type. Lower-casevoid
would match C (our*mut void
is very close to C’svoid*
type), but violates the convension of camel-case for non-primitive types. ButVoid
in camel-case is already the name of an empty enum (not an extern type like here) in a popular crate. (An extern type is desirable so that<*mut _>::offset
cannot be called without first casting to another pointer type.) Maybe call itOpaque
?Unknown
?Opaque
.- Rename again to something else?
-
TakingLayout
by reference, or making itCopy
Alloc methods should take layout by reference #48458.Copy
: Implement Copy for std::alloc::Layout #50109 -
Not to be required by this trait since glibc and Windows do not support this.GlobalAlloc::owns
: Alloc: Add owns method #44302 proposes making it required for allocators to be able to tell if it “owns” a given pointer. -
Settle semantics of layout "fit" and other safety conditions.Without anusable_size
method (for now), the layout passed to dealloc must be the same as was passed to alloc. (Same as withAlloc::usable_size
’s default impl returning(layout.size(), layout.size())
.) -
AnReplace {Alloc,GlobalAlloc}::oom with a lang item. #50144oom
method is part ofGlobalAlloc
so that implementation-specific printing to stderr does not need to be part of liballoc and instead goes through#[global_allocator]
, but this does not really belong in theGlobalAlloc
trait. Should another mechanism like#[global_allocator]
be added to have pluggable OOM handling?
Not proposed (yet) for stabilization
- The
Alloc
trait - The rest of
Layout
methods - The
AllocErr
type
We’re not ready to settle on a design for collections generic over the allocation type. Discussion of this use case should continue on the tracking issue for the Alloc
trait: #32838.
Metadata
Metadata
Assignees
Labels
Area: Custom and system allocatorsBlocker: Implemented in the nightly compiler and unstable.Category: An issue tracking the progress of sth. like the implementation of an RFCRelevant to the library API team, which will review and decide on the PR/issue.This issue / PR is in PFCP or FCP with a disposition to merge it.In the final comment period and will be merged soon unless new substantive objections are raised.The final comment period is finished for this PR / Issue.