Skip to content

Add UnsafeRawPointer type and API. #3677

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

Merged
merged 4 commits into from
Jul 22, 2016
Merged

Add UnsafeRawPointer type and API. #3677

merged 4 commits into from
Jul 22, 2016

Conversation

atrick
Copy link
Contributor

@atrick atrick commented Jul 22, 2016

As proposed in SE-0107: UnsafeRawPointer.
https://github.com/apple/swift-evolution/blob/master/proposals/0107-unsaferawpointer.md

The fundamental difference between Unsafe[Mutable]RawPointer and
Unsafe[Mutable]Pointer is simply that the former is used for "untyped"
memory access, and the later is used for "typed" memory access. Let's refer to
these as "raw pointers" and "typed pointers". Because operations on raw pointers
access untyped memory, the compiler cannot make assumptions about the underlying
type of memory and must be conservative. With operations on typed pointers, the
compiler may make strict assumptions about the type of the underlying memory,
which allows more aggressive optimization.

Memory can only be accessed by a typed pointer when it is currently
bound to the Pointee type. Memory can be bound to type T via:

  • UnsafePointer<T>.allocate(capacity: n)
  • UnsafePointer<Pointee>.withMemoryRebound(to: T.self, capacity: n) {...}
  • UnsafeMutableRawPointer.initializeMemory(as: T.self, at: i, count: n, to: x)
  • UnsafeMutableRawPointer.initializeMemory(as: T.self, from: p, count: n)
  • UnsafeMutableRawPointer.moveInitializeMemory(as: T.self, from: p, count: n)
  • UnsafeMutableRawPointer.bindMemory(to: T.self, capacity: n)

Mangle UnsafeRawPointer as predefined substitution 'Sv' for Swift void
pointer ([urp] are taken).

@atrick
Copy link
Contributor Author

atrick commented Jul 22, 2016

@swift-ci Please test and merge.

As proposed in SE-0107:   UnsafeRawPointer.
https://github.com/apple/swift-evolution/blob/master/proposals/0107-unsaferawpointer.md

The fundamental difference between Unsafe[Mutable]RawPointer and
Unsafe[Mutable]Pointer<Pointee> is simply that the former is used for "untyped"
memory access, and the later is used for "typed" memory access. Let's refer to
these as "raw pointers" and "typed pointers". Because operations on raw pointers
access untyped memory, the compiler cannot make assumptions about the underlying
type of memory and must be conservative. With operations on typed pointers, the
compiler may make strict assumptions about the type of the underlying memory,
which allows more aggressive optimization.

Memory can only be accessed by a typed pointer when it is currently
bound to the Pointee type. Memory can be bound to type `T` via:
- `UnsafePointer<T>.allocate(capacity: n)`
- `UnsafePointer<Pointee>.withMemoryRebound(to: T.self, capacity: n) {...}`
- `UnsafeMutableRawPointer.initializeMemory(as: T.self, at: i, count: n, to: x)`
- `UnsafeMutableRawPointer.initializeMemory(as: T.self, from: p, count: n)`
- `UnsafeMutableRawPointer.moveInitializeMemory(as: T.self, from: p, count: n)`
- `UnsafeMutableRawPointer.bindMemory(to: T.self, capacity: n)`

Mangle UnsafeRawPointer as predefined substitution 'Sv' for Swift void
pointer ([urp] are taken).
@atrick
Copy link
Contributor Author

atrick commented Jul 22, 2016

@swift-ci Please test and merge.

var UnsafeMutableRawPointerExtraTestSuite =
TestSuite("UnsafeMutableRawPointerExtra")

class Missile {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not LifetimeTracked? The harness will do leak checks for you.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made a note to follow up on this because I should also change the original UnsafePointer tests. For now the tests are consistent.

@atrick
Copy link
Contributor Author

atrick commented Jul 22, 2016

Assuming this merge goes through, I'll create a new PR with a slew of test case tweaks, comments and formatting based on Dmitri's comments.

Incorporate Dmitri's feedback.

Properly use a _memmove helper.

Add load/storeBytes alignment precondition checks.

Reword comments.

Demangler tests.
@atrick
Copy link
Contributor Author

atrick commented Jul 22, 2016

Don't merge this yet. UnsafePointer mangling test cases don't work for some reason.

@atrick
Copy link
Contributor Author

atrick commented Jul 22, 2016

@swift-ci Please test and merge.

@atrick
Copy link
Contributor Author

atrick commented Jul 22, 2016

This depends on a generic substitution feature that need to pull in first.

@atrick atrick closed this Jul 22, 2016
@atrick atrick reopened this Jul 22, 2016
@atrick
Copy link
Contributor Author

atrick commented Jul 22, 2016

This passes the "long" tests now.
@swift-ci Please smoke test and merge.

@atrick
Copy link
Contributor Author

atrick commented Jul 22, 2016

This symbol is missing from corelibs-foundation:
Swift.== infix (Swift.String, Swift.String) -> Swift.Bool

@atrick atrick merged commit a41484e into swiftlang:master Jul 22, 2016
@atrick atrick deleted the rawptr-api branch May 26, 2017 02:06
kateinoigakukun pushed a commit that referenced this pull request Aug 31, 2022
[pull] swiftwasm from main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants