-
Notifications
You must be signed in to change notification settings - Fork 13.3k
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
Document interaction between std::alloc
and Box::{into_raw, from_raw}
#53039
Comments
I think we probably want to guarantee that Box uses the global allocator (until we add an allocator type parameter) and that this kind of thing is okay. @rust-lang/libs thoughts? |
Same thing for |
This may not work on Windows, in my understanding, given that you may be using two different versions of the system allocator.
… On Aug 3, 2018, at 5:37 PM, FenrirWolf ***@***.***> wrote:
Same thing for Vec I imagine? And also for going the other way around? (i.e. Box::into_raw and then calling alloc::dealloc on the pointer with the appropriate Layout)
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub, or mute the thread.
|
Would the stdlib ever be using two system allocators at once? I can see that being a problem if you were, say calling a function in a C dependency that in turn calls out to an allocator and hands you the resulting pointer, or something like that. But if |
Yes, the issue is C code; if you say that it this works “with the system allocator”, people will assume that C using the system allocator means it works, when it may not.
Again, this is just my understanding; I *might* be wrong here.
… On Aug 4, 2018, at 3:52 PM, FenrirWolf ***@***.***> wrote:
Would the stdlib ever be using two system allocators at once? I can see that being a problem if you were, say calling a function in a C dependency that in turn calls out to an allocator and hands you the resulting pointer. It seems like if std ever called out to multiple allocators, then even Box::into_raw and Box::from_raw wouldn't necessarily be compatible either.
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub, or mute the thread.
|
@steveklabnik You're right that memory allocated with |
Is there a guarantee how Or is that not the case? |
I don't think any of those things are the case. |
@sfackler, I’m willing to buy that but what leads you to that conclusion? Is there some part of the documentation that I overlooked? |
Sure, the docs might imply that currently, hence the issue to have that changed =P |
@vitalyd It's how Box is implemented, but beyond that the "job" of Box is just to be a smart pointer around a heap allocation. If you want to cache align or whatever you'd pick a type that's built around doing that. |
Today’s But we’re already constrained by the existence of |
I suspect that this is defacto true today in the sense that it's "as if" we've documented this anyway, likely with unsafe code relying on it. That, plus what @SimonSapin was mentioning, I think we should go ahead and document this. |
Also it looks like the interaction between alignment and deallocation isn't settled yet (see #32838), so if this does get documented there would need to be discussion about the ability or inability to put specially aligned memory in a Box or Vec (i.e. if the example code would still be valid if the layout were declared as, say, |
I realize that cache aligning inside |
I don't think anyone misunderstands what you're getting at. I just don't see "making a guarantee" and "documenting the behavior" as separate things. What use is a guarantee that's not documented? Right now the implementation happens to line up such that the example is valid, but it's not guaranteed. So the only question is whether it's desirable to guarantee that in documentation, or to not so that the implementations of |
Although the issue description is only about |
Looks like this was addressed by #58183 |
The current documentation for
Box::from_raw
states thatBox
es can only be made from raw pointers previously derived fromBox::into_raw
, but now that there are stable functions to directly access the allocator, one can imagine writing code like this:Is such code sound? Or is
Box
's allocator still considered unspecified?The text was updated successfully, but these errors were encountered: