-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
Changes to GlobalAlloc #51160
Changes to GlobalAlloc #51160
Conversation
Team member @SimonSapin has proposed to merge this. The next step is review by the rest of the tagged teams: Concerns:
Once a majority of reviewers approve (and none object), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up! See this document for info about what commands tagged team members can give me. |
src/liballoc/alloc.rs
Outdated
// We do not allocate for Box<T> when T is ZST, so deallocation is also not necessary. | ||
if size != 0 { | ||
let layout = Layout::from_size_align_unchecked(size, align); | ||
Global.dealloc(ptr as *mut Opaque, layout); | ||
Global.dealloc(NonNull::new_unchecked(ptr.as_ptr() as *mut Opaque), layout); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can be NonNull::from(ptr).cast()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can be NonNull::from(ptr).cast()
That's true in many other places. Same for things like ptr.as_ptr() as *mut u8
which can probably be ptr.cast().as_ptr()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My comment was mostly about using the safe From
impl instead of the unsafe new_unchecked
. I don’t have a strong opinion on cast()
v.s. as
.
/// An opaque, byte-sized type. Used for pointers to allocated memory. | ||
/// | ||
/// This type can only be used behind a pointer like `*mut Opaque` or `ptr::NonNull<Opaque>`. | ||
/// Such pointers are similar to C’s `void*` type. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this still the case?
The job Click to expand the log.
I'm a bot! I can only do what humans tell me to, so if this was not helpful or you have suggestions for improvements, please ping or otherwise contact |
I'm not super opposed to the |
With |
If we ever augment the error with state |
The job Click to expand the log.
I'm a bot! I can only do what humans tell me to, so if this was not helpful or you have suggestions for improvements, please ping or otherwise contact |
@rfcbot concern typesafe-to-a-fault I have yet to be personally convinced that The cons of this approach, however, I feel are:
Overall I feel the cons outweigh the pros for global allocators. I personally feel the same way for the Finally I believe the intention with |
Name resolution for extern type methods is also buggy at the moment: #46665 |
I don’t feel strongly about the result vs nullable, I don’t mind not doing that change. Its benefit is small, but I find that the benefit of diverging from Regarding changing |
The job Click to expand the log.
I'm a bot! I can only do what humans tell me to, so if this was not helpful or you have suggestions for improvements, please ping or otherwise contact |
If we can't get I also don't think we need to strive for matching |
FWIW, changing the return type of the |
☔ The latest upstream changes (presumably #50880) made this pull request unmergeable. Please resolve the merge conflicts. |
Per libs team discussion today we’re ready to stabilize Thanks for the PR @Amanieu ! (And sorry I lead you to doing work that didn’t end up being used.) |
As discussed with @SimonSapin at Rustfest Paris, this PR makes several changes to
GlobalAlloc
:Opaque
is changed from an extern type to au8
newtype (with a private member). This allows.offset
to work on*mut Opaque
.GlobalAlloc
signatures are changed to takeNonNull<Opaque>
as parameters and returnResult<NonNull<Opaque>, AllocErr>
. This has the same size as*mut Opaque
but matches theAlloc
trait and generally makes it easier to write custom allocators in Rust.r? @SimonSapin