about summary refs log tree commit diff
path: root/src/libcore/ptr.rs
AgeCommit message (Collapse)AuthorLines
2018-06-01Reword {ptr,mem}::replace docs.Corey Farwell-2/+3
Fixes https://github.com/rust-lang/rust/issues/50657.
2018-05-18Auto merge of #50319 - nagisa:align_to, r=alexcrichtonbors-46/+224
Implement [T]::align_to Note that this PR deviates from what is accepted by RFC slightly by making `align_offset` to return an offset in elements, rather than bytes. This is necessary to sanely support `[T]::align_to` and also simply makes more sense™. The caveat is that trying to align a pointer of ZST is now an equivalent to `is_aligned` check, rather than anything else (as no number of ZST elements will align a misaligned ZST pointer). It also implements the `align_to` slightly differently than proposed in the RFC to properly handle cases where size of T and U aren’t co-prime. Furthermore, a promise is made that the slice containing `U`s will be as large as possible (contrary to the RFC) – otherwise the function is quite useless. The implementation uses quite a few underhanded tricks and takes advantage of the fact that alignment is a power-of-two quite heavily to optimise the machine code down to something that results in as few known-expensive instructions as possible. Currently calling `ptr.align_offset` with an unknown-at-compile-time `align` results in code that has just a single "expensive" modulo operation; the rest is "cheap" arithmetic and bitwise ops. cc https://github.com/rust-lang/rust/issues/44488 @oli-obk As mentioned in the commit message for align_offset, many thanks go to Chris McDonald.
2018-05-17Remove the intrinsic for align_offsetSimonas Kazlauskas-10/+21
Keep only the language item. This removes some indirection and makes codegen worse for debug builds, but simplifies code significantly, which is a good tradeoff to make, in my opinion. Besides, the codegen can be improved even further with some constant evaluation improvements that we expect to happen in the future.
2018-05-17Change align_offset to support different stridesSimonas Kazlauskas-46/+213
This is necessary if we want to implement `[T]::align_to` and is more useful in general. This implementation effort has begun during the All Hands and represents a month of my futile efforts to do any sort of maths. Luckily, I found the very very nice Chris McDonald (cjm) on IRC who figured out the core formulas for me! All the thanks for existence of this PR go to them! Anyway… Those formulas were mangled by yours truly into the arcane forms you see here to squeeze out the best assembly possible on most of the modern architectures (x86 and ARM were evaluated in practice). I mean, just look at it: *one actual* modulo operation and everything else is just the cheap single cycle ops! Admitedly, the naive solution might be faster in some common scenarios, but this code absolutely butchers the naive solution on the worst case scenario. Alas, the result of this arcane magic also means that the code pretty heavily relies on the preconditions holding true and breaking those preconditions will unleash the UB-est of all UBs! So don’t.
2018-05-17Revert #49767steveklabnik-300/+70
There was [some confusion](https://github.com/rust-lang/rust/pull/49767#issuecomment-389250815) and I accidentally merged a PR that wasn't ready.
2018-05-16Make core::nonzero privateSimon Sapin-9/+2
It is now an implementation detail of ptr::NonNull and num::NonZero*
2018-05-15Rollup merge of #49767 - ecstatic-morse:ptr-docs, r=steveklabnikGuillaume Gomez-70/+300
Rewrite docs for `std::ptr` This PR attempts to resolve #29371. This is a fairly major rewrite of the `std::ptr` docs, and deserves a fair bit of scrutiny. It adds links to the GNU libc docs for various instrinsics, adds internal links to types and functions referenced in the docs, adds new, more complex examples for many functions, and introduces a common template for discussing unsafety of functions in `std::ptr`. All functions in `std::ptr` (with the exception of `ptr::eq`) are unsafe because they either read from or write to a raw pointer. The "Safety" section now informs that the function is unsafe because it dereferences a raw pointer and requires that any pointer to be read by the function points to "a valid vaue of type `T`". Additionally, each function imposes some subset of the following conditions on its arguments. * The pointer points to valid memory. * The pointer points to initialized memory. * The pointer is properly aligned. These requirements are discussed in the "Undefined Behavior" section along with the consequences of using functions that perform bitwise copies without requiring `T: Copy`. I don't love my new descriptions of the consequences of making such copies. Perhaps the old ones were good enough? Some issues which still need to be addressed before this is merged: - [ ] The new docs assert that `drop_in_place` is equivalent to calling `read` and discarding the value. Is this correct? - [ ] Do `write_bytes` and `swap_nonoverlapping` require properly aligned pointers? - [ ] The new example for `drop_in_place` is a lackluster. - [ ] Should these docs rigorously define what `valid` memory is? Or should is that the job of the reference? Should we link to the reference? - [ ] Is it correct to require that pointers that will be read from refer to "valid values of type `T`"? - [x] I can't imagine ever using `{read,write}_volatile` with non-`Copy` types. Should I just link to {read,write} and say that the same issues with non-`Copy` types apply? - [x] `write_volatile` doesn't link back to `read_volatile`. - [ ] Update docs for the unstable [`swap_nonoverlapping`](https://github.com/rust-lang/rust/issues/42818) - [ ] Update docs for the unstable [unsafe pointer methods RFC](https://github.com/rust-lang/rfcs/pull/1966) Looking forward to your feedback. r? @steveklabnik
2018-05-09Shorten ownership safety discussion in `read_volatile`Dylan MacKenzie-8/+15
Non-`Copy` types should not be in volatile memory.
2018-05-09Use the "Safety" heading instead of "Undefined Behavior"Dylan MacKenzie-43/+1
2018-05-01Rollup merge of #50233 - mark-i-m:const_vec, r=kennytmkennytm-3/+2
Make `Vec::new` a `const fn` `RawVec::empty/_in` are a hack. They're there because `if size_of::<T> == 0 { !0 } else { 0 }` is not allowed in `const` yet. However, because `RawVec` is unstable, the `empty/empty_in` constructors can be removed when #49146 is done...
2018-04-28Rollup merge of #49858 - dmizuk:unique-doc-hidden, r=steveklabnikkennytm-0/+1
std: Mark `ptr::Unique` with `#[doc(hidden)]` `Unique` is now perma-unstable, so let's hide its docs.
2018-04-25Make Vec::new constMark Mansi-3/+2
2018-04-17stabilize `nonnull_cast` featuretinaun-1/+1
2018-04-17stabilize `swap_nonoverlapping` featuretinaun-3/+1
2018-04-12Rename alloc::Void to alloc::OpaqueSimon Sapin-2/+2
2018-04-12Use NonNull<Void> instead of *mut u8 in the Alloc traitMike Hommey-0/+8
Fixes #49608
2018-04-11std: Mark `ptr::Unique` with `#[doc(hidden)]`Daiki Mizukami-0/+1
2018-04-09Fix various nits from PR reviewDylan MacKenzie-7/+6
- Remove redundant "unsafe" from module description. - Add a missing `Safety` heading to `read_unaligned`. - Remove weasel words in `Undefined Behavior` description for `write{,_unaligned,_bytes}`.
2018-04-07Fix broken relative linksDylan MacKenzie-2/+2
2018-04-07Fix broken link in `write_unaligned` docsDylan MacKenzie-1/+1
2018-04-07Don't link "Undefined Behavior" headingDylan MacKenzie-22/+9
The rendered version does not make clear that this is a link to another page, and it breaks the anchor link.
2018-04-07Fix example for `ptr::replace`Dylan MacKenzie-1/+1
2018-04-07Rewrite docs for `std::ptr`Dylan MacKenzie-71/+350
- Add links to the GNU libc docs for `memmove`, `memcpy`, and `memset`, as well as internally linking to other functions in `std::ptr` - List sources of UB for all functions. - Add example to `ptr::drop_in_place` and compares it to `ptr::read`. - Add examples which more closely mirror real world uses for the functions in `std::ptr`. Also, move the reimplementation of `mem::swap` to the examples of `ptr::read` and use a more interesting example for `copy_nonoverlapping`. - Change module level description
2018-03-31Deprecate offset_to; switch core&alloc to using offset_from insteadScott McMurray-4/+8
Bonus: might make code than uses `.len()` on slice iterators faster
2018-03-29src/libcore/ptr.rs: Fix documentation for size of `Option<NonNull<T>>`Josh Triplett-1/+1
Seems more useful to say that it has the same size as `*mut T`.
2018-03-26Auto merge of #49297 - scottmcm:offset-from, r=dtolnaybors-2/+227
Introduce unsafe offset_from on pointers Adds intrinsics::exact_div to take advantage of the unsafe, which reduces the implementation from ```asm sub rcx, rdx mov rax, rcx sar rax, 63 shr rax, 62 lea rax, [rax + rcx] sar rax, 2 ret ``` down to ```asm sub rcx, rdx sar rcx, 2 mov rax, rcx ret ``` (for `*const i32`) See discussion on the `offset_to` tracking issue https://github.com/rust-lang/rust/issues/41079 Some open questions - Would you rather I split the intrinsic PR from the library PR? - Do we even want the safe version of the API? https://github.com/rust-lang/rust/issues/41079#issuecomment-374426786 I've added some text to its documentation that even if it's not UB, it's useless to use it between pointers into different objects. and todos - [x] ~~I need to make a codegen test~~ Done - [x] ~~Can the subtraction use nsw/nuw?~~ No, it can't https://github.com/rust-lang/rust/pull/49297#discussion_r176697574 - [x] ~~Should there be `usize` variants of this, like there are now `add` and `sub` that you almost always want over `offset`? For example, I imagine `sub_ptr` that returns `usize` and where it's UB if the distance is negative.~~ Can wait for later; C gives a signed result https://github.com/rust-lang/rust/issues/41079#issuecomment-375842235, so we might as well, and this existing to go with `offset` makes sense.
2018-03-24Fix doctest mutability copy-pastaScott McMurray-2/+2
2018-03-24Documentation and naming improvementsScott McMurray-15/+33
2018-03-23Introduce unsafe offset_from on pointersScott McMurray-0/+207
Adds intrinsics::exact_div to take advantage of the unsafe, which reduces the implementation from ```asm sub rcx, rdx mov rax, rcx sar rax, 63 shr rax, 62 lea rax, [rax + rcx] sar rax, 2 ret ``` down to ```asm sub rcx, rdx sar rcx, 2 mov rax, rcx ret ``` (for `*const i32`)
2018-03-17Remove deprecated unstable ptr::Shared type alias.Simon Sapin-5/+0
It has been deprecated for about one release cycle.
2018-03-17Stop using deprecated NonZero APIsSimon Sapin-10/+18
These will eventually be removed (though the NonZero<T> lang item will likely stay).
2018-03-17Deprecate core::nonzero in favor of ptr::NonNull and num::NonZero*Simon Sapin-2/+9
2018-03-02Stabilize Unsafe Pointer Methodstinaun-78/+30
also minor doc fixes. Closes #43941
2018-02-10Correct a few stability attributesOliver Middleton-2/+2
2018-01-22Add an unstable `cast<U>() -> NonNull<U>` method to `NonNull<T>`.Simon Sapin-0/+8
This is less verbose than going through raw pointers to cast with `as`.
2018-01-21Implement Eq, PartialEq, Ord, PartialOrd, and Hash for NonNull<_>Simon Sapin-0/+31
2018-01-21Move Debug for NonNull impl closer to other trait implsSimon Sapin-7/+7
2018-01-21NonNull ended up landing in 1.25Simon Sapin-18/+18
2018-01-20Rename NonNull::empty to dangling.Simon Sapin-1/+2
2018-01-20Preserve formatting options in Debug for NonNull/UniqueSimon Sapin-2/+2
2018-01-20Fix some doc-comment examples for earlier API refactorSimon Sapin-4/+4
https://github.com/rust-lang/rust/pull/41064
2018-01-20Stabilize std::ptr::NonNullSimon Sapin-15/+17
2018-01-20Remove a deprecated (renamed) and unstable method of NonNullSimon Sapin-7/+0
2018-01-20Mark Unique as perma-unstable, with the feature renamed to ptr_internals.Simon Sapin-15/+15
2018-01-20Replace Unique<T> with NonZero<T> in Alloc traitSimon Sapin-0/+7
2018-01-20Rename std::ptr::Shared to NonNullSimon Sapin-41/+46
`Shared` is now a deprecated `type` alias. CC https://github.com/rust-lang/rust/issues/27730#issuecomment-352800629
2018-01-20Implement Debug for ptr::Shared and ptr::Unique.Corey Farwell-2/+14
Fixes https://github.com/rust-lang/rust/issues/46755.
2017-12-25Auto merge of #46914 - mikeyhew:raw_pointer_self, r=arielb1bors-2/+2
Convert warning about `*const _` to a future-compat lint #46664 was merged before I could convert the soft warning about method lookup on `*const _` into a future-compatibility lint. This PR makes that change. fixes #46837 tracking issue for the future-compatibility lint: #46906 r? @arielb1
2017-12-23fix doctests in libcoreMichael Hewson-2/+2
2017-12-21docs: do not call integer overflows as underflowsTrevor Spiteri-8/+4