|
11 | 11 | //===----------------------------------------------------------------------===//
|
12 | 12 |
|
13 | 13 | /// The memory layout of a type, describing its size, stride, and alignment.
|
| 14 | +/// |
| 15 | +/// You can use `MemoryLayout` as a source of information about a type when |
| 16 | +/// allocating or binding memory using unsafe pointers. The following example |
| 17 | +/// declares a `Point` type with `x` and `y` coordinates and a Boolean |
| 18 | +/// `isFilled` property. |
| 19 | +/// |
| 20 | +/// struct Point { |
| 21 | +/// let x: Double |
| 22 | +/// let y: Double |
| 23 | +/// let isFilled: Bool |
| 24 | +/// } |
| 25 | +/// |
| 26 | +/// The size, stride, and alignment of the `Point` type are accessible as |
| 27 | +/// static properties of `MemoryLayout<Point>`. |
| 28 | +/// |
| 29 | +/// // MemoryLayout<Point>.size == 17 |
| 30 | +/// // MemoryLayout<Point>.stride == 24 |
| 31 | +/// // MemoryLayout<Point>.alignment == 8 |
| 32 | +/// |
| 33 | +/// Always use a multiple of a type's `stride` instead of its `size` when |
| 34 | +/// allocating memory or accounting for the distance between instances in |
| 35 | +/// memory. This example allocates untyped, uninitialized memory with space |
| 36 | +/// for four instances of `Point`. |
| 37 | +/// |
| 38 | +/// let count = 4 |
| 39 | +/// let pointPointer = UnsafeMutableRawPointer.allocate( |
| 40 | +/// bytes: count * MemoryLayout<Point>.stride, |
| 41 | +/// alignedTo: MemoryLayout<Point>.alignment) |
14 | 42 | public enum MemoryLayout<T> {
|
15 |
| - /// The contiguous memory footprint of `T`. |
| 43 | + /// The contiguous memory footprint of `T`, in bytes. |
16 | 44 | ///
|
17 |
| - /// Does not include any dynamically-allocated or "remote" storage. In |
18 |
| - /// particular, `MemoryLayout<T>.size`, when `T` is a class type, is the same |
19 |
| - /// regardless of how many stored properties `T` has. |
| 45 | + /// A type's size does not include any dynamically allocated or remote |
| 46 | + /// storage. In particular, `MemoryLayout<T>.size`, when `T` is a class |
| 47 | + /// type, is the same regardless of how many stored properties `T` has. |
| 48 | + /// |
| 49 | + /// When allocating memory for multiple instances of `T` using an unsafe |
| 50 | + /// pointer, use a multiple of the type's `stride` property instead of its |
| 51 | + /// `size`. |
| 52 | + /// |
| 53 | + /// - SeeAlso: `stride` |
20 | 54 | @_transparent
|
21 | 55 | public static var size: Int {
|
22 | 56 | return Int(Builtin.sizeof(T.self))
|
23 | 57 | }
|
24 | 58 |
|
25 | 59 | /// The number of bytes from the start of one instance of `T` to the start of
|
26 |
| - /// the next in an `Array<T>`. |
| 60 | + /// the next when stored in contiguous memory or in an `Array<T>`. |
27 | 61 | ///
|
28 | 62 | /// This is the same as the number of bytes moved when an `UnsafePointer<T>`
|
29 |
| - /// is incremented. `T` may have a lower minimal alignment that trades runtime |
30 |
| - /// performance for space efficiency. The result is always positive. |
| 63 | + /// instance is incremented. `T` may have a lower minimal alignment that |
| 64 | + /// trades runtime performance for space efficiency. This value is always |
| 65 | + /// positive. |
31 | 66 | @_transparent
|
32 | 67 | public static var stride: Int {
|
33 | 68 | return Int(Builtin.strideof(T.self))
|
34 | 69 | }
|
35 | 70 |
|
36 |
| - /// The default memory alignment of `T`. |
| 71 | + /// The default memory alignment of `T`, in bytes. |
| 72 | + /// |
| 73 | + /// Use the `alignment` property for a type when allocating memory using an |
| 74 | + /// unsafe pointer. This value is always positive. |
37 | 75 | @_transparent
|
38 | 76 | public static var alignment: Int {
|
39 | 77 | return Int(Builtin.alignof(T.self))
|
40 | 78 | }
|
41 | 79 | }
|
42 | 80 |
|
43 | 81 | extension MemoryLayout {
|
44 |
| - /// Returns the contiguous memory footprint of `T`. |
| 82 | + /// Returns the contiguous memory footprint of the given instance. |
| 83 | + /// |
| 84 | + /// The result does not include any dynamically allocated or remote |
| 85 | + /// storage. In particular, `MemoryLayout.size(ofValue: x)`, when `x` is an |
| 86 | + /// instance of a class `C`, is the same regardless of how many stored |
| 87 | + /// properties `C` has. |
| 88 | + /// |
| 89 | + /// When you have a type instead of an instance, use the |
| 90 | + /// `MemoryLayout<T>.size` static property instead. |
| 91 | + /// |
| 92 | + /// let x: Int = 100 |
45 | 93 | ///
|
46 |
| - /// Does not include any dynamically-allocated or "remote" storage. In |
47 |
| - /// particular, `MemoryLayout.size(ofValue: x)`, when `x` is a class instance, |
48 |
| - /// is the same regardless of how many stored properties `T` has. |
| 94 | + /// // Finding the size of a value's type |
| 95 | + /// let s = MemoryLayout.size(ofValue: x) |
| 96 | + /// // s == 8 |
| 97 | + /// |
| 98 | + /// // Finding the size of a type directly |
| 99 | + /// let t = MemoryLayout<Int>.size |
| 100 | + /// // t == 8 |
| 101 | + /// |
| 102 | + /// - Parameter value: A value representative of the type to describe. |
| 103 | + /// - Returns: The size, in bytes, of the given value's type. |
| 104 | + /// |
| 105 | + /// - SeeAlso: `MemoryLayout.size` |
49 | 106 | @_transparent
|
50 |
| - public static func size(ofValue _: T) -> Int { |
| 107 | + public static func size(ofValue value: T) -> Int { |
51 | 108 | return MemoryLayout.size
|
52 | 109 | }
|
53 | 110 |
|
54 | 111 | /// Returns the number of bytes from the start of one instance of `T` to the
|
55 |
| - /// start of the next in an `Array<T>`. |
| 112 | + /// start of the next when stored in contiguous memory or in an `Array<T>`. |
56 | 113 | ///
|
57 | 114 | /// This is the same as the number of bytes moved when an `UnsafePointer<T>`
|
58 |
| - /// is incremented. `T` may have a lower minimal alignment that trades runtime |
59 |
| - /// performance for space efficiency. The result is always positive. |
| 115 | + /// instance is incremented. `T` may have a lower minimal alignment that |
| 116 | + /// trades runtime performance for space efficiency. The result is always |
| 117 | + /// positive. |
| 118 | + /// |
| 119 | + /// When you have a type instead of an instance, use the |
| 120 | + /// `MemoryLayout<T>.stride` static property instead. |
| 121 | + /// |
| 122 | + /// let x: Int = 100 |
| 123 | + /// |
| 124 | + /// // Finding the stride of a value's type |
| 125 | + /// let s = MemoryLayout.stride(ofValue: x) |
| 126 | + /// // s == 8 |
| 127 | + /// |
| 128 | + /// // Finding the stride of a type directly |
| 129 | + /// let t = MemoryLayout<Int>.stride |
| 130 | + /// // t == 8 |
| 131 | + /// |
| 132 | + /// - Parameter value: A value representative of the type to describe. |
| 133 | + /// - Returns: The stride, in bytes, of the given value's type. |
| 134 | + /// |
| 135 | + /// - SeeAlso: `MemoryLayout.stride` |
60 | 136 | @_transparent
|
61 |
| - public static func stride(ofValue _: T) -> Int { |
| 137 | + public static func stride(ofValue value: T) -> Int { |
62 | 138 | return MemoryLayout.stride
|
63 | 139 | }
|
64 | 140 |
|
65 | 141 | /// Returns the default memory alignment of `T`.
|
| 142 | + /// |
| 143 | + /// Use a type's alignment when allocating memory using an unsafe pointer. |
| 144 | + /// |
| 145 | + /// When you have a type instead of an instance, use the |
| 146 | + /// `MemoryLayout<T>.stride` static property instead. |
| 147 | + /// |
| 148 | + /// let x: Int = 100 |
| 149 | + /// |
| 150 | + /// // Finding the alignment of a value's type |
| 151 | + /// let s = MemoryLayout.alignment(ofValue: x) |
| 152 | + /// // s == 8 |
| 153 | + /// |
| 154 | + /// // Finding the alignment of a type directly |
| 155 | + /// let t = MemoryLayout<Int>.alignment |
| 156 | + /// // t == 8 |
| 157 | + /// |
| 158 | + /// - Parameter value: A value representative of the type to describe. |
| 159 | + /// - Returns: The default memory alignment, in bytes, of the given value's |
| 160 | + /// type. This value is always positive. |
| 161 | + /// |
| 162 | + /// - SeeAlso: `MemoryLayout.alignment` |
66 | 163 | @_transparent
|
67 |
| - public static func alignment(ofValue _: T) -> Int { |
| 164 | + public static func alignment(ofValue value: T) -> Int { |
68 | 165 | return MemoryLayout.alignment
|
69 | 166 | }
|
70 | 167 | }
|
0 commit comments