[swift-evolution] Pitch: Improved Swift pointers

Taylor Swift kelvin13ma at gmail.com
Wed Jul 12 17:42:43 CDT 2017


i’m sorry this sounds like something different than what this proposal is
trying to achieve. Right now I’m just trying to smooth out the singular vs.
vector functionality of the existing Swift pointer types. Maybe your idea
would be better in a separate proposal?

On Wed, Jul 12, 2017 at 4:16 PM, Gor Gyolchanyan <gor at gyolchanyan.com>
wrote:

> In my understanding, the address manipulation abstractions are as follows:
>
>    - An *address* is an integer denoting an index into the current *address
>    space* (which, in case of a user-space process, is a virtual address
>    space, uniquely allocated by the operating system for that process). The *minimal
>    addressable unit* (also known as *byte*, which is not to be confused
>    with an *octet*) of the address space is the number of contiguous *binary
>    digits* (also known as *bits*) that are uniquely identified by a
>    single address (in most modern architectures that number is *8*).
>    - A *buffer* is a range of addresses, denoting a fragment of the
>    address space, where the delimited contiguous sequence of addresses share
>    common semantics.
>    - A *pointer* is a buffer with some metadata. In statically typed
>    languages the length of the buffer is a compile-time constant and metadata
>    contains type information of the buffer’s contents.
>    - An *array* is a buffer, representing a contiguous sequence of
>    pointers.
>
>
> Notice, that in above terms the address space is essentially a buffer
> denoting all available addresses.
>
> In Swift, the aforementioned concepts correspond to the following types:
>
>    - *address*: UnsafeRawPointer, UnsafeRawMutablePointer, OpaquePointer.
>    - *buffer*: UnsafeRawBufferPointer, UnsafeMutableRawBufferPointer.
>    - *pointer*: UnsafePointer, UnsafeMutablePointer,
>    AutoreleasingUnsafeMutablePointer.
>    - *array*: UnsafeBufferPointer, UnsafeMutableBufferPointer.
>
>
> If the unsafe tools of Swift would be redesigned, I’d suggest defining
> them in the following manner:
>
>    - A comparable and hashable address that can be added an address
>    offset, and subtracted another address to get an address offset. An address
>    would also expose a mutable/immutable property that is the byte referred to
>    by that address.
>    - A buffer, that is a random access mutable/immutable collection of
>    addresses, represented as a range.
>    - A pointer, that is a generic type that is represented by an address
>    and the inherent type information of its generic parameter. The pointer
>    would only expose the address in the form of a buffer and would expose the
>    referred object in the form of a mutable/immutable property of the
>    respective type.
>    - An array, that is a random access mutable/immutable collection of
>    pointers, represented as a pointer coupled by a count.
>
>
> So, in the end, we’d have the following set of types: UnsafeAddress,
> UnsafeBuffer, UnsafePointer, UnsafeArray (as well as their mutable
> counter-parts).
> The OpaquePointer would then be a thin wrapper around UnsafeAddress and
> would probably better off renamed to CStructPointer, due to the fact that
> all non-C usages of this type should be replaced by usage of UnsafeAddress
> .
> And the AutoreleasingUnsafeMutablePointer would also be a thing wrapper
> around UnsafeAddress with a possible renaming to NSObjectPointer.
>
> On Jul 12, 2017, at 10:26 PM, Taylor Swift via swift-evolution <
> swift-evolution at swift.org> wrote:
>
> Hi all, I’ve written up a proposal to modify the unsafe pointer API for
> greater consistency, safety, and ease of use.
>
> ~~~
>
> Swift currently offers two sets of pointer types — singular pointers such
> as UnsafeMutablePointer, and vector (buffer) pointers such as
> UnsafeMutable*Buffer*Pointer. This implies a natural separation of tasks
> the two kinds of pointers are meant to do. For example, buffer pointers
> implement Collection conformance, while singular pointers do not.
>
> However, some aspects of the pointer design contradict these implied
> roles. It is possible to allocate an arbitrary number of instances from a
> type method on a singular pointer, but not from a buffer pointer. The
> result of such an operation returns a singular pointer, even though a
> buffer pointer would be more appropriate to capture the information about
> the *number* of instances allocated. It’s possible to subscript into a
> singular pointer, even though they are not real Collections. Some parts
> of the current design turn UnsafePointers into downright *Dangerous*Pointers,
> leading users to believe that they have allocated or freed memory when in
> fact, they have not.
>
> This proposal seeks to iron out these inconsistencies, and offer a more
> convenient, more sensible, and less bug-prone API for Swift pointers.
>
> <https://gist.github.com/kelvin13/a9c033193a28b1d4960a89b25fbffb06>
>
> ~~~
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20170712/a3580d5d/attachment.html>


More information about the swift-evolution mailing list