-
Notifications
You must be signed in to change notification settings - Fork 13.2k
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
docs: GlobalAlloc: completely replace example with one that works #81864
Conversation
r? @sfackler (rust-highfive has picked a reviewer for you, use r? to override) |
library/core/src/alloc/global.rs
Outdated
/// new = counter; | ||
/// Some(counter) | ||
/// }).unwrap(); | ||
/// if new > ARENA { abort(); } |
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.
The allocator returns null on allocation failure, it does not abort
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.
The allocator returns null on allocation failure, it does not abort
Ideally, yes. But with the current structure of the critical section (no conditional other than the one inherent in fetch_update
, the counter would eventually overflow after many many repeated allocations.
Although this is only a demo, I think your implication, that this ought to be addressed, is right, because it's supposed to be demoing the GlobalAlloc
trait. I will improve this.
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.
I would make it limited (in features), complete and correct. Use a Mutex to simplify the code for the reader. Compute the needed bump for the allocation, if there is no more space, return null, otherwise "commit" to it and update and release the lock. Limited could mean that it only supports one alignment (everything is 16-aligned and larger aligns fail to allocate?) or other realistic restriction, just for this example. just to make the example easier to read and write. (This way we avoid thinking about atomics and arithmetic overflow.)
If Mutex is not available then I guess that's a shame and maybe the example can be simplified using the same idea but with an atomic.
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.
I like the suggestion of trying to use a Mutex. Unfortunately they can't be constructed in const context (AIUI because of Windows). I think fetch_update
is a good primitive to use but I take your point that I could just bung everything relevant into the critical section which would simplify matters considerably.
The alignment comes out much nicer when counting down so it may turn out to be shorter to keep the alignment support than special case it.
I've updated it quite a lot, after review of the |
How about this. Moving the error handling into the critical section has considerably simplified it. Almost all the bit-twiddling is gone. I replaced the special arithmetic operations with plain |
I'm not sure the comment for |
@sfackler this is ready for review |
oops |
I'm still concerned that the example both:
If we want to emphasize that alignment requirements must be respected, then we can talk about it in the docs. I don't think we need to force an arbitrarily artificial example just for its own sake. |
I don't agree that this isn't a "real-world" implementation. I think this implementation would be suitable for an embedded environment where only a small number of allocations will ever occur. I seriously anticipate that someone might c&p this into their microcontroller.
I recognise this is hyperbole but I wanted to pick at it anyway. The example is 49 lines (l.26 to 74 of the file). Of those, lines 30, 47, 49-52, and 59 (6 lines in all) would be gone in an implementation which didn't care about or check alignment (of which only lines 49,51 and 59 are actually bit-twiddling). Lines 27, 62-64 and arguably half of each of 33, 39 and 72 (call that 6 lines in all) would be gone in an implementation which didn't care about threadsafety. I make that an absolute outside of 12 lines of "atomics and bit-twiddling" - 25%. I don't think it would be sensible to produce an example which pretended to work but didn't honour alignment, or which wasn't threadsafe, if it saves only a quarter of the size. The example is full of what one might reasonably think of as "weird shit" - the combination of raw pointers,
I think the best options are something like this example; no example; or a very minimal dummy example - eg as suggested by @vitalyd. The latter is still a fair chunk of text, despite not illuminating much more than how to spell and place the If the libs team doesn't feel a proper example is worth the space here then please do feel free to close this MR and leave #81847 open for someone to fix another way. Obviously that would be disappointing to me but it's not like there aren't other things I could be trying to fix :-). Alternatively, perhaps this particular example, which demonstrates not just Personally ISTM that Rust nowadays likes to put stuff like that in the docs examples, rather than off in some external place which has to be separately hunted down (and isn't tested in CI, etc.). And it's not like the docs for Thanks. |
76ed49f
to
07e11e8
Compare
Thanks, folded in your suggestion and squashed. |
@bors r+ |
RIP bors? @bors r+ |
@bors r+ |
📌 Commit 07e11e8 has been approved by |
…nieu docs: GlobalAlloc: completely replace example with one that works Since this is an example, this could really do with some review from someone familiar with unsafe stuff! I made the example no longer `no_run` since it works for me. Fixes rust-lang#81847
⌛ Testing commit 07e11e8 with merge cf975bbd6418f1571394d60fb875ef5880c24b6e... |
💔 Test failed - checks-actions |
This comment has been minimized.
This comment has been minimized.
|
Alignments > 4k are not supported, rust-lang#70022 rust-lang#70144 Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
@bors r+ |
📌 Commit 03d7001 has been approved by |
Rollup of 5 pull requests Successful merges: - rust-lang#81864 (docs: GlobalAlloc: completely replace example with one that works) - rust-lang#87024 (rustdoc: show count of item contents when hidden) - rust-lang#87278 (:arrow_up: rust-analyzer) - rust-lang#87326 (Update cargo) - rust-lang#87346 (Rename force-warns to force-warn) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
Since this is an example, this could really do with some review from someone familiar with unsafe stuff!
I made the example no longer
no_run
since it works for me.Fixes #81847