-
Notifications
You must be signed in to change notification settings - Fork 9
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
Remove _zeroed
variants and pass a flag instead
#44
Comments
I would prefer an Otherwise this seems like a good idea. Do we want to do something similar for |
I'm aware of that post, but I wasn't able to find a good enum name so far. Maybe something like enum ??? {
Zeroed,
Unspecified
} Yes for |
I mean having an |
Oh, yeah, that's also a good idea. So basically like |
Tentative names: enum AllocContents {
Zero,
Uninitialized, // With a big warning that reading this is UB if you haven't initialized it
}
enum AllocConstraints {
None,
InPlace,
} Note that I am less sure about the |
I'd call it NewMemory, because a realloc doesn't also zeroed the previous data. |
As this will be in |
Names should generally make sense on their own, without knowing the module path to the item. |
Can we agree on this? /// Passed to an allocating method to determine how newly requested memory is
/// initialized.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
enum AllocInit {
/// Depending on the used allocator, newly allocated memory is not
/// necessarily initialized. Reading uninitialized memory is
/// [undefined behavior]!
///
/// [undefined behavior]: https://doc.rust-lang.org/reference/behavior-considered-undefined.html
Unspecified,
/// Newly allocated memory returned by an allocator is guaranteed to be
/// zeroed.
Zero,
}
/// Passed to an allocator to determine how newly requested memory is
/// allocated when growing or shrinking an existing memory block.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
enum AllocPlacement {
/// The location is not specified. The allocator is allowed to make a
/// byte-by-byte copy of the memory.
Unspecified,
/// When allocating or releasing memory, the contents of the memory must not
/// be copied.
InPlace,
} |
Sounds good to me. Nice work on the |
Thank you! |
I feel, dubious about those definitions.
Consider this revision: /// A desired initial state for allocated memory.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
enum AllocInit {
/// The new memory is not initialized to any particular value.
///
/// Remember that reading uninitialized memory is Undefined Behavior.
Uninitialized,
/// The new memory is guaranteed to be zeroed.
Zeroed,
}
/// A reallocation constraint.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
enum ReAllocPlacement {
/// The new address of the memory can be any heap location.
///
/// If the allocation _does_ move, it's the responsibility of the allocator
/// to also move the data from the previous location to the new location.
Unspecified,
/// The address of the new memory must not change.
///
/// If the allocation would have to be moved to a new location to fit, the
/// reallocation request should instead fail.
InPlace,
} |
Fair point. Additionally,
👍
I'd use
Thanks, sounds better!
Is that true? Is this necessarily a heap location? I can imagine, that this could also be a global buffer or something, which has been allocated on the stack. |
Good point on the end there.... Well, so as far as I know, LLVM has the concept of the call stack memory, and then it also has the concept that memory can come "from places that aren't the call stack memory", and that's all it thinks about. And memory "not in the call stack memory space" can come from all sorts of places that you might want an allocator for, and if you've got an OS under you then ultimately you're asking the OS to tell the memory management unit to do the right thing. And if you don't have an OS under you then you're doing whatever the device's data sheets say you can do because you are the OS and you have that phenomenal cosmic power. And we need to allow for all these possibilities. So... What we probably want to do is to say "The new address of the memory can be any valid location." and then the words "valid location" are a link to a section on the top level of some module that describes Rust's rules (or lack of rules) for what is even a legal allocator. Such a section would be similar to how the |
The main rules would be things that "make sense anyway", like you can't start allocating from a location just past the end of the stack and then call a function because when you push stuff onto the stack it starts writing into your allocation pool and the program gets very sad. |
Adding fn grow(&mut self, ptr: NonNull<T>, layout: Layout, new_size: usize, init: AllocInit) -> Result<(NonNull<T>, usize), AllocErr>;
fn grow_in_place(&mut self, ptr: NonNull<T>, layout: Layout, new_size: usize, init: AllocInit) -> Result<usize, AllocErr>;
fn shrink(&mut self, ptr: NonNull<T>, layout: Layout, new_size: usize) -> Result<(NonNull<T>, usize), AllocErr>;
fn shrink_in_place(&mut self, ptr: NonNull<T>, layout: Layout, new_size: usize) -> Result<usize, AllocErr>;
fn shrink_in_place(&mut self, ptr: NonNull<T>, layout: Layout, new_size: usize) -> Result<usize, AllocErr> {
Ok((ptr, layout.size())
} which cannot fail. So I'd come to this conclusion:
|
We just agreed in #42 that we can't remove the |
I was just about to say that 😄 |
Overhaul of the `AllocRef` trait to match allocator-wg's latest consens; Take 2 GitHub won't let me reopen rust-lang#69889 so I make a new PR. In addition to rust-lang#69889 this fixes the unsoundness of `RawVec::into_box` when using allocators supporting overallocating. Also it uses `MemoryBlock` in `AllocRef` to unify `_in_place` methods by passing `&mut MemoryBlock`. Additionally, `RawVec` now checks for `size_of::<T>()` again and ignore every ZST. The internal capacity of `RawVec` isn't used by ZSTs anymore, as `into_box` now requires a length to be specified. r? @Amanieu fixes rust-lang/wg-allocators#38 fixes rust-lang/wg-allocators#41 fixes rust-lang/wg-allocators#44 fixes rust-lang/wg-allocators#51
Currently, for almost all methods (except
dealloc
andshrink(_in_place)
) inAllocRef
, there is a_zeroed
variant, which blows up the trait.I propose to remove those variants and add a
zeroed: bool
to the signatures. E.g.:The text was updated successfully, but these errors were encountered: