Skip to content

Commit 6c50752

Browse files
author
Maxim Moiseev
committed
UnsafePointer: initializeMemory and deinitializePointee
1 parent 0e54467 commit 6c50752

37 files changed

+103
-103
lines changed

Diff for: stdlib/private/StdlibUnittest/OpaqueIdentityFunctions.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ func _stdlib_getPointer(x: OpaquePointer) -> OpaquePointer
1515

1616
public func _opaqueIdentity<T>(x: T) -> T {
1717
let ptr = UnsafeMutablePointer<T>(allocatingCapacity: 1)
18-
ptr.initialize(x)
18+
ptr.initializeMemory(x)
1919
let result =
2020
UnsafeMutablePointer<T>(_stdlib_getPointer(OpaquePointer(ptr))).pointee
21-
ptr.destroy()
21+
ptr.deinitializePointee()
2222
ptr.deallocateCapacity(1)
2323
return result
2424
}

Diff for: stdlib/private/StdlibUnittest/StdlibUnittest.swift.gyb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1937,7 +1937,7 @@ public func checkSequence<
19371937
for i in 0..<(end - buf) {
19381938
expectTrue(sameValue(expected[j++], buf[i]))
19391939
}
1940-
buf.destroy(end - buf)
1940+
buf.deinitializePointee(end - buf)
19411941
buf.deallocateCapacity(count)
19421942
}
19431943
}

Diff for: stdlib/private/SwiftPrivate/ShardedAtomicCounter.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,14 @@ public struct _stdlib_ShardedAtomicCounter {
3535
let count = max(8, hardwareConcurrency * hardwareConcurrency)
3636
let shards = UnsafeMutablePointer<Int>(allocatingCapacity: count)
3737
for var i = 0; i != count; i++ {
38-
(shards + i).initialize(0)
38+
(shards + i).initializeMemory(0)
3939
}
4040
self._shardsPtr = shards
4141
self._shardsCount = count
4242
}
4343

4444
public func `deinit`() {
45-
self._shardsPtr.destroy(self._shardsCount)
45+
self._shardsPtr.deinitializePointee(self._shardsCount)
4646
self._shardsPtr.deallocateCapacity(self._shardsCount)
4747
}
4848

Diff for: stdlib/private/SwiftPrivatePthreadExtras/PthreadBarriers.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,9 @@ public func _stdlib_pthread_barrier_destroy(
9292
// FIXME: leaking memory.
9393
return -1
9494
}
95-
barrier.pointee.cond.destroy()
95+
barrier.pointee.cond.deinitializePointee()
9696
barrier.pointee.cond.deallocateCapacity(1)
97-
barrier.pointee.mutex.destroy()
97+
barrier.pointee.mutex.deinitializePointee()
9898
barrier.pointee.mutex.deallocateCapacity(1)
9999
return 0
100100
}

Diff for: stdlib/private/SwiftPrivatePthreadExtras/SwiftPrivatePthreadExtras.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ internal class PthreadBlockContextImpl<Argument, Result>: PthreadBlockContext {
4242

4343
override func run() -> UnsafeMutablePointer<Void> {
4444
let result = UnsafeMutablePointer<Result>(allocatingCapacity: 1)
45-
result.initialize(block(arg))
45+
result.initializeMemory(block(arg))
4646
return UnsafeMutablePointer(result)
4747
}
4848
}
@@ -91,7 +91,7 @@ public func _stdlib_pthread_join<Result>(
9191
let result = pthread_join(thread, &threadResultPtr)
9292
if result == 0 {
9393
let threadResult = UnsafeMutablePointer<Result>(threadResultPtr).pointee
94-
threadResultPtr.destroy()
94+
threadResultPtr.deinitializePointee()
9595
threadResultPtr.deallocateCapacity(1)
9696
return (result, threadResult)
9797
} else {

Diff for: stdlib/public/SDK/Foundation/NSError.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public func _stdlib_bridgeNSErrorToErrorProtocol<
5454
T : _ObjectiveCBridgeableErrorProtocol
5555
>(error: NSError, out: UnsafeMutablePointer<T>) -> Bool {
5656
if let bridged = T(_bridgedNSError: error) {
57-
out.initialize(bridged)
57+
out.initializeMemory(bridged)
5858
return true
5959
} else {
6060
return false

Diff for: stdlib/public/core/ArrayBuffer.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ extension _ArrayBuffer {
224224
// Make another pass to retain the copied objects
225225
var result = target
226226
for _ in bounds {
227-
result.initialize(result.pointee)
227+
result.initializeMemory(result.pointee)
228228
++result
229229
}
230230
return result

Diff for: stdlib/public/core/ArrayBufferProtocol.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ extension _ArrayBufferProtocol {
162162
}
163163
// Initialize the hole left by sliding the tail forward
164164
for j in oldTailIndex..<newTailIndex {
165-
(elements + j).initialize(newValues[i++])
165+
(elements + j).initializeMemory(newValues[i++])
166166
}
167167
_expectEnd(i, newValues)
168168
}
@@ -197,7 +197,7 @@ extension _ArrayBufferProtocol {
197197
newTailStart.moveAssignFrom(oldTailStart, count: tailCount)
198198

199199
// Destroy elements remaining after the tail in subRange
200-
(newTailStart + tailCount).destroy(shrinkage - tailCount)
200+
(newTailStart + tailCount).deinitializePointee(shrinkage - tailCount)
201201
}
202202
}
203203
}

Diff for: stdlib/public/core/ArrayCast.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public func _arrayForceCast<SourceElement, TargetElement>(
8383
_precondition(
8484
bridged != nil, "array element cannot be bridged to Objective-C")
8585
// FIXME: should be an unsafeDowncast, but for <rdar://problem/18638230>
86-
p++.initialize(unsafeBitCast(bridged!, TargetElement.self))
86+
p++.initializeMemory(unsafeBitCast(bridged!, TargetElement.self))
8787
}
8888
}
8989
return Array(_ArrayBuffer(buf, shiftedToStartIndex: 0))
@@ -162,7 +162,7 @@ ElementwiseBridging:
162162
if _slowPath(value == nil) {
163163
break ElementwiseBridging
164164
}
165-
p++.initialize(value!)
165+
p++.initializeMemory(value!)
166166
}
167167
return Array(_ArrayBuffer(buf, shiftedToStartIndex: 0))
168168
}

Diff for: stdlib/public/core/Arrays.swift.gyb

+8-8
Original file line numberDiff line numberDiff line change
@@ -510,7 +510,7 @@ extension ${Self} : _ArrayProtocol {
510510
var p: UnsafeMutablePointer<Element>
511511
(self, p) = ${Self}._allocateUninitialized(count)
512512
for _ in 0..<count {
513-
p++.initialize(repeatedValue)
513+
p++.initializeMemory(repeatedValue)
514514
}
515515
}
516516

@@ -668,7 +668,7 @@ extension ${Self} : _ArrayProtocol {
668668
_sanityCheck(_buffer.capacity >= _buffer.count + 1)
669669

670670
_buffer.count = oldCount + 1
671-
(_buffer.firstElementAddress + oldCount).initialize(newElement)
671+
(_buffer.firstElementAddress + oldCount).initializeMemory(newElement)
672672
}
673673

674674
/// Append `newElement` to the ${Self}.
@@ -896,7 +896,7 @@ internal struct _InitializeMemoryFromCollection<
896896
var p = rawMemory
897897
var q = newValues.startIndex
898898
for _ in 0..<count {
899-
p++.initialize(newValues[q++])
899+
p++.initializeMemory(newValues[q++])
900900
}
901901
_expectEnd(q, newValues)
902902
}
@@ -1119,13 +1119,13 @@ internal func _arrayOutOfPlaceUpdate<
11191119
// its real first element
11201120
let backingStart = backing.firstElementAddress
11211121
let sourceOffset = sourceStart - backingStart
1122-
backingStart.destroy(sourceOffset)
1122+
backingStart.deinitializePointee(sourceOffset)
11231123

11241124
// Move the head items
11251125
destStart.moveInitializeFrom(sourceStart, count: headCount)
11261126

11271127
// Destroy unused source items
1128-
oldStart.destroy(oldCount)
1128+
oldStart.deinitializePointee(oldCount)
11291129

11301130
initializeNewElements.call(newStart, count: newCount)
11311131

@@ -1136,7 +1136,7 @@ internal func _arrayOutOfPlaceUpdate<
11361136
// its real last element
11371137
let backingEnd = backingStart + backing.count
11381138
let sourceEnd = sourceStart + sourceCount
1139-
sourceEnd.destroy(backingEnd - sourceEnd)
1139+
sourceEnd.deinitializePointee(backingEnd - sourceEnd)
11401140
backing.count = 0
11411141
}
11421142
else {
@@ -1156,7 +1156,7 @@ internal struct _InitializePointer<T> : _PointerFunction {
11561156
internal func call(rawMemory: UnsafeMutablePointer<T>, count: Int) {
11571157
_sanityCheck(count == 1)
11581158
// FIXME: it would be better if we could find a way to move, here
1159-
rawMemory.initialize(newValue)
1159+
rawMemory.initializeMemory(newValue)
11601160
}
11611161

11621162
@_transparent
@@ -1231,7 +1231,7 @@ internal func _arrayAppendSequence<
12311231
let base = buffer.firstElementAddress
12321232

12331233
while (nextItem != nil) && count < capacity {
1234-
(base + count++).initialize(nextItem!)
1234+
(base + count++).initializeMemory(nextItem!)
12351235
nextItem = stream.next()
12361236
}
12371237
buffer.count = count

Diff for: stdlib/public/core/Collection.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -579,7 +579,7 @@ extension Sequence
579579
} else {
580580
var p = ptr
581581
for x in self {
582-
p++.initialize(x)
582+
p++.initializeMemory(x)
583583
}
584584
return p
585585
}

Diff for: stdlib/public/core/ContiguousArrayBuffer.swift

+5-5
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,8 @@ class _ContiguousArrayStorage1 : _ContiguousArrayStorageBase {
101101
final class _ContiguousArrayStorage<Element> : _ContiguousArrayStorage1 {
102102

103103
deinit {
104-
__manager._elementPointer.destroy(__manager._valuePointer.pointee.count)
105-
__manager._valuePointer.destroy()
104+
__manager._elementPointer.deinitializePointee(__manager._valuePointer.pointee.count)
105+
__manager._valuePointer.deinitializePointee()
106106
_fixLifetime(__manager)
107107
}
108108

@@ -146,7 +146,7 @@ final class _ContiguousArrayStorage<Element> : _ContiguousArrayStorage1 {
146146
let resultPtr = result.baseAddress
147147
let p = __manager._elementPointer
148148
for i in 0..<count {
149-
(resultPtr + i).initialize(_bridgeToObjectiveCUnconditional(p[i]))
149+
(resultPtr + i).initializeMemory(_bridgeToObjectiveCUnconditional(p[i]))
150150
}
151151
_fixLifetime(__manager)
152152
return result
@@ -608,7 +608,7 @@ internal func _copyCollectionToNativeArrayBuffer<
608608
var i = source.startIndex
609609
for _ in 0..<count {
610610
// FIXME(performance): use _initializeTo().
611-
(p++).initialize(source[i++])
611+
(p++).initializeMemory(source[i++])
612612
}
613613
_expectEnd(i, source)
614614
return result
@@ -665,7 +665,7 @@ internal struct _UnsafePartiallyInitializedContiguousArrayBuffer<Element> {
665665
"_UnsafePartiallyInitializedContiguousArrayBuffer has no more capacity")
666666
remainingCapacity--
667667

668-
(p++).initialize(element)
668+
(p++).initializeMemory(element)
669669
}
670670

671671
/// Finish initializing the buffer, adjusting its count to the final

Diff for: stdlib/public/core/HashedCollections.swift.gyb

+15-15
Original file line numberDiff line numberDiff line change
@@ -1642,7 +1642,7 @@ internal struct _BitMap {
16421642

16431643
internal func initializeToZero() {
16441644
for i in 0 ..< numberOfWords {
1645-
(values + i).initialize(0)
1645+
(values + i).initializeMemory(0)
16461646
}
16471647
}
16481648

@@ -1845,7 +1845,7 @@ final internal class _Native${Self}StorageImpl<${TypeParameters}> :
18451845
if !_isPOD(Key.self) {
18461846
for i in 0 ..< capacity {
18471847
if initializedEntries[i] {
1848-
(keys+i).destroy()
1848+
(keys+i).deinitializePointee()
18491849
}
18501850
}
18511851
}
@@ -1854,12 +1854,12 @@ final internal class _Native${Self}StorageImpl<${TypeParameters}> :
18541854
if !_isPOD(Value.self) {
18551855
for i in 0 ..< capacity {
18561856
if initializedEntries[i] {
1857-
(values+i).destroy()
1857+
(values+i).deinitializePointee()
18581858
}
18591859
}
18601860
}
18611861
%end
1862-
buffer._valuePointer.destroy()
1862+
buffer._valuePointer.deinitializePointee()
18631863
_fixLifetime(self)
18641864
}
18651865
}
@@ -1968,9 +1968,9 @@ struct _Native${Self}Storage<${TypeParametersDecl}> :
19681968
@_transparent
19691969
internal func destroyEntryAt(i: Int) {
19701970
_sanityCheck(isInitializedEntry(i))
1971-
(keys + i).destroy()
1971+
(keys + i).deinitializePointee()
19721972
%if Self == 'Dictionary':
1973-
(values + i).destroy()
1973+
(values + i).deinitializePointee()
19741974
%end
19751975
initializedEntries[i] = false
19761976
_fixLifetime(self)
@@ -1981,15 +1981,15 @@ struct _Native${Self}Storage<${TypeParametersDecl}> :
19811981
internal func initializeKey(k: Key, at i: Int) {
19821982
_sanityCheck(!isInitializedEntry(i))
19831983

1984-
(keys + i).initialize(k)
1984+
(keys + i).initializeMemory(k)
19851985
initializedEntries[i] = true
19861986
_fixLifetime(self)
19871987
}
19881988

19891989
@_transparent
19901990
internal func moveInitializeFrom(from: Storage, at: Int, toEntryAt: Int) {
19911991
_sanityCheck(!isInitializedEntry(toEntryAt))
1992-
(keys + toEntryAt).initialize((from.keys + at).move())
1992+
(keys + toEntryAt).initializeMemory((from.keys + at).move())
19931993
from.initializedEntries[at] = false
19941994
initializedEntries[toEntryAt] = true
19951995
}
@@ -2007,17 +2007,17 @@ struct _Native${Self}Storage<${TypeParametersDecl}> :
20072007
internal func initializeKey(k: Key, value v: Value, at i: Int) {
20082008
_sanityCheck(!isInitializedEntry(i))
20092009

2010-
(keys + i).initialize(k)
2011-
(values + i).initialize(v)
2010+
(keys + i).initializeMemory(k)
2011+
(values + i).initializeMemory(v)
20122012
initializedEntries[i] = true
20132013
_fixLifetime(self)
20142014
}
20152015

20162016
@_transparent
20172017
internal func moveInitializeFrom(from: Storage, at: Int, toEntryAt: Int) {
20182018
_sanityCheck(!isInitializedEntry(toEntryAt))
2019-
(keys + toEntryAt).initialize((from.keys + at).move())
2020-
(values + toEntryAt).initialize((from.values + at).move())
2019+
(keys + toEntryAt).initializeMemory((from.keys + at).move())
2020+
(values + toEntryAt).initializeMemory((from.values + at).move())
20212021
from.initializedEntries[at] = false
20222022
initializedEntries[toEntryAt] = true
20232023
}
@@ -2330,7 +2330,7 @@ internal struct _BridgedNative${Self}Storage {
23302330
internal func initializeKey(k: AnyObject, at i: Int) {
23312331
_sanityCheck(!isInitializedEntry(i))
23322332

2333-
(keys + i).initialize(k)
2333+
(keys + i).initializeMemory(k)
23342334
initializedEntries[i] = true
23352335
_fixLifetime(self)
23362336
}
@@ -2340,8 +2340,8 @@ internal struct _BridgedNative${Self}Storage {
23402340
) {
23412341
_sanityCheck(!isInitializedEntry(i))
23422342

2343-
(keys + i).initialize(k)
2344-
(values + i).initialize(v)
2343+
(keys + i).initializeMemory(k)
2344+
(values + i).initializeMemory(v)
23452345
initializedEntries[i] = true
23462346
_fixLifetime(self)
23472347
}

Diff for: stdlib/public/core/HeapBuffer.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class _HeapBufferStorage<Value,Element> : NonObjectiveCBase {
4545
/// `_HeapBufferStorage<Value,Element>`.
4646
typealias Buffer = _HeapBuffer<Value, Element>
4747
deinit {
48-
Buffer(self)._value.destroy()
48+
Buffer(self)._value.deinitializePointee()
4949
}
5050

5151
@warn_unused_result
@@ -173,7 +173,7 @@ struct _HeapBuffer<Value, Element> : Equatable {
173173
let object: AnyObject = _swift_bufferAllocate(
174174
storageClass, totalSize, alignMask)
175175
self._storage = Builtin.castToNativeObject(object)
176-
self._value.initialize(initializer)
176+
self._value.initializeMemory(initializer)
177177
}
178178

179179
public // @testable

Diff for: stdlib/public/core/ManagedBuffer.swift

+4-4
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ public class ManagedBuffer<Value, Element>
112112

113113
/// Destroy the stored Value.
114114
deinit {
115-
ManagedBufferPointer(self).withUnsafeMutablePointerToValue { $0.destroy() }
115+
ManagedBufferPointer(self).withUnsafeMutablePointerToValue { $0.deinitializePointee() }
116116
}
117117

118118
/// The stored `Value` instance.
@@ -147,8 +147,8 @@ public class ManagedBuffer<Value, Element>
147147
/// deinit {
148148
/// Manager(unsafeBufferObject: self).withUnsafeMutablePointers {
149149
/// (pointerToValue, pointerToElements)->Void in
150-
/// pointerToElements.destroy(self.count)
151-
/// pointerToValue.destroy()
150+
/// pointerToElements.deinitializePointee(self.count)
151+
/// pointerToValue.deinitializePointee()
152152
/// }
153153
/// }
154154
///
@@ -187,7 +187,7 @@ public struct ManagedBufferPointer<Value, Element> : Equatable {
187187

188188
// initialize the value field
189189
withUnsafeMutablePointerToValue {
190-
$0.initialize(
190+
$0.initializeMemory(
191191
initialValue(
192192
buffer: self.buffer,
193193
allocatedCount: {

0 commit comments

Comments
 (0)