fix: fix feature "alloc_trait" to adapt to the current Rust Allocator API#37
Open
fix: fix feature "alloc_trait" to adapt to the current Rust Allocator API#37
Conversation
Member
Author
|
Temporarily close. I'll reopen it when my fork repo is stable. |
BusyJay
reviewed
Feb 20, 2023
| unsafe fn alloc(&mut self, layout: Layout) -> Result<NonNull<u8>, AllocErr> { | ||
| NonNull::new(GlobalAlloc::alloc(self, layout)).ok_or(AllocErr) | ||
| fn alloc_impl(&self, layout: Layout, zeroed: bool) -> Result<NonNull<[u8]>, AllocError> { | ||
| match layout.size() { |
Member
There was a problem hiding this comment.
Better use if/else and check non-zero first.
| let raw_ptr = GlobalAlloc::realloc(self, ptr.as_ptr(), old_layout, new_size); | ||
| let ptr = NonNull::new(raw_ptr).ok_or(AllocError)?; | ||
| if zeroed { | ||
| raw_ptr.add(old_size).write_bytes(0, new_size - old_size); |
Member
There was a problem hiding this comment.
Should use realloc(ptr, flags | MALLOCX_ZERO.
| ) -> Result<NonNull<u8>, AllocErr> { | ||
| NonNull::new(GlobalAlloc::realloc(self, ptr.as_ptr(), layout, new_size)).ok_or(AllocErr) | ||
| fn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError> { | ||
| self.alloc_impl(layout, false) |
Member
There was a problem hiding this comment.
As these are not complicated functions, Instead of extracting an impl method, implementing them in the function directly seems more clear.
| if layout.size() != 0 { | ||
| // SAFETY: `layout` is non-zero in size, | ||
| // other conditions must be upheld by the caller | ||
| unsafe { GlobalAlloc::dealloc(self, ptr.as_ptr(), layout) } |
Member
There was a problem hiding this comment.
You can call self.dealloc directly.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The
alloc_traitfeature cannot be compiled because it used deprecated APIcore::alloc::{Alloc, Excess}.Status:
alloc_traitcompilable.TODO (may be not in this PR):
growandshrink.growandshrink.